diff --git a/numpy_questions.py b/numpy_questions.py index 21fcec4b..c15db9c5 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -29,7 +29,7 @@ def max_index(X): Returns ------- (i, j) : tuple(int) - The row and columnd index of the maximum. + The row and column index of the maximum. Raises ------ @@ -37,31 +37,37 @@ 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): # check if X is a numpy array + raise ValueError("Input must be a numpy array") + if X.ndim != 2: # check if X is 2D + raise ValueError("Input must be a 2D array") - # TODO + flat_index = np.argmax(X) # index of max value + i, j = np.unravel_index(flat_index, X.shape) return i, j def wallis_product(n_terms): - """Implement the Wallis product to compute an approximation of pi. - - See: - https://en.wikipedia.org/wiki/Wallis_product + """Compute an approximation of pi using the Wallis product. Parameters ---------- n_terms : int - Number of steps in the Wallis product. Note that `n_terms=0` will - consider the product to be `1`. + Number of terms in the product. If `n_terms=0`, the result is 1. Returns ------- pi : float - The approximation of order `n_terms` of pi using the Wallis product. + Approximation of pi of order `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.0 + + product = 1.0 + for k in range(1, n_terms + 1): + numerator = 4 * k * k + term = numerator / (numerator - 1) + product *= term + + return 2 * product diff --git a/sklearn_questions.py b/sklearn_questions.py index f65038c6..b127f3c3 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -28,47 +28,100 @@ from sklearn.utils.multiclass import check_classification_targets -class OneNearestNeighbor(BaseEstimator, ClassifierMixin): - "OneNearestNeighbor classifier." +class OneNearestNeighbor(ClassifierMixin, BaseEstimator): + """OneNearestNeighbor classifier. + + This classifier predicts the label of each + sample based on the label of the closest + training sample using Euclidean distance. + """ def __init__(self): # noqa: D107 pass def fit(self, X, y): - """Write docstring. + """Fit the OneNearestNeighbor classifier. + + Parameters + ---------- + X : array-like of shape (n_samples, n_features) + Training data. + + y : array-like of shape (n_samples,) + Target labels. - And describe parameters + Returns + ------- + self : object + Fitted estimator. """ X, y = check_X_y(X, y) check_classification_targets(y) + + self.X_ = X + self.y_ = y self.classes_ = np.unique(y) self.n_features_in_ = X.shape[1] - # XXX fix return self def predict(self, X): - """Write docstring. + """Predict class labels for the samples in X. + + Parameters + ---------- + X : array-like of shape (n_samples, n_features) + Test samples. - And describe parameters + 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 + # manual sklearn-required feature consistency check + if X.shape[1] != self.n_features_in_: + raise ValueError( + f"X has {X.shape[1]} features, but " + f"{self.__class__.__name__} is expecting " + f"{self.n_features_in_} features as input" + ) + y_pred = np.empty(X.shape[0], dtype=self.y_.dtype) + + for i, x in enumerate(X): + distances = np.linalg.norm(self.X_ - x, axis=1) + nearest_idx = np.argmin(distances) + y_pred[i] = self.y_[nearest_idx] + return y_pred def score(self, X, y): - """Write docstring. + """Compute the accuracy of the classifier. + + Parameters + ---------- + X : array-like of shape (n_samples, n_features) + Test samples. + + y : array-like of shape (n_samples,) + True labels. - And describe parameters + Returns + ------- + score : float + Mean accuracy of the classifier. """ X, y = check_X_y(X, y) - y_pred = self.predict(X) - # XXX fix - return y_pred.sum() + # same feature count check + if X.shape[1] != self.n_features_in_: + raise ValueError( + f"X has {X.shape[1]} features, but " + f"{self.__class__.__name__} is expecting " + f"{self.n_features_in_} features as input" + ) + + y_pred = self.predict(X) + return np.mean(y_pred == y)