diff --git a/numpy_questions.py b/numpy_questions.py index 21fcec4b..7beb5367 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -42,6 +42,14 @@ def max_index(X): # TODO + if not isinstance(X, np.ndarray): + raise ValueError + if X.ndim != 2: + raise ValueError + + index = np.argmax(X) + i, j = np.unravel_index(index, X.shape) + return i, j @@ -64,4 +72,12 @@ 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. + + if n_terms == 0: + return 1 + wp = 1 + + for n in range(1, n_terms+1): + wp = wp * ((4 * n**2) / (4 * n**2 - 1)) + pi = wp * 2 + return pi diff --git a/sklearn_questions.py b/sklearn_questions.py index f65038c6..192ae662 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -23,34 +23,60 @@ 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.multiclass import check_classification_targets +from sklearn.metrics import pairwise_distances_argmin_min +from sklearn.utils.validation import check_array -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. + + This function stores training data X and the labels y. + + Parameters + ---------- + X : Training data (n_samples, n_features). + + Y : Target labels (n_samples). + + Returns + ------- + self : returns the fitted classifier. - And describe parameters + Raises + ------ + ValueError + If X and y have different numbers of samples. """ 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_ = np.asarray(X) + self.y_ = np.asarray(y) + return self def predict(self, X): - """Write docstring. + """Return the predicted class for a data set in an numpy array. + + Parameters + ---------- + X : ndarray of shape (n_samples, n_features) + The input array. - And describe parameters + Returns + ------- + y_pred : ndarray of shape (n_samples) + The predicted classes for the n_samples. """ check_is_fitted(self) X = check_array(X) @@ -59,16 +85,29 @@ def predict(self, X): dtype=self.classes_.dtype ) - # XXX fix + argmin, _ = pairwise_distances_argmin_min(X, self.X_) + y_pred = self.y_[argmin] + return y_pred def score(self, X, y): - """Write docstring. - - And describe parameters + """Return the score of the OneNearestNeighbor on a data set. + + Parameters + ---------- + X : ndarray of shape (n_samples, n_features) + The input array. + y : ndarray of shape (n_samples) + The true classes of the samples. + + Returns + ------- + score : float + The percentage of samples accurately predicted. """ X, y = check_X_y(X, y) y_pred = self.predict(X) - # XXX fix - return y_pred.sum() + score = np.mean(y_pred == y) + + return score