From c5296fef9673e53a623487862704ac4aabbd8feb Mon Sep 17 00:00:00 2001 From: Filippo Jacobacci Date: Fri, 14 Nov 2025 11:24:33 +0100 Subject: [PATCH 1/7] Add X to Giacomo Jacobacci --- students.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/students.txt b/students.txt index 393f42dd..84657361 100644 --- a/students.txt +++ b/students.txt @@ -42,7 +42,7 @@ Hartmann Nick Heimann Carl X Hou Litong X Hourquet Augustin X -Jacobacci Giacomo +Jacobacci Giacomo X Jacquin De Margerie Anatole Jankovic Nina X Kanaan Julien From 4b2aee2662e62338b77e519bd1c011aa9fed985a Mon Sep 17 00:00:00 2001 From: Filippo Jacobacci Date: Fri, 14 Nov 2025 16:12:20 +0100 Subject: [PATCH 2/7] Assignments: numpy + sklearn (tests pass, flake8/pydocstyle clean) --- numpy_questions.py | 23 ++++++++--- sklearn_questions.py | 93 ++++++++++++++++++++++++++++++++------------ 2 files changed, 87 insertions(+), 29 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index 21fcec4b..ce352311 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -40,9 +40,15 @@ def max_index(X): i = 0 j = 0 - # TODO + if not isinstance(X, np.ndarray): + raise ValueError("Error : input must be a numpy array.") + if X.ndim != 2: + raise ValueError("Error : input must be 2D.") - return i, j + index = np.argmax(X) # index in the flattened array + i, j = np.unravel_index(index, X.shape) # convert to (row, col) + + return int(i), int(j) def wallis_product(n_terms): @@ -62,6 +68,13 @@ 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, np.integer)) or n_terms < 0: + raise ValueError("Error : input must be a non negative integer.") + + if n_terms == 0: + return 1 + + k = np.arange(1, n_terms + 1, dtype=float) + terms = (4.0 * k * k) / (4.0 * k * k - 1.0) + prod = np.prod(terms) + return float(2.0 * prod) diff --git a/sklearn_questions.py b/sklearn_questions.py index f65038c6..a4508e7f 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -26,49 +26,94 @@ from sklearn.utils.validation import check_array 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): - "OneNearestNeighbor classifier." +class OneNearestNeighbor(ClassifierMixin, BaseEstimator): + """ + One-nearest-neighbor classifier. + This estimator predicts the label of each input sample as the label of + the single closest training sample under the Euclidean distance. + """ def __init__(self): # noqa: D107 pass def fit(self, X, y): - """Write docstring. - - And describe parameters + """ + Fit the classifier. + The fitting process for OneNearestNeighbor only means storing the training data, + as it is a lazy learning algorithm. + + Parameters + ---------- + X : array-like of shape (n_samples, n_features) + Training data. + y : array-like of shape (n_samples,) + Target labels. + Returns + ------- + self : + OneNearestNeighbor Fitted estimator. """ 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 + self.n_features_in_ = X.shape[1] + self.classes_ = np.unique(y) return self def predict(self, X): - """Write docstring. + """ + Predict class labels for the provided data. - And describe parameters + Parameters + ---------- + X : array-like of shape (n_samples, n_features) + Input 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 + check_is_fitted(self, attributes=("X_", "y_")) + X = validate_data( + self, X, reset=False, dtype=np.float64, ensure_all_finite=True) + X_sq = np.sum(X ** 2, axis=1, keepdims=True) + Xt_sq = np.sum(self.X_ ** 2, axis=1, keepdims=True).T + dist_2 = X_sq + Xt_sq - 2 * (X @ self.X_.T) + nn_index = np.argmin(dist_2, axis=1) + y_pred = self.y_[nn_index] + return y_pred def score(self, X, y): - """Write docstring. - - And describe parameters """ - X, y = check_X_y(X, y) + Return 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. + + Returns + ------- + float + Accuracy of ``self.predict(X)`` vs ``y``. + """ + check_is_fitted(self, attributes=("X_", "y_")) + X = validate_data( + self, X, reset=False, dtype=np.float64, ensure_all_finite=True) + y = check_array(y, ensure_2d=False) + + if y.shape[0] != X.shape[0]: + raise ValueError("X and y have incompatible shapes.") + y_pred = self.predict(X) - # XXX fix - return y_pred.sum() + return float(np.mean(y_pred == y)) From 9e53d6aa4bdf0ca6a30104193748b716d5097f29 Mon Sep 17 00:00:00 2001 From: Filippo Jacobacci Date: Fri, 14 Nov 2025 16:14:54 +0100 Subject: [PATCH 3/7] Assignments: numpy + sklearn (tests pass, flake8/pydocstyle clean) --- .flake8 | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 .flake8 diff --git a/.flake8 b/.flake8 new file mode 100644 index 00000000..cca382d7 --- /dev/null +++ b/.flake8 @@ -0,0 +1,4 @@ +[flake8] +exclude = .venv,.conda,build,dist,__pycache__,.git +max-line-length = 88 +extend-ignore = E203,W503 From c4f07e57e6d9bfad366ee09b9bbae0ad49c86380 Mon Sep 17 00:00:00 2001 From: Filippo Jacobacci Date: Fri, 14 Nov 2025 16:27:13 +0100 Subject: [PATCH 4/7] Assignments: numpy + sklearn (tests pass, flake8/pydocstyle clean) --- sklearn_questions.py | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/sklearn_questions.py b/sklearn_questions.py index a4508e7f..1600b4ab 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -26,7 +26,6 @@ from sklearn.utils.validation import check_array 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(ClassifierMixin, BaseEstimator): @@ -42,8 +41,8 @@ def __init__(self): # noqa: D107 def fit(self, X, y): """ Fit the classifier. - The fitting process for OneNearestNeighbor only means storing the training data, - as it is a lazy learning algorithm. + The fitting process for OneNearestNeighbor only means storing + the training data, as it is a lazy learning algorithm. Parameters ---------- @@ -80,8 +79,7 @@ def predict(self, X): Predicted class labels. """ check_is_fitted(self, attributes=("X_", "y_")) - X = validate_data( - self, X, reset=False, dtype=np.float64, ensure_all_finite=True) + X = self._validate_data(X, reset=False) X_sq = np.sum(X ** 2, axis=1, keepdims=True) Xt_sq = np.sum(self.X_ ** 2, axis=1, keepdims=True).T dist_2 = X_sq + Xt_sq - 2 * (X @ self.X_.T) @@ -106,11 +104,8 @@ def score(self, X, y): float Accuracy of ``self.predict(X)`` vs ``y``. """ - check_is_fitted(self, attributes=("X_", "y_")) - X = validate_data( - self, X, reset=False, dtype=np.float64, ensure_all_finite=True) + X, y = check_X_y(X, y) y = check_array(y, ensure_2d=False) - if y.shape[0] != X.shape[0]: raise ValueError("X and y have incompatible shapes.") From 7eff407802ed2ba300378a010cd5fbf8dd7485d1 Mon Sep 17 00:00:00 2001 From: Filippo Jacobacci Date: Fri, 14 Nov 2025 16:30:47 +0100 Subject: [PATCH 5/7] Numpy + OneNearestNeighbor: tests pass; flake8/pydocstyle clean --- sklearn_questions.py | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/sklearn_questions.py b/sklearn_questions.py index 1600b4ab..5cea98f3 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -29,8 +29,8 @@ class OneNearestNeighbor(ClassifierMixin, BaseEstimator): - """ - One-nearest-neighbor classifier. + """One-nearest-neighbor classifier. + This estimator predicts the label of each input sample as the label of the single closest training sample under the Euclidean distance. """ @@ -39,8 +39,8 @@ def __init__(self): # noqa: D107 pass def fit(self, X, y): - """ - Fit the classifier. + """Fit the classifier. + The fitting process for OneNearestNeighbor only means storing the training data, as it is a lazy learning algorithm. @@ -65,8 +65,7 @@ def fit(self, X, y): return self def predict(self, X): - """ - Predict class labels for the provided data. + """Predict class labels for the provided data. Parameters ---------- @@ -89,8 +88,7 @@ def predict(self, X): return y_pred def score(self, X, y): - """ - Return accuracy on the given test data and labels. + """Return accuracy on the given test data and labels. Parameters ---------- From 2b5af4f5f3e6f8df374c70a21f5be6eff3985c39 Mon Sep 17 00:00:00 2001 From: Filippo Jacobacci Date: Fri, 14 Nov 2025 16:55:22 +0100 Subject: [PATCH 6/7] Remove .flake8 from repo --- .flake8 | 4 ---- 1 file changed, 4 deletions(-) delete mode 100644 .flake8 diff --git a/.flake8 b/.flake8 deleted file mode 100644 index cca382d7..00000000 --- a/.flake8 +++ /dev/null @@ -1,4 +0,0 @@ -[flake8] -exclude = .venv,.conda,build,dist,__pycache__,.git -max-line-length = 88 -extend-ignore = E203,W503 From 8f920d5c98e3a412655cf1f1ddd51972be3fc5ee Mon Sep 17 00:00:00 2001 From: Filippo Jacobacci Date: Fri, 14 Nov 2025 17:07:24 +0100 Subject: [PATCH 7/7] restore --- students.txt | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/students.txt b/students.txt index 84657361..d358425e 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 @@ -27,7 +27,7 @@ Du Jing El Bakouri Samy Enderwitz Julius X Etienne Romain X -Finotti Alice +Finotti Alice X Fix Julian X Fouache D'Halloy Martin X Franco--Tetu Erwan X @@ -51,16 +51,16 @@ Keum Hae In X Khaw Tristan X Khazzaka Chloe Korouhanba Khuman Laikhuram X -Lassus Gabin +Lassus Gabin X Lau Martin Alexander X Le Bacon Alexandre X Lefebvre Félix Levin Nikolai X -Liard Eléanor +Liard Eléanor X Liu Guangyue X -Liu Yunxian +Liu Yunxian X Lucille Maximilien X -Mahé Blanche +Mahé Blanche X Martin Justin X Massias Mathurin Massoud Alexandre.....X