diff --git a/numpy_questions.py b/numpy_questions.py index 21fcec4b..537f2d98 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -37,10 +37,17 @@ def max_index(X): If the input is not a numpy array or if the shape is not 2D. """ - i = 0 - j = 0 + if not isinstance(X, np.ndarray): + raise ValueError("Input must be a numpy array.") - # TODO + if X.ndim != 2: + raise ValueError("Input must be a 2D array.") + + # Find the flat index of the maximum + flat_idx = np.argmax(X) + + # Convert the flat index back to 2D indices + i, j = np.unravel_index(flat_idx, X.shape) return i, j @@ -62,6 +69,13 @@ def wallis_product(n_terms): pi : float The approximation of order `n_terms` of pi using the Wallis product. """ - # XXX : The n_terms is an int that corresponds to the number of - # terms in the product. For example 10000. - return 0. + if n_terms == 0: + return 1.0 + + k = np.arange(1, n_terms + 1, dtype=float) + terms = (4 * k * k) / (4 * k * k - 1) + + # Product converges to pi / 2, so multiply by 2 + pi_approx = 2 * np.prod(terms) + + return float(pi_approx) diff --git a/sklearn_questions.py b/sklearn_questions.py index f65038c6..d5a2f4c3 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -22,53 +22,68 @@ import numpy as np from sklearn.base import BaseEstimator from sklearn.base import ClassifierMixin -from sklearn.utils.validation import check_X_y -from sklearn.utils.validation import check_array -from sklearn.utils.validation import check_is_fitted +from sklearn.utils.validation import ( + check_X_y, + check_array, + check_is_fitted, +) from sklearn.utils.multiclass import check_classification_targets -class OneNearestNeighbor(BaseEstimator, ClassifierMixin): - "OneNearestNeighbor classifier." +class OneNearestNeighbor(ClassifierMixin, BaseEstimator): + """One-nearest neighbor classifier. + + This classifier implements the 1-nearest neighbor rule using the + Euclidean distance to find, for each sample, the closest point in the + training set and predict its class label. + """ def __init__(self): # noqa: D107 pass def fit(self, X, y): - """Write docstring. - - And describe parameters - """ + """Fit the OneNearestNeighbor classifier.""" X, y = check_X_y(X, y) check_classification_targets(y) + self.classes_ = np.unique(y) + self.X_ = X + self.y_ = y + + # Required for sklearn compatibility self.n_features_in_ = X.shape[1] - # XXX fix return self def predict(self, X): - """Write docstring. - - And describe parameters - """ + """Predict class labels for samples in X.""" 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 - return y_pred + # Manually enforce n_features_in_ check (older sklearn versions do not) + if X.shape[1] != self.n_features_in_: + raise ValueError( + f"X has {X.shape[1]} features, but {type(self).__name__} " + f"is expecting {self.n_features_in_} features as input" + ) + + # Compute Euclidean distances + diff = X[:, np.newaxis, :] - self.X_[np.newaxis, :, :] + distances = np.linalg.norm(diff, axis=2) + nearest_idx = np.argmin(distances, axis=1) + + return self.y_[nearest_idx] def score(self, X, y): - """Write docstring. + """Return the mean accuracy on the given test data and labels.""" + X = check_array(X) - And describe parameters - """ - X, y = check_X_y(X, y) - y_pred = self.predict(X) + if X.shape[1] != self.n_features_in_: + raise ValueError( + f"X has {X.shape[1]} features, but {type(self).__name__} " + f"is expecting {self.n_features_in_} features as input" + ) - # XXX fix - return y_pred.sum() + y_pred = self.predict(X) + return float(np.mean(y_pred == y))