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
16 changes: 15 additions & 1 deletion numpy_questions.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ def max_index(X):
j = 0

# TODO
if not isinstance(X, np.ndarray) or X.ndim != 2:
raise ValueError("Input must be a 2D NumPy array")

max_flat_index = np.argmax(X)
i, j = np.unravel_index(max_flat_index, X.shape)

return i, j

Expand All @@ -64,4 +69,13 @@ def wallis_product(n_terms):
"""
# XXX : The n_terms is an int that corresponds to the number of
# terms in the product. For example 10000.
return 0.
product = 1.0

if n_terms == 0:
return product

for n in range(1, n_terms + 1):
numerator = 4 * n * n
product *= numerator / (numerator - 1)

return 2 * product
80 changes: 65 additions & 15 deletions sklearn_questions.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,46 +29,96 @@


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

This classifier assigns to each test sample the label of the
training point closest to it in Euclidean distance.

Methods
-------
fit(X, y) :
Store the training data.

predict(X) :
Predict class labels for samples in X.

score(X, y) :
Return the mean accuracy on the given test data and labels.
"""

def __init__(self): # noqa: D107
"""Initialize the OneNearestNeighbor classifier."""
pass

def fit(self, X, y):
"""Write docstring.
"""Fit the classifier from the training set (X, y).

And describe parameters
Parameters
----------
X : ndarray of shape (n_samples, n_features)
Training data.

y : ndarray of shape (n_samples,)
Target labels.

Returns
-------
self : object
Returns the fitted estimator.
"""
X, y = check_X_y(X, y)
check_classification_targets(y)

self.X_train_ = X
self.y_train_ = y
self.classes_ = np.unique(y)
self.n_features_in_ = X.shape[1]

# XXX fix
return self

def predict(self, X):
"""Write docstring.
"""Predict the class label for each sample in X.

And describe parameters
Parameters
----------
X : ndarray of shape (n_queries, n_features)
Query data.

Returns
-------
y_pred : ndarray of shape (n_queries,)
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
check_is_fitted(self, ["X_train_", "y_train_"])

distances = np.linalg.norm(
X[:, np.newaxis] - self.X_train_, axis=2
)

# XXX fix
nearest_indices = np.argmin(distances, axis=1)

y_pred = self.y_train_[nearest_indices]

return y_pred

def score(self, X, y):
"""Write docstring.
"""Return the mean accuracy on the given test data and labels.

Parameters
----------
X : ndarray of shape (n_samples, n_features)
Test data.

y : ndarray of shape (n_samples,)
True labels for X.

And describe parameters
Returns
-------
score : float
Mean accuracy of predictions.
"""
X, y = check_X_y(X, y)
y_pred = self.predict(X)

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