From deeb10a9f7112a46f0f85c0bc8e7a1a2320f188c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9lia?= Date: Thu, 13 Nov 2025 14:20:59 +0100 Subject: [PATCH 1/4] added a cross next to my name --- students.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/students.txt b/students.txt index 91aaa9d6..f8781123 100644 --- a/students.txt +++ b/students.txt @@ -68,7 +68,7 @@ Mayette Scott Melhaoui Romaissae Meng Ziqi Meyer Felix -Morais Célia +Morais Célia X Moufad Badr Nasr Najib Odend'Hal Charles From 15c439716a230cdd721c429511ad964badb15025 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9lia?= Date: Thu, 13 Nov 2025 16:02:37 +0100 Subject: [PATCH 2/4] questions answered --- numpy_questions.py | 19 ++++++++++----- sklearn_questions.py | 56 ++++++++++++++++++++++++++++++++------------ 2 files changed, 54 insertions(+), 21 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index 21fcec4b..b2cfc6cb 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 should be a numpy array.") + if X.ndim != 2: + raise ValueError("Input should be a 2D numpy array.") - # TODO + index = np.argmax(X) + i, j = np.unravel_index(index, X.shape) return i, j @@ -62,6 +65,10 @@ 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 n_terms == 0: + return 1 + pi = 1.0 + for i in range(1, n_terms + 1): + pi = pi * (4 * i ** 2) / (4 * i ** 2 - 1) + return pi * 2 diff --git a/sklearn_questions.py b/sklearn_questions.py index f65038c6..e65206bc 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -23,34 +23,49 @@ 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 check_array from sklearn.utils.multiclass import check_classification_targets -class OneNearestNeighbor(BaseEstimator, ClassifierMixin): +class OneNearestNeighbor(ClassifierMixin, BaseEstimator): "OneNearestNeighbor classifier." def __init__(self): # noqa: D107 pass def fit(self, X, y): - """Write docstring. + """ Fit the OneNearestNeighbor model to the given training data. - And describe parameters + Parameters + ---------- + X : ndarray of shape (n_samples, n_features) + y : ndarray of shape (n_samples,) + + Returns + ------- + self : object + The fitted model """ 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 labels for the given training data. - And describe parameters + Parameters + ---------- + X : ndarray of shape (n_samples, n_features) + + Returns + ------- + y_pred : ndarray of shape (n_samples,) + The predicted labels. """ check_is_fitted(self) X = check_array(X) @@ -59,16 +74,27 @@ def predict(self, X): dtype=self.classes_.dtype ) - # XXX fix + for i in range(len(X)): + distances = np.linalg.norm(self.X_ - X[i, :], axis=1) + nearest_index = distances.argmin() + y_pred[i] = self.y_[nearest_index] return y_pred def score(self, X, y): - """Write docstring. - - And describe parameters + """ Return the accuracy, i.e. the number of correct label predictions + on the given targeted data. + + Parameters + ---------- + X : ndarray of shape (n_samples, n_features) + y : ndarray of shape (n_samples,) + + Returns + ------- + score : float + The number of correct predictions. """ X, y = check_X_y(X, y) y_pred = self.predict(X) - - # XXX fix - return y_pred.sum() + y_pred = (y_pred == y) + return y_pred.sum()/len(y_pred) From b46ef4cd9155f547dd1cd6cf80ba9cd548f67b0e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9lia?= Date: Thu, 13 Nov 2025 16:20:08 +0100 Subject: [PATCH 3/4] questions answered --- numpy_questions.py | 1 - sklearn_questions.py | 10 +++++----- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index b2cfc6cb..953f1c45 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -65,7 +65,6 @@ 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 pi = 1.0 diff --git a/sklearn_questions.py b/sklearn_questions.py index e65206bc..30c35fab 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -29,13 +29,13 @@ class OneNearestNeighbor(ClassifierMixin, BaseEstimator): - "OneNearestNeighbor classifier." + """OneNearestNeighbor classifier.""" def __init__(self): # noqa: D107 pass def fit(self, X, y): - """ Fit the OneNearestNeighbor model to the given training data. + """Fit the OneNearestNeighbor model to the given training data. Parameters ---------- @@ -56,7 +56,7 @@ def fit(self, X, y): return self def predict(self, X): - """ Predict the labels for the given training data. + """Predict the labels for the given training data. Parameters ---------- @@ -81,8 +81,7 @@ def predict(self, X): return y_pred def score(self, X, y): - """ Return the accuracy, i.e. the number of correct label predictions - on the given targeted data. + """Return the number of correct label predictions on the given targeted data. Parameters ---------- @@ -97,4 +96,5 @@ def score(self, X, y): X, y = check_X_y(X, y) y_pred = self.predict(X) y_pred = (y_pred == y) + return y_pred.sum()/len(y_pred) From c5a455c8af5f402dd810d09bb4ed13008bd2708d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9lia?= Date: Thu, 13 Nov 2025 16:31:59 +0100 Subject: [PATCH 4/4] questions answered --- sklearn_questions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sklearn_questions.py b/sklearn_questions.py index 30c35fab..bc905462 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -81,7 +81,7 @@ def predict(self, X): return y_pred def score(self, X, y): - """Return the number of correct label predictions on the given targeted data. + """Return the number of correct label predictions on the given data. Parameters ----------