diff --git a/numpy_questions.py b/numpy_questions.py index 21fcec4b..c6734961 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,12 +37,23 @@ 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 must be a numpy array.") + if X.ndim != 2: + raise ValueError( + "Input must be a 2D array with shape (n_samples, n_features)." + ) - # TODO + current_max = X[0, 0] + max_i, max_j = 0, 0 - return i, j + for i in range(X.shape[0]): + for j in range(X.shape[1]): + if X[i, j] > current_max: + current_max = X[i, j] + max_i, max_j = i, j + + return max_i, max_j def wallis_product(n_terms): @@ -62,6 +73,12 @@ 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 + + product = 1.0 + for i in range(1, n_terms + 1): + term = (4 * i**2) / (4 * i**2 - 1) + product *= term + + return 2 * product diff --git a/sklearn_questions.py b/sklearn_questions.py index f65038c6..dbcc0152 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -28,47 +28,98 @@ 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 classifier. - And describe parameters + This stores the training data so that predictions can be made + by looking for the closest training sample to each new point. + + Parameters + ---------- + X : array-like of shape (n_samples, n_features) + Training input samples. + + y : array-like of shape (n_samples,) + Target labels for each training sample. + + Returns + ------- + self : OneNearestNeighbor + The fitted classifier. """ 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 given samples. - And describe parameters + For each sample in X, the predicted label is the label of the + closest training sample (in Euclidean distance). + + Parameters + ---------- + X : array-like of shape (n_samples, n_features) + Input samples to classify. + + 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 + 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" + ) + + n_samples = X.shape[0] + y_pred = np.empty(n_samples, dtype=self.y_.dtype) + + for i in range(n_samples): + x = X[i] + dists = np.linalg.norm(self.X_ - x, axis=1) + nn_idx = np.argmin(dists) + y_pred[i] = self.y_[nn_idx] + return y_pred def score(self, X, y): - """Write docstring. + """Compute accuracy of the classifier on the given test data. + + The accuracy is the proportion of correctly classified samples. + + Parameters + ---------- + X : array-like of shape (n_samples, n_features) + Test input samples. + + y : array-like of shape (n_samples,) + True labels for X. - And describe parameters + Returns + ------- + score : float + Mean accuracy of predictions on X compared to y. """ X, y = check_X_y(X, y) - y_pred = self.predict(X) + check_is_fitted(self) - # XXX fix - return y_pred.sum() + y_pred = self.predict(X) + return np.mean(y_pred == y)