From 4c4a3a0cc10516692e359c007762b624df21a5c4 Mon Sep 17 00:00:00 2001 From: Louis TONNOM Date: Tue, 10 Nov 2026 14:33:40 +0100 Subject: [PATCH 1/6] Questions --- numpy_questions.py | 14 +++++++--- sklearn_questions.py | 66 +++++++++++++++++++++++++++++++++++--------- 2 files changed, 63 insertions(+), 17 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index 21fcec4b..22e0b0b0 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -40,7 +40,13 @@ def max_index(X): i = 0 j = 0 - # TODO + if not isinstance(X, np.ndarray): + raise ValueError("X must be a numpy array.") + if X.ndim != 2: + raise ValueError("X must be a 2D array.") + + i, j = np.unravel_index(np.argmax(X), X.shape) + i, j = int(i), int(j) return i, j @@ -62,6 +68,6 @@ 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. + k = np.arange(1, n_terms + 1) + product = np.prod(4 * k**2 / (4 * k**2 - 1)) + return 2 * product diff --git a/sklearn_questions.py b/sklearn_questions.py index f65038c6..7b52b358 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -29,46 +29,86 @@ class OneNearestNeighbor(BaseEstimator, ClassifierMixin): - "OneNearestNeighbor classifier." + """OneNearestNeighbor classifier. + + Predict the label of each sample as the label of the closest training + sample, using the Euclidean distance. + """ def __init__(self): # noqa: D107 pass def fit(self, X, y): - """Write docstring. - - And describe parameters + """Fit the classifier on the training data. + + Parameters + ---------- + X : ndarray of shape (n_samples, n_features) + The training samples. + y : ndarray of shape (n_samples,) + The training labels. + + Returns + ------- + self : OneNearestNeighbor + The fitted classifier. """ 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 the labels of the input samples. + + Parameters + ---------- + X : ndarray of shape (n_samples, n_features) + The samples to classify. - And describe parameters + Returns + ------- + y_pred : ndarray of shape (n_samples,) + The predicted labels. """ 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 ) - # XXX fix + distances = np.linalg.norm( + X[:, np.newaxis, :] - self.X_train_[np.newaxis, :, :], axis=2 + ) + y_pred[:] = self.y_train_[np.argmin(distances, axis=1)] return y_pred def score(self, X, y): - """Write docstring. - - And describe parameters + """Compute the accuracy of the classifier. + + Parameters + ---------- + X : ndarray of shape (n_samples, n_features) + The test samples. + y : ndarray of shape (n_samples,) + The true labels. + + Returns + ------- + score : float + The fraction of correctly classified samples. """ X, y = check_X_y(X, y) y_pred = self.predict(X) - # XXX fix - return y_pred.sum() + return np.mean(y_pred == y) \ No newline at end of file From 7ea5ca6be2c54c8bfa91e4070ae3fa4b745c3d5a Mon Sep 17 00:00:00 2001 From: Louis TONNOM Date: Tue, 10 Nov 2026 14:50:30 +0100 Subject: [PATCH 2/6] fixed questions --- numpy_questions.py | 2 ++ sklearn_questions.py | 3 ++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/numpy_questions.py b/numpy_questions.py index 22e0b0b0..df43cdb2 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -71,3 +71,5 @@ def wallis_product(n_terms): k = np.arange(1, n_terms + 1) product = np.prod(4 * k**2 / (4 * k**2 - 1)) return 2 * product + + diff --git a/sklearn_questions.py b/sklearn_questions.py index 7b52b358..cc5c674e 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -111,4 +111,5 @@ def score(self, X, y): X, y = check_X_y(X, y) y_pred = self.predict(X) - return np.mean(y_pred == y) \ No newline at end of file + return np.mean(y_pred == y) + From 5bdc77f9b8300ac4cf887cb9495d791a490fbee4 Mon Sep 17 00:00:00 2001 From: Louis TONNOM Date: Tue, 10 Nov 2026 14:59:22 +0100 Subject: [PATCH 3/6] Fixed --- numpy_questions.py | 2 -- sklearn_questions.py | 1 + 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index df43cdb2..22e0b0b0 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -71,5 +71,3 @@ def wallis_product(n_terms): k = np.arange(1, n_terms + 1) product = np.prod(4 * k**2 / (4 * k**2 - 1)) return 2 * product - - diff --git a/sklearn_questions.py b/sklearn_questions.py index cc5c674e..1b994823 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -113,3 +113,4 @@ def score(self, X, y): return np.mean(y_pred == y) + From a6063c3155a86afbc7ae1ebe93c18e6fc6b8f0ba Mon Sep 17 00:00:00 2001 From: Louis TONNOM Date: Tue, 10 Nov 2026 15:02:15 +0100 Subject: [PATCH 4/6] a --- numpy_questions.py | 1 + sklearn_questions.py | 1 + 2 files changed, 2 insertions(+) diff --git a/numpy_questions.py b/numpy_questions.py index 22e0b0b0..fcaee5c5 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -71,3 +71,4 @@ def wallis_product(n_terms): k = np.arange(1, n_terms + 1) product = np.prod(4 * k**2 / (4 * k**2 - 1)) return 2 * product + diff --git a/sklearn_questions.py b/sklearn_questions.py index 1b994823..5c789bd7 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -112,5 +112,6 @@ def score(self, X, y): y_pred = self.predict(X) return np.mean(y_pred == y) + From 4a21cf2fb199f423df009e99743eb939114df258 Mon Sep 17 00:00:00 2001 From: Louis TONNOM Date: Tue, 10 Nov 2026 15:09:42 +0100 Subject: [PATCH 5/6] ll --- numpy_questions.py | 2 ++ sklearn_questions.py | 4 +--- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index fcaee5c5..4d0eebf0 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -68,6 +68,8 @@ def wallis_product(n_terms): pi : float The approximation of order `n_terms` of pi using the Wallis product. """ + if n_terms == 0: + return 1. k = np.arange(1, n_terms + 1) product = np.prod(4 * k**2 / (4 * k**2 - 1)) return 2 * product diff --git a/sklearn_questions.py b/sklearn_questions.py index 5c789bd7..e5bdb105 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -28,7 +28,7 @@ from sklearn.utils.multiclass import check_classification_targets -class OneNearestNeighbor(BaseEstimator, ClassifierMixin): +class OneNearestNeighbor(ClassifierMixin, BaseEstimator): """OneNearestNeighbor classifier. Predict the label of each sample as the label of the closest training @@ -113,5 +113,3 @@ def score(self, X, y): return np.mean(y_pred == y) - - From 7fac759142123691d7b2e13a2b2258bbb9fa91f3 Mon Sep 17 00:00:00 2001 From: Louis TONNOM Date: Tue, 10 Nov 2026 15:12:56 +0100 Subject: [PATCH 6/6] ll --- numpy_questions.py | 1 - sklearn_questions.py | 1 - 2 files changed, 2 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index 4d0eebf0..5c2b3bcf 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -73,4 +73,3 @@ def wallis_product(n_terms): k = np.arange(1, n_terms + 1) product = np.prod(4 * k**2 / (4 * k**2 - 1)) return 2 * product - diff --git a/sklearn_questions.py b/sklearn_questions.py index e5bdb105..4ab22b54 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -112,4 +112,3 @@ def score(self, X, y): y_pred = self.predict(X) return np.mean(y_pred == y) -