diff --git a/numpy_questions.py b/numpy_questions.py index 21fcec4b..96c8820a 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -19,49 +19,30 @@ def max_index(X): - """Return the index of the maximum in a numpy array. + """Return the index of the maximum in a numpy array.""" + # Check input type + if not isinstance(X, np.ndarray): + raise ValueError("Input must be a numpy array.") - Parameters - ---------- - X : ndarray of shape (n_samples, n_features) - The input array. + # Check shape + if X.ndim != 2: + raise ValueError("Input must be a 2D array.") - Returns - ------- - (i, j) : tuple(int) - The row and columnd index of the maximum. - - Raises - ------ - ValueError - If the input is not a numpy array or - if the shape is not 2D. - """ - i = 0 - j = 0 - - # TODO + # Find the index of maximum + flat_index = np.argmax(X) + i, j = np.unravel_index(flat_index, X.shape) return i, j def wallis_product(n_terms): - """Implement the Wallis product to compute an approximation of pi. - - See: - https://en.wikipedia.org/wiki/Wallis_product + """Compute an approximation of pi using the Wallis product.""" + if n_terms == 0: + return 1.0 # by definition - Parameters - ---------- - n_terms : int - Number of steps in the Wallis product. Note that `n_terms=0` will - consider the product to be `1`. + product = 1.0 + for n in range(1, n_terms + 1): + term = (4 * n * n) / (4 * n * n - 1) + product *= term - Returns - ------- - 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. + return 2 * product diff --git a/sklearn_questions.py b/sklearn_questions.py index f65038c6..8f379d93 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -28,47 +28,90 @@ from sklearn.utils.multiclass import check_classification_targets -class OneNearestNeighbor(BaseEstimator, ClassifierMixin): - "OneNearestNeighbor classifier." +class OneNearestNeighbor(ClassifierMixin, BaseEstimator): + """OneNearestNeighbor classifier.""" def __init__(self): # noqa: D107 pass def fit(self, X, y): - """Write docstring. + """Fit the OneNearestNeighbor classifier. - And describe parameters + Parameters + ---------- + X : array-like of shape (n_samples, n_features) + Training data. + + y : array-like of shape (n_samples,) + Class labels. + + Returns + ------- + self : object + Fitted estimator. """ + # Validate X and y X, y = check_X_y(X, y) check_classification_targets(y) + + # Store training data + self.X_train_ = X + self.y_train_ = y + + # Required by sklearn API self.classes_ = np.unique(y) self.n_features_in_ = X.shape[1] - # XXX fix return self def predict(self, X): - """Write docstring. + """Predict class labels using nearest neighbor. + + Parameters + ---------- + X : array-like of shape (n_samples, n_features) + Input data. - And describe parameters + Returns + ------- + y_pred : ndarray of shape (n_samples,) + Predicted 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 - return y_pred + # required by sklearn test: MUST match regex + if X.shape[1] != self.n_features_in_: + raise ValueError( + f"X has {X.shape[1]} features, but " + f"expects {self.n_features_in_} features as input" + ) + + y_pred = [] + + for x in X: + distances = np.linalg.norm(self.X_train_ - x, axis=1) + idx = np.argmin(distances) + y_pred.append(self.y_train_[idx]) + + return np.array(y_pred) def score(self, X, y): - """Write docstring. + """Compute accuracy score. - And describe parameters + Parameters + ---------- + X : array-like + Input features. + + y : array-like + True labels. + + Returns + ------- + accuracy : float """ X, y = check_X_y(X, y) y_pred = self.predict(X) - - # XXX fix - return y_pred.sum() + return np.mean(y_pred == y)