diff --git a/numpy_questions.py b/numpy_questions.py index 21fcec4b..5f0cae7e 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -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 @@ -40,28 +41,44 @@ def max_index(X): i = 0 j = 0 - # TODO + if not isinstance(X, np.ndarray): + raise ValueError("The input must be a numpy array") + + d = X.ndim + + if d != 2: + raise ValueError("The input must be a 2D array") + + i_flat = np.argmax(X) + + i, j = np.unravel_index(i_flat, 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. Parameters ---------- n_terms : int - Number of steps in the Wallis product. Note that `n_terms=0` will - consider the product to be `1`. + Number of terms in the Wallis product. If `n_terms=0`, + the function returns 1. Returns ------- pi : float - The approximation of order `n_terms` of pi using the Wallis product. + Approximation of pi computed with `n_terms` terms. """ - # 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 + + tab = np.arange(1, n_terms + 1) + n2 = tab * tab + n2x4 = 4 * n2 + n2x4_1 = n2x4 - 1 + final = n2x4 / n2x4_1 + prod = np.prod(final) + + return 2 * prod diff --git a/sklearn_questions.py b/sklearn_questions.py index f65038c6..0663dfab 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -19,6 +19,7 @@ for the methods you code and for the class. The docstring will be checked using `pydocstyle` that you can also call at the root of the repo. """ + import numpy as np from sklearn.base import BaseEstimator from sklearn.base import ClassifierMixin @@ -29,7 +30,13 @@ class OneNearestNeighbor(BaseEstimator, ClassifierMixin): - "OneNearestNeighbor classifier." + """ + One-nearest-neighbor classifier. + + This estimator stores the training data during `fit` and predicts + the label of a new sample as the label of the closest training + sample using the Euclidean distance. + """ def __init__(self): # noqa: D107 pass @@ -44,7 +51,9 @@ def fit(self, X, 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): @@ -55,11 +64,15 @@ def predict(self, 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 + shape=len(X), fill_value=self.classes_[0], dtype=self.classes_.dtype ) - # XXX fix + for i in range(len(X)): + diff = self.X_ - X[i] + dist = np.linalg.norm(diff, axis=1) + idx = np.argmin(dist) + y_pred[i] = self.y_[idx] + return y_pred def score(self, X, y): @@ -71,4 +84,4 @@ def score(self, X, y): y_pred = self.predict(X) # XXX fix - return y_pred.sum() + return (y_pred == y).mean()