diff --git a/numpy_questions.py b/numpy_questions.py index 21fcec4b..058f25d1 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 @@ -39,8 +40,13 @@ def max_index(X): """ i = 0 j = 0 + if not isinstance(X, np.ndarray): + raise ValueError("Input must be a numpy array.") + + if X.ndim != 2: + raise ValueError("Input array must be 2-dimensional.") - # TODO + i, j = np.unravel_index(np.argmax(X), X.shape) return i, j @@ -64,4 +70,14 @@ 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. + + prod = 1.0 + + if n_terms == 0: + return 1.0 + + for i in range(1, n_terms + 1): + prod *= (4 * i**2) / (4 * i**2 - 1) + pi = 2 * prod + + return pi diff --git a/sklearn_questions.py b/sklearn_questions.py index f65038c6..2ae045b2 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,28 +30,49 @@ class OneNearestNeighbor(BaseEstimator, ClassifierMixin): - "OneNearestNeighbor classifier." + """OneNearestNeighbor classifier.""" def __init__(self): # noqa: D107 pass def fit(self, X, y): - """Write docstring. + """Fit the OneNearestNeighbor class. + + Parameters + ---------- + X: ndarray of shape (n_samples, n_features) + Training data. - And describe parameters + y: ndarray of shape (n_samples, n_features) + Target for each training samples. + + Returns + ---------- + self: object + 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 the OneNearestNeighbor class. - And describe parameters + Parameters + ---------- + X: ndarray of shape (n_samples, n_features) + Test data. + + Returns + ---------- + y_pred: ndnarray of shape (n_samples) + Predicted output. """ check_is_fitted(self) X = check_array(X) @@ -59,16 +81,30 @@ def predict(self, X): dtype=self.classes_.dtype ) - # XXX fix + for i, x_test in enumerate(X): + distances = np.sqrt(np.sum((self.X_ - x_test) ** 2, axis=1)) + nearest = np.argmin(distances) + y_pred[i] = self.y_[nearest] return y_pred def score(self, X, y): - """Write docstring. + """Comptute the accuracy of the OneNearestNeighbor class. + + Parameters + ---------- + X: ndarray of shape (n_samples, n_features) + Test data. + + y: ndarray of shape (n_samples, n_features) + Train data. - And describe parameters + Returns + ---------- + accuracy: float + Mean accuracy of prediction. """ X, y = check_X_y(X, y) y_pred = self.predict(X) - # XXX fix - return y_pred.sum() + accuracy = np.mean(y_pred == y) + return accuracy