diff --git a/numpy_questions.py b/numpy_questions.py index 21fcec4b..1e702be6 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -37,11 +37,11 @@ def max_index(X): If the input is not a numpy array or if the shape is not 2D. """ - i = 0 - j = 0 - - # TODO - + 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.") + i, j = np.unravel_index(np.argmax(X, axis=None), X.shape) return i, j @@ -62,6 +62,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.0 + approx_pi = 2.0 + for n in range(1, n_terms + 1): + approx_pi *= (4 * n**2) / (4 * n**2 - 1) + return approx_pi diff --git a/sklearn_questions.py b/sklearn_questions.py index f65038c6..daa8e1f7 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -29,46 +29,87 @@ class OneNearestNeighbor(BaseEstimator, ClassifierMixin): - "OneNearestNeighbor classifier." + """1-Nearest Neighbor (1-NN) classifier. + + Classifies samples based on the label of their single nearest + training neighbor using Euclidean distance. + """ def __init__(self): # noqa: D107 pass def fit(self, X, y): - """Write docstring. + """Fit the 1-Nearest Neighbor classifier from the training dataset. + + Essentially, this method stores the training data for later use + during prediction. + + Parameters + ---------- + X : array-like of shape (n_samples, n_features) - And describe parameters + y : array-like of shape (n_samples,) + + Returns + ------- + self : object + Returns the fitted estimator instance. """ 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_train_ = X + self.y_train_ = y return self def predict(self, X): - """Write docstring. + """Predict class labels for the input samples. - And describe parameters + Computes the Euclidean distance from each sample in `X` to the + training data and assigns the label of the nearest neighbor. + + Parameters + ---------- + X : array-like of shape (n_samples, n_features) + Input samples. + + Returns + ------- + y_pred : ndarray of shape (n_samples,) + Predicted class labels. """ check_is_fitted(self) X = check_array(X) - y_pred = np.full( - shape=len(X), fill_value=self.classes_[0], - dtype=self.classes_.dtype - ) - # XXX fix - return y_pred + distances = np.sqrt( + np.sum( + (X[:, np.newaxis, :] - self.X_train_[np.newaxis, :, :]) ** 2, + axis=2 + )) - def score(self, X, y): - """Write docstring. + # For each test sample, get the index of the nearest training point + nearest_indices = np.argmin(distances, axis=1) - And describe parameters + # Return the corresponding labels + return self.y_train_[nearest_indices] + + def score(self, X, y): + """Return the mean accuracy on the test data and labels. + + Parameters + ---------- + X : array-like of shape (n_samples, n_features) + Test input samples. + y : array-like of shape (n_samples,) + True class labels. + + Returns + ------- + score : float + Mean accuracy of the classifier (fraction of correct predictions). """ X, y = check_X_y(X, y) y_pred = self.predict(X) - # XXX fix - return y_pred.sum() + return np.mean(y_pred == y)