diff --git a/numpy_questions.py b/numpy_questions.py index 21fcec4b..69d1cc07 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -40,7 +40,12 @@ def max_index(X): i = 0 j = 0 - # TODO + if not isinstance(X, np.ndarray): + raise ValueError("X must be a numpy ndarray.") + if X.ndim != 2: + raise ValueError("X must be a 2D array.") + flat_index = np.argmax(X) + i, j = np.unravel_index(flat_index, X.shape) return i, j @@ -64,4 +69,13 @@ 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: + raise ValueError("The number of terms must be positive") + if n_terms == 0: + return 1.0 + n = np.arange(1, n_terms+1, dtype=float) + terms = 4*n**2/(4*n**2-1) + product = np.prod(terms) + pi_approx = 2*product + return pi_approx diff --git a/sklearn_questions.py b/sklearn_questions.py index f65038c6..d8f893fa 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -20,55 +20,105 @@ `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 -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.base import BaseEstimator, ClassifierMixin +from sklearn.utils.validation import check_X_y, check_is_fitted, check_array from sklearn.utils.multiclass import check_classification_targets -class OneNearestNeighbor(BaseEstimator, ClassifierMixin): - "OneNearestNeighbor classifier." +class OneNearestNeighbor(ClassifierMixin, BaseEstimator): + """One-nearest-neighbor classifier.""" def __init__(self): # noqa: D107 pass def fit(self, X, y): - """Write docstring. - - And describe parameters + """Fit the OneNearestNeighbor classifier. + + This method stores the training data X and y inside the estimator + so that predictions can be made based on the nearest neighbor rule. + + Parameters + ---------- + X : ndarray of shape (n_samples, n_features) + Training data. + y : ndarray of shape (n_samples,) + Target labels corresponding to X. + + 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 class labels for the given samples. + + For each sample in X, this method finds the closest training sample + stored during ``fit`` using the Euclidean distance, and returns its + corresponding label. - And describe parameters + Parameters + ---------- + X : array-like of shape (n_samples, n_features) + Input samples for which to predict class labels. + + Returns + ------- + y_pred : ndarray of shape (n_samples,) + Predicted class labels for each sample in X. """ check_is_fitted(self) X = check_array(X) + + if X.shape[1] != self.n_features_in_: + raise ValueError( + f"X has {X.shape[1]} features, but OneNearestNeighbor " + f"is expecting {self.n_features_in_} features as input." + ) + 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 idx, x_i in enumerate(X): + diff = self.X_ - x_i + distances = np.sqrt(np.sum(diff**2, axis=1)) + nearest = np.argmin(distances) + y_pred[idx] = self.y_[nearest] + return y_pred def score(self, X, y): - """Write docstring. - - And describe parameters + """Compute the accuracy of the classifier. + + This method compares the predicted labels for X with the true labels y + and returns the proportion of correctly classified samples. + + Parameters + ---------- + X : ndarray of shape (n_samples, n_features) + Test samples. + y : ndarray of shape (n_samples,) + True labels for X. + + Returns + ------- + accuracy : float + Mean accuracy of the classifier on the given test data. """ X, y = check_X_y(X, y) y_pred = self.predict(X) - - # XXX fix - return y_pred.sum() + correct = y_pred == y + accuracy = np.mean(correct) + return accuracy