From 3bdf1af4db93c81df05a240213be369dfbc6508f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Combe?= Date: Tue, 10 Nov 2026 18:50:06 +0100 Subject: [PATCH 01/20] Completed questions --- numpy_questions.py | 18 +++++++++----- sklearn_questions.py | 58 ++++++++++++++++++++++++++++++++++++-------- 2 files changed, 60 insertions(+), 16 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index 21fcec4b..be23e982 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -37,10 +37,10 @@ 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) or X.ndim != 2: + raise ValueError - # TODO + i, j = np.unravel_index(np.argmax(X), X.shape) return i, j @@ -62,6 +62,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. + i = np.arrange(1, n_terms + 1) + + W = np.prod((4 * i ** 2) / (4 * i ** 2 - 1)) + + pi = 2 * W + + return W + + diff --git a/sklearn_questions.py b/sklearn_questions.py index f65038c6..42eb9e62 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -35,22 +35,44 @@ def __init__(self): # noqa: D107 pass def fit(self, X, y): - """Write docstring. - - And describe parameters + + """Fit the nearest neighbor classifier. + + Parameters + ---------- + X : array-like of shape (n_samples, n_features) + Training samples. + y : array-like of shape (n_samples,) + Target class labels. + + Returns + ------- + self : OneNearestNeighbor + 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 class labels for samples in X. - And describe parameters + Parameters + ---------- + X : array-like of shape (n_samples, n_features) + Samples to classify. + + Returns + ------- + y_pred : ndarray of shape (n_samples,) + Predicted class labels. """ check_is_fitted(self) X = check_array(X) @@ -59,16 +81,32 @@ def predict(self, X): dtype=self.classes_.dtype ) - # XXX fix + distances = np.sqrt(((X[:, np.newaxis, :] - + self.X_train_[np.newaxis, :, :]) ** 2).sum(axis=2)) + + nearest_indices = np.argmin(distances, axis=1) + + y_pred = self.y_train_[nearest_indices] + return y_pred def score(self, X, y): - """Write docstring. + """Return the mean accuracy on the given test data. + + Parameters + ---------- + X : array-like of shape (n_samples, n_features) + Test samples. + + y : array-like of shape (n_samples,) + True class 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() From 02332d9d7390c18476e46c5141377b67833ee622 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Combe?= Date: Tue, 10 Nov 2026 18:56:24 +0100 Subject: [PATCH 02/20] Fix code style issues --- numpy_questions.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index be23e982..92790db0 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -68,6 +68,4 @@ def wallis_product(n_terms): pi = 2 * W - return W - - + return pi From 0ee27a793e9a12fb6fafb85896168f62448f1599 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Combe?= Date: Tue, 10 Nov 2026 18:59:39 +0100 Subject: [PATCH 03/20] Fix numpy code style --- numpy_questions.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index 92790db0..f6963045 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -64,8 +64,8 @@ def wallis_product(n_terms): """ i = np.arrange(1, n_terms + 1) - W = np.prod((4 * i ** 2) / (4 * i ** 2 - 1)) + pi = np.prod((4 * i ** 2) / (4 * i ** 2 - 1)) - pi = 2 * W + pi = 2 * pi - return pi + return pi \ No newline at end of file From ad4b695d3d16dea899fcd4b96c358cf7f590be02 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Combe?= Date: Tue, 10 Nov 2026 19:09:12 +0100 Subject: [PATCH 04/20] Fixes --- numpy_questions.py | 8 +++----- sklearn_questions.py | 3 +++ 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index f6963045..7efe92d3 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -62,10 +62,8 @@ def wallis_product(n_terms): pi : float The approximation of order `n_terms` of pi using the Wallis product. """ - i = np.arrange(1, n_terms + 1) + i = np.arange(1, n_terms + 1) - pi = np.prod((4 * i ** 2) / (4 * i ** 2 - 1)) + pi_approx = 2 * np.prod((4 * i ** 2) / (4 * i ** 2 - 1)) - pi = 2 * pi - - return pi \ No newline at end of file + return pi_approx diff --git a/sklearn_questions.py b/sklearn_questions.py index 42eb9e62..29e48dc9 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -76,6 +76,9 @@ def predict(self, X): """ check_is_fitted(self) X = check_array(X) + if X.shape[1] != self.n_features_in_: + raise ValueError + y_pred = np.full( shape=len(X), fill_value=self.classes_[0], dtype=self.classes_.dtype From c0c4afe835ae30300669111aad57cfffc28eb85b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Combe?= Date: Tue, 10 Nov 2026 19:20:22 +0100 Subject: [PATCH 05/20] fixes --- numpy_questions.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/numpy_questions.py b/numpy_questions.py index 7efe92d3..17ae3042 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -62,6 +62,9 @@ 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 + i = np.arange(1, n_terms + 1) pi_approx = 2 * np.prod((4 * i ** 2) / (4 * i ** 2 - 1)) From 8ff74fe7c8e7e5bf052480483899bc0c01be2068 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Combe?= Date: Tue, 10 Nov 2026 19:27:51 +0100 Subject: [PATCH 06/20] Fixes --- numpy_questions.py | 2 +- sklearn_questions.py | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index 17ae3042..e770a477 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -64,7 +64,7 @@ def wallis_product(n_terms): """ if n_terms == 0: return 1 - + i = np.arange(1, n_terms + 1) pi_approx = 2 * np.prod((4 * i ** 2) / (4 * i ** 2 - 1)) diff --git a/sklearn_questions.py b/sklearn_questions.py index 29e48dc9..43fa5b1c 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -77,7 +77,11 @@ def predict(self, X): check_is_fitted(self) X = check_array(X) if X.shape[1] != self.n_features_in_: - raise ValueError + raise ValueError( + f"X has {X.shape[1]} features, but " + f"OneNearestNeighbor is expecting " + f"{self.n_features_in_} features as input" + ) y_pred = np.full( shape=len(X), fill_value=self.classes_[0], @@ -112,4 +116,4 @@ def score(self, X, y): X, y = check_X_y(X, y) y_pred = self.predict(X) - return y_pred.sum() + return np.mean(y_pred == y) \ No newline at end of file From 84703080278fbe135db7567e18226ce0a6127fce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Combe?= Date: Tue, 10 Nov 2026 19:36:23 +0100 Subject: [PATCH 07/20] Final fixes --- sklearn_questions.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/sklearn_questions.py b/sklearn_questions.py index 43fa5b1c..69878167 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -35,7 +35,7 @@ def __init__(self): # noqa: D107 pass def fit(self, X, y): - + """Fit the nearest neighbor classifier. Parameters @@ -47,7 +47,7 @@ def fit(self, X, y): Returns ------- - self : OneNearestNeighbor + self : OneNearestNeighbor Fitted classifier. """ @@ -82,14 +82,14 @@ def predict(self, X): f"OneNearestNeighbor is expecting " f"{self.n_features_in_} features as input" ) - + y_pred = np.full( shape=len(X), fill_value=self.classes_[0], dtype=self.classes_.dtype ) distances = np.sqrt(((X[:, np.newaxis, :] - - self.X_train_[np.newaxis, :, :]) ** 2).sum(axis=2)) + self.X_train_[np.newaxis, :, :]) ** 2).sum(axis=2)) nearest_indices = np.argmin(distances, axis=1) @@ -116,4 +116,4 @@ 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 c5df57703dc5c781b388ccc8d8715db3ba762abf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Combe?= Date: Tue, 10 Nov 2026 19:41:38 +0100 Subject: [PATCH 08/20] Fixes sklearn --- sklearn_questions.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/sklearn_questions.py b/sklearn_questions.py index 69878167..1bceaa8e 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -88,8 +88,7 @@ def predict(self, X): dtype=self.classes_.dtype ) - distances = np.sqrt(((X[:, np.newaxis, :] - - self.X_train_[np.newaxis, :, :]) ** 2).sum(axis=2)) + distances = np.sqrt(((X[:, np.newaxis, :] - self.X_train_[np.newaxis, :, :]) ** 2).sum(axis=2)) nearest_indices = np.argmin(distances, axis=1) From 127cc6baccdac2eea76deaf3820539eee56ae624 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Combe?= Date: Tue, 10 Nov 2026 19:44:59 +0100 Subject: [PATCH 09/20] fixes --- sklearn_questions.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sklearn_questions.py b/sklearn_questions.py index 1bceaa8e..87fff03e 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -88,7 +88,8 @@ def predict(self, X): dtype=self.classes_.dtype ) - distances = np.sqrt(((X[:, np.newaxis, :] - self.X_train_[np.newaxis, :, :]) ** 2).sum(axis=2)) + distances = np.sqrt(((X[:, np.newaxis, :] + - self.X_train_[np.newaxis, :, :]) ** 2).sum(axis=2)) nearest_indices = np.argmin(distances, axis=1) From 02deeb428d601c544477ac905016c2ce552968e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Combe?= Date: Tue, 10 Nov 2026 19:47:11 +0100 Subject: [PATCH 10/20] fixes --- sklearn_questions.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/sklearn_questions.py b/sklearn_questions.py index 87fff03e..19c3bfef 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -88,8 +88,9 @@ def predict(self, X): dtype=self.classes_.dtype ) - distances = np.sqrt(((X[:, np.newaxis, :] - - self.X_train_[np.newaxis, :, :]) ** 2).sum(axis=2)) + distances = np.sqrt(((X[:, np.newaxis, :] + - self.X_train_[np.newaxis, :, :]) ** 2) + .sum(axis=2)) nearest_indices = np.argmin(distances, axis=1) From 52465d8f9d4683392625ab537fe06a7742e02152 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Combe?= Date: Tue, 10 Nov 2026 19:50:11 +0100 Subject: [PATCH 11/20] fixes --- sklearn_questions.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sklearn_questions.py b/sklearn_questions.py index 19c3bfef..86bd59bc 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -89,8 +89,8 @@ def predict(self, X): ) distances = np.sqrt(((X[:, np.newaxis, :] - - self.X_train_[np.newaxis, :, :]) ** 2) - .sum(axis=2)) + - self.X_train_[np.newaxis, :, :]) ** 2) + .sum(axis=2)) nearest_indices = np.argmin(distances, axis=1) From e9413fc5fac245a74ddedfd23d927bdb8a6e0418 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Combe?= Date: Tue, 10 Nov 2026 19:56:26 +0100 Subject: [PATCH 12/20] final --- sklearn_questions.py | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/sklearn_questions.py b/sklearn_questions.py index 86bd59bc..909ae1e6 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -22,8 +22,7 @@ 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 validate_data from sklearn.utils.validation import check_is_fitted from sklearn.utils.multiclass import check_classification_targets @@ -51,7 +50,7 @@ def fit(self, X, y): Fitted classifier. """ - X, y = check_X_y(X, y) + X, y = validate_data(self, X, y) check_classification_targets(y) self.classes_ = np.unique(y) self.n_features_in_ = X.shape[1] @@ -75,13 +74,7 @@ def predict(self, X): Predicted class 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 " - f"OneNearestNeighbor is expecting " - f"{self.n_features_in_} features as input" - ) + X = validate_data(self, X, reset=False) y_pred = np.full( shape=len(X), fill_value=self.classes_[0], From e0d6bc164d18cbbfd9967d64fa982ee6f208550c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Combe?= Date: Tue, 10 Nov 2026 19:59:00 +0100 Subject: [PATCH 13/20] Final --- sklearn_questions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sklearn_questions.py b/sklearn_questions.py index 909ae1e6..7b7935c0 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -107,7 +107,7 @@ def score(self, X, y): score : float Mean accuracy of the classifier. """ - X, y = check_X_y(X, y) + X, y = validate_data(self, X, y, reset=False) y_pred = self.predict(X) return np.mean(y_pred == y) From dd7a1bafad05284d721845662f905ec2c62ae3e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Combe?= Date: Tue, 10 Nov 2026 20:03:28 +0100 Subject: [PATCH 14/20] f811 solved --- sklearn_questions.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sklearn_questions.py b/sklearn_questions.py index 7b7935c0..b5d7b0cf 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -23,6 +23,7 @@ from sklearn.base import BaseEstimator from sklearn.base import ClassifierMixin from sklearn.utils.validation import validate_data +from sklearn.utils.validation import check_X_y from sklearn.utils.validation import check_is_fitted from sklearn.utils.multiclass import check_classification_targets @@ -107,7 +108,7 @@ def score(self, X, y): score : float Mean accuracy of the classifier. """ - X, y = validate_data(self, X, y, reset=False) + X, y = check_X_y(X, y) y_pred = self.predict(X) return np.mean(y_pred == y) From 6fc19c5e642f925d1e8ae38d1901f5d7d2181cb3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Combe?= Date: Tue, 10 Nov 2026 20:06:39 +0100 Subject: [PATCH 15/20] f811 --- sklearn_questions.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/sklearn_questions.py b/sklearn_questions.py index b5d7b0cf..b0b554eb 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -22,10 +22,9 @@ import numpy as np from sklearn.base import BaseEstimator from sklearn.base import ClassifierMixin -from sklearn.utils.validation import validate_data -from sklearn.utils.validation import check_X_y from sklearn.utils.validation import check_is_fitted from sklearn.utils.multiclass import check_classification_targets +from sklearn.utils.validation import validate_data class OneNearestNeighbor(BaseEstimator, ClassifierMixin): @@ -108,7 +107,7 @@ def score(self, X, y): score : float Mean accuracy of the classifier. """ - X, y = check_X_y(X, y) + X, y = validate_data(self, X, y, reset=False) y_pred = self.predict(X) return np.mean(y_pred == y) From 9e6c85e0c3340d1fece95360d789f0305bc4fc87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Combe?= Date: Tue, 10 Nov 2026 20:09:09 +0100 Subject: [PATCH 16/20] f811 --- sklearn_questions.py | 1 - 1 file changed, 1 deletion(-) diff --git a/sklearn_questions.py b/sklearn_questions.py index b0b554eb..e8366094 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -24,7 +24,6 @@ from sklearn.base import ClassifierMixin from sklearn.utils.validation import check_is_fitted from sklearn.utils.multiclass import check_classification_targets -from sklearn.utils.validation import validate_data class OneNearestNeighbor(BaseEstimator, ClassifierMixin): From f85d8c865c013d3cca9ffc47a4ad31e3bcc84d45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Combe?= Date: Tue, 10 Nov 2026 20:12:58 +0100 Subject: [PATCH 17/20] formating --- sklearn_questions.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/sklearn_questions.py b/sklearn_questions.py index e8366094..5f92db00 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -27,13 +27,12 @@ class OneNearestNeighbor(BaseEstimator, ClassifierMixin): - "OneNearestNeighbor classifier." + """OneNearestNeighbor classifier.""" def __init__(self): # noqa: D107 pass def fit(self, X, y): - """Fit the nearest neighbor classifier. Parameters @@ -48,7 +47,6 @@ def fit(self, X, y): self : OneNearestNeighbor Fitted classifier. """ - X, y = validate_data(self, X, y) check_classification_targets(y) self.classes_ = np.unique(y) From 24c7bff4bf65c17dd55e6661bf6a74316aadf9d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Combe?= Date: Tue, 10 Nov 2026 20:17:13 +0100 Subject: [PATCH 18/20] inheritance --- sklearn_questions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sklearn_questions.py b/sklearn_questions.py index 5f92db00..f5448179 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -26,7 +26,7 @@ from sklearn.utils.multiclass import check_classification_targets -class OneNearestNeighbor(BaseEstimator, ClassifierMixin): +class OneNearestNeighbor(ClassifierMixin, BaseEstimator): """OneNearestNeighbor classifier.""" def __init__(self): # noqa: D107 From 6be539873272a47d049f1500c6233084ddee02b9 Mon Sep 17 00:00:00 2001 From: clementcombe Date: Wed, 11 Nov 2026 16:11:25 +0100 Subject: [PATCH 19/20] Reversed unintended changes --- students26.txt | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/students26.txt b/students26.txt index db3b624a..2b91705a 100644 --- a/students26.txt +++ b/students26.txt @@ -8,7 +8,7 @@ Clément Bayle Niccolo Benetti Omar Benharbit X Mattis Berenthal -Tiphaine Berthou +Tiphaine Berthou x Arthur Bertrand X Noé Besson X Gauthier Boyen X @@ -49,12 +49,12 @@ Cyprien Kutek X Guillaume Leopold Léonard Levy Lucas Lonegro Gurfinkel X -Elisa Marlow +Elisa Marlow X Mathurin Massias Martin Michaut X Jana Naaman Rita Najeddine -Abdelilah Nossair +Abdelilah Nossair X Brendan O'Sullivan X Joseph Paillard Louis PetitdidierX @@ -73,7 +73,7 @@ Nathan Serfaty Shreyas Sridharan Rithika Susan Abraham Alessandro Tavolato X -Francesco Tavolato +Francesco Tavolato X Lise Teyssier X Luigi Tintinaglia Maurice Van Den Boogaerde X @@ -90,7 +90,7 @@ Hao Ying Ossama Zaroual Xinle Zhang X Yihan Zhang X -Xiaoyu Zhao +Xiaoyu Zhao X Xiao Zhou X Yilin Zhou Jia Zhu X From 48bd288fc4a97ea686cd5d66f24dabbff266cf25 Mon Sep 17 00:00:00 2001 From: clementcombe Date: Wed, 11 Nov 2026 16:12:15 +0100 Subject: [PATCH 20/20] Correct capitalization of 'X' for Tiphaine Berthou --- students26.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/students26.txt b/students26.txt index 2b91705a..16d25691 100644 --- a/students26.txt +++ b/students26.txt @@ -8,7 +8,7 @@ Clément Bayle Niccolo Benetti Omar Benharbit X Mattis Berenthal -Tiphaine Berthou x +Tiphaine Berthou X Arthur Bertrand X Noé Besson X Gauthier Boyen X