Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions numpy_questions.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
This will be enforced with `flake8`. You can check that there is no flake8
errors by calling `flake8` at the root of the repo.
"""

import numpy as np


Expand All @@ -39,9 +40,11 @@ def max_index(X):
"""
i = 0
j = 0

# TODO

if not isinstance(X, np.ndarray):
raise ValueError("This value is not a numpy array")
if X.ndim != 2:
raise ValueError("This value has the wrong dimensions")
i, j = np.unravel_index(np.argmax(X, axis=None), X.shape)
return i, j


Expand All @@ -62,6 +65,13 @@ def wallis_product(n_terms):
pi : float
The approximation of order `n_terms` of pi using the Wallis product.
"""
if n_terms < 0:
raise ValueError
if n_terms == 0:
return 1.0
current = 1.0
for i in range(1, n_terms+1):
current *= (4 * i**2) / ((4 * i**2)-1)
# XXX : The n_terms is an int that corresponds to the number of
# terms in the product. For example 10000.
return 0.
return current * 2
78 changes: 63 additions & 15 deletions sklearn_questions.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,46 +29,94 @@


class OneNearestNeighbor(BaseEstimator, ClassifierMixin):
"OneNearestNeighbor classifier."
"""One-Nearest-Neighbor classifier using Euclidean distance.

The classifier predicts, for each input sample, the label of the single
closest training sample in Euclidean distance.

Attributes
----------
classes_ : ndarray of shape (n_classes,)
Unique class labels seen during `fit`.

n_features_in_ : int
Number of features seen during `fit`.

X_ : ndarray of shape (n_samples, n_features)
Training data stored after fitting.

y_ : ndarray of shape (n_samples,)
Training labels stored after fitting.
"""

def __init__(self): # noqa: D107
pass

def fit(self, X, y):
"""Write docstring.
"""Fit the One-Nearest-Neighbor classifier.

Parameters
----------
X : array-like of shape (n_samples, n_features)
Training data.

And describe parameters
y : array-like of shape (n_samples,)
Target labels.

Returns
-------
self : OneNearestNeighbor
Fitted estimator.
"""
X, y = check_X_y(X, y)
check_classification_targets(y)
self.classes_ = np.unique(y)
self.n_features_in_ = X.shape[1]

# XXX fix
self.X_ = X
self.y_ = y
return self

def predict(self, X):
"""Write docstring.
"""Predict class labels for samples in X.

Parameters
----------
X : array-like of shape (n_samples, n_features)
Input samples.

And describe parameters
Returns
-------
y_pred : ndarray of shape (n_samples,)
Predicted class labels.
"""
check_is_fitted(self)
X = check_array(X)
y_pred = np.full(
shape=len(X), fill_value=self.classes_[0],
dtype=self.classes_.dtype
)

# XXX fix
X_sq = np.sum(X * X, axis=1, keepdims=True)
Xtr_sq = np.sum(self.X_ * self.X_, axis=1, keepdims=True)
cross = X @ self.X_.T
d2 = X_sq - 2.0 * cross + Xtr_sq.T
nn_idx = np.argmin(d2, axis=1)
y_pred = self.y_[nn_idx]
return y_pred

def score(self, X, y):
"""Write docstring.

And describe parameters
"""Compute the mean accuracy on the given test data and labels.

Parameters
----------
X : array-like of shape (n_samples, n_features)
Test samples.
y : array-like of shape (n_samples,)
True labels.
Returns
-------
score : float
Mean accuracy of predictions on X compared to y.
"""
X, y = check_X_y(X, y)
y_pred = self.predict(X)

# XXX fix
return y_pred.sum()
return float(np.mean(y_pred == y))