From a60c34c65119b93c17581aab145e0a1f9e83c8a4 Mon Sep 17 00:00:00 2001 From: JFxhec Date: Thu, 13 Nov 2025 18:05:20 +0100 Subject: [PATCH 1/9] fichiers modifies --- numpy_questions.py | 21 +++++++-- sklearn_questions.py | 103 +++++++++++++++++++++++++++---------------- 2 files changed, 83 insertions(+), 41 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index 21fcec4b..5d1019ad 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -37,10 +37,13 @@ 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(f"Input array must be 2D, not {X.ndim} dimensions.") - # TODO + index = np.argmax(X) + (i, j) = np.unravel_index(index, X.shape) return i, j @@ -64,4 +67,14 @@ def wallis_product(n_terms): """ # XXX : The n_terms is an int that corresponds to the number of # terms in the product. For example 10000. - return 0. + + pi_by_2 = 1.0 + + for n in range(1, n_terms + 1): + # Use spaces around operators like ** for PEP 8 compliance + num = 4.0 * n ** 2 + den = num - 1.0 + + # Corrected variable name from pi_over_2 to pi_by_2 + pi_by_2 *= (num / den) + return 2.0 * pi_by_2 diff --git a/sklearn_questions.py b/sklearn_questions.py index f65038c6..c9879c8f 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -1,23 +1,9 @@ -"""Assignment - making a sklearn estimator. - -The goal of this assignment is to implement by yourself a scikit-learn -estimator for the OneNearestNeighbor and check that it is working properly. - -The nearest neighbor classifier predicts for a point X_i the target y_k of -the training sample X_k which is the closest to X_i. We measure proximity with -the Euclidean distance. The model will be evaluated with the accuracy (average -number of samples corectly classified). You need to implement the `fit`, -`predict` and `score` methods for this class. The code you write should pass -the test we implemented. You can run the tests by calling at the root of the -repo `pytest test_sklearn_questions.py`. - -We also ask to respect the pep8 convention: https://pep8.org. This will be -enforced with `flake8`. You can check that there is no flake8 errors by -calling `flake8` at the root of the repo. - -Finally, you need to write docstring similar to the one in `numpy_questions` -for the methods you code and for the class. The docstring will be checked using -`pydocstyle` that you can also call at the root of the repo. +""" +Implementation of a One-Nearest Neighbor classifier. + +Implementation of a One-Nearest Neighbor classifier adhering to +the scikit-learn estimator interface. + """ import numpy as np from sklearn.base import BaseEstimator @@ -29,46 +15,89 @@ class OneNearestNeighbor(BaseEstimator, ClassifierMixin): - "OneNearestNeighbor classifier." + """One-Nearest-Neighbor classifier. + + This classifier predicts the class of a sample by finding the + closest sample in the training data (using Euclidean distance) + and assigning its class. + """ def __init__(self): # noqa: D107 + """Initialize the OneNearestNeighbor classifier.""" pass def fit(self, X, y): - """Write docstring. - - And describe parameters + """Fit the One-Nearest-Neighbor classifier. + + This method stores the training data (X and y) to be used + during prediction. + + Parameters + ---------- + X : array-like of shape (n_samples, n_features) + Training data. + y : array-like of shape (n_samples,) + Target values. + + Returns + ------- + self : object + Returns the instance itself. """ 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_ = X + self.y_ = y + return self def predict(self, X): - """Write docstring. + """Predict the class labels for provided data. - And describe parameters + For each sample in X, finds the closest training sample + (using Euclidean distance) and returns its label. + + Parameters + ---------- + X : array-like of shape (n_samples, n_features) + Test 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 + y_pred = np.empty(shape=len(X), dtype=self.y_.dtype) + + for i, x_test in enumerate(X): + sq_distances = np.sum((self.X_ - x_test)**2, axis=1) + nearest_index = np.argmin(sq_distances) + y_pred[i] = self.y_[nearest_index] + return y_pred def score(self, X, y): - """Write docstring. - - And describe parameters + """Return the mean accuracy on the given test data and labels. + + Parameters + ---------- + X : array-like of shape (n_samples, n_features) + Test samples. + y : array-like of shape (n_samples,) + True labels for X. + + Returns + ------- + score : float + Mean accuracy of self.predict(X) wrt. y. """ X, y = check_X_y(X, y) y_pred = self.predict(X) - # XXX fix - return y_pred.sum() + return np.mean(y_pred == y) From c1c44944067805039f758e7c6585f7f32dbdf29e Mon Sep 17 00:00:00 2001 From: JFxhec Date: Thu, 13 Nov 2025 18:14:34 +0100 Subject: [PATCH 2/9] JulianFix --- numpy_questions.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index 5d1019ad..5da4d0e4 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -68,7 +68,7 @@ def wallis_product(n_terms): # XXX : The n_terms is an int that corresponds to the number of # terms in the product. For example 10000. - pi_by_2 = 1.0 + pi = 1.0 for n in range(1, n_terms + 1): # Use spaces around operators like ** for PEP 8 compliance @@ -76,5 +76,5 @@ def wallis_product(n_terms): den = num - 1.0 # Corrected variable name from pi_over_2 to pi_by_2 - pi_by_2 *= (num / den) - return 2.0 * pi_by_2 + pi *= (num / den) + return 2.0 * pi From 15fa21718cbae40d88c833ebfe2957faa690d198 Mon Sep 17 00:00:00 2001 From: JFxhec Date: Thu, 13 Nov 2025 18:17:08 +0100 Subject: [PATCH 3/9] julianfix --- numpy_questions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/numpy_questions.py b/numpy_questions.py index 5da4d0e4..00c37cd3 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -77,4 +77,4 @@ def wallis_product(n_terms): # Corrected variable name from pi_over_2 to pi_by_2 pi *= (num / den) - return 2.0 * pi + return pi From d7ae2d940f69a0c0fb5ec71f2d27fe2c62b8228b Mon Sep 17 00:00:00 2001 From: JFxhec Date: Thu, 13 Nov 2025 18:19:33 +0100 Subject: [PATCH 4/9] jf --- numpy_questions.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index 00c37cd3..2b870a35 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -71,10 +71,7 @@ def wallis_product(n_terms): pi = 1.0 for n in range(1, n_terms + 1): - # Use spaces around operators like ** for PEP 8 compliance num = 4.0 * n ** 2 den = num - 1.0 - - # Corrected variable name from pi_over_2 to pi_by_2 pi *= (num / den) - return pi + return pi*2 From 32a1fa7b2380aaaa21591e460f8327a01b07df70 Mon Sep 17 00:00:00 2001 From: JFxhec Date: Thu, 13 Nov 2025 18:29:32 +0100 Subject: [PATCH 5/9] numpy_questions.py --- numpy_questions.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index 2b870a35..08eb6f66 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -68,10 +68,14 @@ def wallis_product(n_terms): # XXX : The n_terms is an int that corresponds to the number of # terms in the product. For example 10000. - pi = 1.0 + if n_terms == 0: + pi = 1.0 + + else: + pi = 1.0 + for n in range(1, n_terms + 1): + num = 4.0 * n ** 2 + den = num - 1.0 + pi *= (num / den) - for n in range(1, n_terms + 1): - num = 4.0 * n ** 2 - den = num - 1.0 - pi *= (num / den) return pi*2 From a22126fc5eeee91939e378865e0bf77c8a3e2392 Mon Sep 17 00:00:00 2001 From: JFxhec Date: Thu, 13 Nov 2025 18:32:32 +0100 Subject: [PATCH 6/9] jf --- numpy_questions.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index 08eb6f66..da74e964 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -65,8 +65,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. if n_terms == 0: pi = 1.0 From cb226a92b41705db6c2d239b85950f46fb35693b Mon Sep 17 00:00:00 2001 From: JFxhec Date: Thu, 13 Nov 2025 18:38:26 +0100 Subject: [PATCH 7/9] v2 --- numpy_questions.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index da74e964..52a0b5c5 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -65,15 +65,15 @@ def wallis_product(n_terms): pi : float The approximation of order `n_terms` of pi using the Wallis product. """ - if n_terms == 0: pi = 1.0 else: - pi = 1.0 + pi_temp = 1.0 for n in range(1, n_terms + 1): num = 4.0 * n ** 2 den = num - 1.0 - pi *= (num / den) + pi_temp *= (num / den) + pi = 2*pi_temp - return pi*2 + return pi From 635fb5eefe71427d87fe6f608dcdd66796ae2295 Mon Sep 17 00:00:00 2001 From: JFxhec Date: Thu, 13 Nov 2025 18:42:35 +0100 Subject: [PATCH 8/9] v3 --- numpy_questions.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index 52a0b5c5..de16c760 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -69,11 +69,10 @@ def wallis_product(n_terms): pi = 1.0 else: - pi_temp = 1.0 + pi = 1.0 for n in range(1, n_terms + 1): num = 4.0 * n ** 2 den = num - 1.0 - pi_temp *= (num / den) - pi = 2*pi_temp + pi *= (num / den) return pi From 51c1e851d8a8a37a931cf33639cf643e20da2f55 Mon Sep 17 00:00:00 2001 From: JFxhec Date: Thu, 13 Nov 2025 18:49:47 +0100 Subject: [PATCH 9/9] v4 --- numpy_questions.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index de16c760..383e5b42 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -69,10 +69,11 @@ def wallis_product(n_terms): pi = 1.0 else: - pi = 1.0 + product = 1.0 for n in range(1, n_terms + 1): num = 4.0 * n ** 2 den = num - 1.0 - pi *= (num / den) + product *= (num / den) + pi = 2.0 * product return pi