From c886cca83d41956a89c6f2fb607698b97ad0511b Mon Sep 17 00:00:00 2001 From: AugustinClot Date: Fri, 14 Nov 2025 13:48:06 +0100 Subject: [PATCH 1/5] Add X next to Augustin Clot --- students.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/students.txt b/students.txt index 6813300e..c909d099 100644 --- a/students.txt +++ b/students.txt @@ -15,7 +15,7 @@ Burtin Léo Chaabouni Kenza Chauhan Bhavesh Chou Wei-Chieh X -Clot Augustin +Clot Augustin X De Sauvan D'Aramon Ithier X Descazeaud Lucien X Despréaux Maxime X From 85b5ef2034552c2faeb11e6d1d24ddc787bd4e05 Mon Sep 17 00:00:00 2001 From: blanchemahe Date: Fri, 14 Nov 2025 14:04:37 +0100 Subject: [PATCH 2/5] added X after my name (#719) --- students.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/students.txt b/students.txt index c909d099..d358425e 100644 --- a/students.txt +++ b/students.txt @@ -60,7 +60,7 @@ Liard Eléanor X Liu Guangyue X Liu Yunxian X Lucille Maximilien X -Mahé Blanche +Mahé Blanche X Martin Justin X Massias Mathurin Massoud Alexandre.....X From 6254ea6836a29a2476d1e3566856e318123a7418 Mon Sep 17 00:00:00 2001 From: AugustinClot Date: Sat, 15 Nov 2025 01:31:00 +0100 Subject: [PATCH 3/5] Files for part 2 --- numpy_questions.py | 21 ++++++---- sklearn_questions.py | 93 +++++++++++++++++++++++++++++++------------- 2 files changed, 80 insertions(+), 34 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index 21fcec4b..aa1a10df 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -37,12 +37,11 @@ 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("X must be a 2D numpy array.") - # TODO - - return i, j + flat_idx = np.argmax(X) + return np.unravel_index(flat_idx, X.shape) def wallis_product(n_terms): @@ -62,6 +61,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 not isinstance(n_terms, int) or n_terms < 0: + raise ValueError("n_terms must be a non-negative integer.") + + if n_terms == 0: + return 1.0 + + k = np.arange(1, n_terms + 1, dtype=np.float64) + terms = (2 * k / (2 * k - 1)) * (2 * k / (2 * k + 1)) + return 2.0 * np.prod(terms) diff --git a/sklearn_questions.py b/sklearn_questions.py index f65038c6..8c713c2e 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -22,53 +22,94 @@ 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 check_is_fitted +from sklearn.utils.validation import validate_data 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 classifier by memorizing the training set. - And describe parameters + Parameters + ---------- + X : ndarray of shape (n_samples, n_features) + Training input samples. + + y : ndarray of shape (n_samples,) + Target class labels. + + Returns + ------- + self : OneNearestNeighbor + Fitted estimator. """ - X, y = check_X_y(X, y) + X, y = validate_data(self, X, y, ensure_2d=True) + + # 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 class labels for samples in `X`. - And describe parameters - """ - check_is_fitted(self) - X = check_array(X) - y_pred = np.full( - shape=len(X), fill_value=self.classes_[0], - dtype=self.classes_.dtype - ) + Parameters + ---------- + X : ndarray of shape (n_queries, n_features) + Input samples. - # XXX fix - return y_pred + Returns + ------- + y_pred : ndarray of shape (n_queries,) + Predicted class labels. + """ + check_is_fitted( + self, attributes=["X_", "y_", "classes_", "n_features_in_"] + ) + # X = check_array(X) + X = validate_data(self, X, reset=False) + if X.shape[1] != self.n_features_in_: + raise ValueError( + "X has a different number of features than seen during fit: " + f"{X.shape[1]} != {self.n_features_in_}" + ) + + # Compute squared Euclidean distances to all training points + diff = X[:, np.newaxis, :] - self.X_[np.newaxis, :, :] + dists = np.sum(diff ** 2, axis=2) + + # Index of the closest training sample for each query + nn_idx = np.argmin(dists, axis=1) + + return self.y_[nn_idx] def score(self, X, y): - """Write docstring. + """ + Compute accuracy of the classifier on the given test data and labels. + + Parameters + ---------- + X : ndarray of shape (n_samples, n_features) + Test samples. - And describe parameters + y : ndarray of shape (n_samples,) + True labels for `X`. + + Returns + ------- + accuracy : float + Mean accuracy of predictions on `X` compared to `y`. """ - X, y = check_X_y(X, y) + # X, y = check_X_y(X, y) + X, y = validate_data(self, X, y, reset=False) y_pred = self.predict(X) - - # XXX fix - return y_pred.sum() + return float(np.mean(y_pred == y)) From 4eef4329ffbc6aadb2a22d6e55dec2a485338209 Mon Sep 17 00:00:00 2001 From: AugustinClot Date: Sat, 15 Nov 2025 01:50:19 +0100 Subject: [PATCH 4/5] small changes to pass tests --- sklearn_questions.py | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/sklearn_questions.py b/sklearn_questions.py index 8c713c2e..b2fcc6c0 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -23,8 +23,10 @@ from sklearn.base import BaseEstimator from sklearn.base import ClassifierMixin from sklearn.utils.validation import check_is_fitted -from sklearn.utils.validation import validate_data from sklearn.utils.multiclass import check_classification_targets +from sklearn.utils.validation import check_is_fitted +from sklearn.utils.validation import check_X_y +from sklearn.utils.validation import check_array class OneNearestNeighbor(ClassifierMixin, BaseEstimator): @@ -49,9 +51,7 @@ def fit(self, X, y): self : OneNearestNeighbor Fitted estimator. """ - X, y = validate_data(self, X, y, ensure_2d=True) - - # X, y = check_X_y(X, y) + X, y = check_X_y(X, y) check_classification_targets(y) self.classes_ = np.unique(y) self.n_features_in_ = X.shape[1] @@ -75,21 +75,16 @@ def predict(self, X): check_is_fitted( self, attributes=["X_", "y_", "classes_", "n_features_in_"] ) - # X = check_array(X) - X = validate_data(self, X, reset=False) + X = check_array(X) if X.shape[1] != self.n_features_in_: raise ValueError( - "X has a different number of features than seen during fit: " - f"{X.shape[1]} != {self.n_features_in_}" + f"X has {X.shape[1]} features, but {self.__class__.__name__} " + f"is expecting {self.n_features_in_} features as input" ) - # Compute squared Euclidean distances to all training points diff = X[:, np.newaxis, :] - self.X_[np.newaxis, :, :] dists = np.sum(diff ** 2, axis=2) - - # Index of the closest training sample for each query nn_idx = np.argmin(dists, axis=1) - return self.y_[nn_idx] def score(self, X, y): @@ -109,7 +104,6 @@ def score(self, X, y): accuracy : float Mean accuracy of predictions on `X` compared to `y`. """ - # X, y = check_X_y(X, y) - X, y = validate_data(self, X, y, reset=False) + X, y = check_X_y(X, y) y_pred = self.predict(X) return float(np.mean(y_pred == y)) From 338204894b01e0256ef8b077f58d35b7253cad54 Mon Sep 17 00:00:00 2001 From: AugustinClot Date: Sat, 15 Nov 2025 01:57:06 +0100 Subject: [PATCH 5/5] final commit i hope --- sklearn_questions.py | 1 - 1 file changed, 1 deletion(-) diff --git a/sklearn_questions.py b/sklearn_questions.py index b2fcc6c0..41dc458b 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -22,7 +22,6 @@ import numpy as np from sklearn.base import BaseEstimator 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 check_is_fitted from sklearn.utils.validation import check_X_y