From 89991b98af365d698b332311d9eaf7e4d9a4dd94 Mon Sep 17 00:00:00 2001 From: erwanfrancotetu Date: Thu, 13 Nov 2025 15:55:13 +0100 Subject: [PATCH 1/4] Erwan Franco Tetu Modified both questions files --- numpy_questions.py | 20 ++++++++++++++++++-- sklearn_questions.py | 45 +++++++++++++++++++++++++++++++++++++------- 2 files changed, 56 insertions(+), 9 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index 21fcec4b..058f25d1 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,13 @@ def max_index(X): """ i = 0 j = 0 + if not isinstance(X, np.ndarray): + raise ValueError("Input must be a numpy array.") + + if X.ndim != 2: + raise ValueError("Input array must be 2-dimensional.") - # TODO + i, j = np.unravel_index(np.argmax(X), X.shape) return i, j @@ -64,4 +70,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. + + prod = 1.0 + + if n_terms == 0: + return 1.0 + + for i in range(1, n_terms + 1): + prod *= (4 * i**2) / (4 * i**2 - 1) + pi = 2 * prod + + return pi diff --git a/sklearn_questions.py b/sklearn_questions.py index f65038c6..05e49277 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,15 +30,26 @@ class OneNearestNeighbor(BaseEstimator, ClassifierMixin): - "OneNearestNeighbor classifier." + """OneNearestNeighbor classifier.""" def __init__(self): # noqa: D107 pass def fit(self, X, y): - """Write docstring. + """Fit the OneNearestNeighbor class. + + Parameters + ---------- + X: ndarray of shape (n_samples, n_features) + Training data. + + y: ndarray of shape (n_samples, n_features) + Target for each training samples. - And describe parameters + Returns + ---------- + self: object + Fitted estimator """ X, y = check_X_y(X, y) check_classification_targets(y) @@ -48,9 +60,17 @@ def fit(self, X, 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) + Test data. + + Returns + ---------- + y_pred: ndnarray of shape (n_samples) + Predicted output. """ check_is_fitted(self) X = check_array(X) @@ -63,9 +83,20 @@ def predict(self, X): return y_pred def score(self, X, y): - """Write docstring. + """Comptute the accuracy of the OneNearestNeighbor class. + + Parameters + ---------- + X: ndarray of shape (n_samples, n_features) + Test data. + + y: ndarray of shape (n_samples, n_features) + Train data. - And describe parameters + Returns + ---------- + y_pred.sum(): float + Mean accuracy of prediction. """ X, y = check_X_y(X, y) y_pred = self.predict(X) From f1a146279e918419d158e11ff9fa5f0924e99b10 Mon Sep 17 00:00:00 2001 From: erwanfrancotetu Date: Thu, 13 Nov 2025 16:20:24 +0100 Subject: [PATCH 2/4] Fixed sklearn_question Erwan Franco Tetu --- sklearn_questions.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/sklearn_questions.py b/sklearn_questions.py index 05e49277..d5d54e02 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -56,7 +56,9 @@ def fit(self, X, 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): @@ -79,7 +81,10 @@ 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[1] = self.y_(nearest_idx) return y_pred def score(self, X, y): @@ -95,11 +100,11 @@ def score(self, X, y): Returns ---------- - y_pred.sum(): float + accuracy: float Mean accuracy of prediction. """ X, y = check_X_y(X, y) y_pred = self.predict(X) - # XXX fix - return y_pred.sum() + accuracy = np.mean(y_pred == y) + return accuracy From 217f8a9542e0465e154fa3fc038e28102ed6971d Mon Sep 17 00:00:00 2001 From: erwanfrancotetu Date: Thu, 13 Nov 2025 16:23:24 +0100 Subject: [PATCH 3/4] Fixed sklearn_question Erwan Franco Tetu --- sklearn_questions.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sklearn_questions.py b/sklearn_questions.py index d5d54e02..42dfbce6 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -83,8 +83,8 @@ def predict(self, X): 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[1] = self.y_(nearest_idx) + nearest = np.argmin(distances) + y_pred[1] = self.y_[nearest] return y_pred def score(self, X, y): From cad261f3e296f3d8ab2c0dd0d731cc5ba6c6def4 Mon Sep 17 00:00:00 2001 From: erwanfrancotetu Date: Thu, 13 Nov 2025 16:26:03 +0100 Subject: [PATCH 4/4] Fixed sklearn_question Erwan Franco Tetu --- sklearn_questions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sklearn_questions.py b/sklearn_questions.py index 42dfbce6..2ae045b2 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -84,7 +84,7 @@ def predict(self, X): for i, x_test in enumerate(X): distances = np.sqrt(np.sum((self.X_ - x_test) ** 2, axis=1)) nearest = np.argmin(distances) - y_pred[1] = self.y_[nearest] + y_pred[i] = self.y_[nearest] return y_pred def score(self, X, y):