diff --git a/numpy_questions.py b/numpy_questions.py index 21fcec4b..d17d5a5a 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -15,6 +15,8 @@ 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,9 +41,12 @@ def max_index(X): """ i = 0 j = 0 - - # TODO - + if not isinstance(X, np.ndarray): + raise ValueError("Input must be a numpy array") + if len(X.shape) != 2: + raise ValueError("Input must be a 2D array") + id = np.argmax(X) + i, j = np.unravel_index(id, X.shape) return i, j @@ -62,6 +67,9 @@ 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 + pi = 1 + for i in range(1, n_terms + 1): + pi = pi * (4 * i**2) / (4 * i**2 - 1) + return pi * 2 diff --git a/sklearn_questions.py b/sklearn_questions.py index f65038c6..eae45ad9 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -28,29 +28,52 @@ 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. - - And describe parameters + """ + Fit the OneNearestNeighbor classifier according to X, y. + + Parameters + ---------- + X : ndarray of shape (n_samples, n_features) + Training data, with n_samples the number of samples and + n_features the number of features. + y : ndarray of shape (n_samples,) + Target data, with n_samples the number of 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 labels based on X with the NearestNeighbor Estimator. + + Parameters + ---------- + X : ndarray of shape (n_samples, n_features) + Data to predict, with n_samples the number of samples and + n_features the number of features. + + Returns + ------- + y_pred : ndarray of shape (n_samples,) with the predicted values. + Predicted values for X. - And describe parameters """ check_is_fitted(self) X = check_array(X) @@ -58,17 +81,28 @@ def predict(self, X): shape=len(X), fill_value=self.classes_[0], dtype=self.classes_.dtype ) - - # XXX fix + for i in range(len(X)): + d = np.linalg.norm(self.X_ - X[i, :], axis=1) + nearest_index = d.argmin() + y_pred[i] = self.y_[nearest_index] return y_pred def score(self, X, y): - """Write docstring. - - And describe parameters + """ + Score the prediction with the predict function. + + Parameters + ---------- + X : ndarray of shape (n_sample, n_features) + Data to predict. + y : ndarray of shape (n_sample, ) + Targeted data. + Returns + ------- + score : float + Mean accuracy of the model on the X, y dataset. """ X, y = check_X_y(X, y) y_pred = self.predict(X) - - # XXX fix - return y_pred.sum() + y_pred = (y_pred == y) + return y_pred.sum()/len(y_pred) diff --git a/students.txt b/students.txt index c3f2c93e..e072b973 100644 --- a/students.txt +++ b/students.txt @@ -61,7 +61,7 @@ Liu Guangyue Liu Yunxian Lucille Maximilien X Mahé Blanche -Martin Justin X +Martin Justin X Massias Mathurin Massoud Alexandre.....X Mayette Scott