From e8b0746a430d6c4c5efb0dad048d7babcc4b65bf Mon Sep 17 00:00:00 2001 From: AnaisStorp <157732684+AnaisStorp@users.noreply.github.com> Date: Thu, 13 Nov 2025 15:55:03 +0100 Subject: [PATCH 1/4] Assignments done Anais Storp --- numpy_questions.py | 16 ++++++++++--- sklearn_questions.py | 57 +++++++++++++++++++++++++++++++++++--------- 2 files changed, 59 insertions(+), 14 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index 21fcec4b..ed502ef3 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -15,6 +15,7 @@ This will be enforced with `flake8`. You can check that there is no flake8 errors by calling `flake8` at the root of the repo. """ + import numpy as np @@ -39,8 +40,11 @@ def max_index(X): """ i = 0 j = 0 - - # TODO + if not isinstance(X, np.ndarray): + raise ValueError("Input must be a numpy array") + if X.ndim != 2: + raise ValueError("Input array must be 2D") + i, j = np.unravel_index(np.argmax(X), X.shape) return i, j @@ -64,4 +68,10 @@ 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. + + product = 1.0 + for n in range(1, n_terms + 1): + product *= (4 * n * n) / (4 * n * n - 1) + pi = 2 * product + return pi + # return 0. diff --git a/sklearn_questions.py b/sklearn_questions.py index f65038c6..bd8d2c3a 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -19,6 +19,7 @@ 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. """ + import numpy as np from sklearn.base import BaseEstimator from sklearn.base import ClassifierMixin @@ -29,28 +30,48 @@ class OneNearestNeighbor(BaseEstimator, ClassifierMixin): - "OneNearestNeighbor classifier." + """One Nearest Neighbor classifier.""" def __init__(self): # noqa: D107 pass def fit(self, X, y): - """Write docstring. - - And describe parameters + """Fit the OneNearestNeighbor class. + + Parameters + ---------- + X : ndarray of shape (n_samples, n_features) + The training data. + y : ndarray of shape (n_samples, n_features) + Target for each training sample + + Returns + ------- + self : OneNearestNeighbor + The 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 + return self def predict(self, X): - """Write docstring. + """Predict the OneNearestNeighbor class. - And describe parameters + Parameters + ---------- + X : ndarray of shape (n_samples, n_features) + The test data. + + Returns + ------- + y_pred : ndarray of shape (n_samples,) + The predicted class for each sample. """ check_is_fitted(self) X = check_array(X) @@ -59,13 +80,27 @@ def predict(self, X): dtype=self.classes_.dtype ) - # XXX fix + for i, x_test in enumerate(X): + distances = np.sqrt(np.sum((self.X_ - x_test) ** 2, axis=1)) + nearest_idx = np.argmin(distances) + y_pred[i] = self.y_[nearest_idx] + return y_pred def score(self, X, y): - """Write docstring. - - And describe parameters + """Compute the accuracy of the OneNearestNeighbor classifier. + + Parameters + ---------- + X : ndarray of shape (n_samples, n_features) + The test data. + y : ndarray of shape (n_samples, n_features) + Train data. + + Returns + ------- + accuracy : float + The mean accuracy of the prediction. """ X, y = check_X_y(X, y) y_pred = self.predict(X) From 3665fde6cf4b8f0f15e094c74ce12dd6b67ed4b9 Mon Sep 17 00:00:00 2001 From: AnaisStorp <157732684+AnaisStorp@users.noreply.github.com> Date: Thu, 13 Nov 2025 16:08:30 +0100 Subject: [PATCH 2/4] Fixed numpy_questions Anais Storp --- numpy_questions.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/numpy_questions.py b/numpy_questions.py index ed502ef3..25d75518 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -69,6 +69,8 @@ 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. + if n_terms == 0: + return 1.0 product = 1.0 for n in range(1, n_terms + 1): product *= (4 * n * n) / (4 * n * n - 1) From 6c0c20509c183aa40d423740ed03450cb85acce3 Mon Sep 17 00:00:00 2001 From: AnaisStorp <157732684+AnaisStorp@users.noreply.github.com> Date: Thu, 13 Nov 2025 16:11:34 +0100 Subject: [PATCH 3/4] Fixed sklearn_questions Anais Storp --- sklearn_questions.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/sklearn_questions.py b/sklearn_questions.py index bd8d2c3a..44651189 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -104,6 +104,4 @@ def score(self, X, y): """ X, y = check_X_y(X, y) y_pred = self.predict(X) - - # XXX fix return y_pred.sum() From a6060109801267c4c7fccf66419da77bfc995817 Mon Sep 17 00:00:00 2001 From: AnaisStorp <157732684+AnaisStorp@users.noreply.github.com> Date: Thu, 13 Nov 2025 16:15:35 +0100 Subject: [PATCH 4/4] Fixed sklearn_questions again Anais Storp --- sklearn_questions.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/sklearn_questions.py b/sklearn_questions.py index 44651189..e74df9c7 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -104,4 +104,6 @@ def score(self, X, y): """ X, y = check_X_y(X, y) y_pred = self.predict(X) - return y_pred.sum() + + accuracy = np.mean(y_pred == y) + return accuracy