diff --git a/numpy_questions.py b/numpy_questions.py index 21fcec4b..953f1c45 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -37,10 +37,13 @@ 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 should be a numpy array.") + if X.ndim != 2: + raise ValueError("Input should be a 2D numpy array.") - # TODO + index = np.argmax(X) + i, j = np.unravel_index(index, X.shape) return i, j @@ -62,6 +65,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.0 + 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..bc905462 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -23,34 +23,49 @@ 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_array 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 model to the given training data. + + Parameters + ---------- + X : ndarray of shape (n_samples, n_features) + y : ndarray of shape (n_samples,) - And describe parameters + Returns + ------- + self : object + The fitted model """ 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 for the given training data. + + Parameters + ---------- + X : ndarray of shape (n_samples, n_features) - And describe parameters + Returns + ------- + y_pred : ndarray of shape (n_samples,) + The predicted labels. """ check_is_fitted(self) X = check_array(X) @@ -59,16 +74,27 @@ def predict(self, X): dtype=self.classes_.dtype ) - # XXX fix + for i in range(len(X)): + distances = np.linalg.norm(self.X_ - X[i, :], axis=1) + nearest_index = distances.argmin() + y_pred[i] = self.y_[nearest_index] return y_pred def score(self, X, y): - """Write docstring. + """Return the number of correct label predictions on the given data. + + Parameters + ---------- + X : ndarray of shape (n_samples, n_features) + y : ndarray of shape (n_samples,) - And describe parameters + Returns + ------- + score : float + The number of correct predictions. """ X, y = check_X_y(X, y) y_pred = self.predict(X) + y_pred = (y_pred == y) - # XXX fix - return y_pred.sum() + return y_pred.sum()/len(y_pred)