From d52627b5889adeedeb719a9d8f9f96fcb4eda651 Mon Sep 17 00:00:00 2001 From: Lucien Descazeaud Date: Thu, 13 Nov 2025 15:15:52 +0100 Subject: [PATCH 1/8] Complete numpy and sklearn assignments --- numpy_questions.py | 27 +++++++++++++++++++++++++-- sklearn_questions.py | 17 ++++++++++++++++- 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index 21fcec4b..9ab70235 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -40,7 +40,18 @@ def max_index(X): i = 0 j = 0 - # TODO + + if not isinstance(X, np.ndarray): + raise ValueError("The input must be a numpy array") + + d = X.ndim + + if d != 2: + raise ValueError("The input must be a 2D array") + + i_flat = np.argmax(X) + + i, j = np.unravel_index(i_flat, X.shape) return i, j @@ -64,4 +75,16 @@ 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. + + if n_terms == 0: + return 1 + + tab = np.arange(1, n_terms + 1) + n2 = tab * tab + n2x4 = 4*n2 + n2x4_1 = n2x4 - 1 + final = n2x4/n2x4_1 + prod = np.prod(final) + + + return 2*prod diff --git a/sklearn_questions.py b/sklearn_questions.py index f65038c6..9e55031c 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -44,6 +44,11 @@ def fit(self, X, y): self.classes_ = np.unique(y) self.n_features_in_ = X.shape[1] + self.X_ = X + self.y_ = y + + + # XXX fix return self @@ -60,6 +65,15 @@ def predict(self, X): ) # XXX fix + + for i in range(len(X)): + diff = self.X_ - X[i] + dist = np.linalg.norm(diff, axis=1) + idx = np.argmin(dist) + y_pred[i] = self.y_[idx] + + + return y_pred def score(self, X, y): @@ -71,4 +85,5 @@ def score(self, X, y): y_pred = self.predict(X) # XXX fix - return y_pred.sum() + return (y_pred == y).mean() + From e2bba67e5c334ea8d0b1536fe47f743d745388c4 Mon Sep 17 00:00:00 2001 From: Lucien Descazeaud Date: Thu, 13 Nov 2025 15:37:03 +0100 Subject: [PATCH 2/8] Fix formatting and style issues --- numpy_questions.py | 33 +++++++-------------------------- sklearn_questions.py | 7 ------- 2 files changed, 7 insertions(+), 33 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index 9ab70235..45928f20 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 @@ -40,7 +41,6 @@ def max_index(X): i = 0 j = 0 - if not isinstance(X, np.ndarray): raise ValueError("The input must be a numpy array") @@ -48,7 +48,7 @@ def max_index(X): if d != 2: raise ValueError("The input must be a 2D array") - + i_flat = np.argmax(X) i, j = np.unravel_index(i_flat, X.shape) @@ -57,34 +57,15 @@ def max_index(X): def wallis_product(n_terms): - """Implement the Wallis product to compute an approximation of pi. - - See: - https://en.wikipedia.org/wiki/Wallis_product - - Parameters - ---------- - n_terms : int - Number of steps in the Wallis product. Note that `n_terms=0` will - consider the product to be `1`. - - Returns - ------- - 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. if n_terms == 0: return 1 - + tab = np.arange(1, n_terms + 1) n2 = tab * tab - n2x4 = 4*n2 - n2x4_1 = n2x4 - 1 - final = n2x4/n2x4_1 + n2x4 = 4 * n2 + n2x4_1 = n2x4 - 1 + final = n2x4 / n2x4_1 prod = np.prod(final) - - return 2*prod + return 2 * prod diff --git a/sklearn_questions.py b/sklearn_questions.py index 9e55031c..bc921230 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -47,9 +47,6 @@ def fit(self, X, y): self.X_ = X self.y_ = y - - - # XXX fix return self def predict(self, X): @@ -64,16 +61,12 @@ def predict(self, X): dtype=self.classes_.dtype ) - # XXX fix - for i in range(len(X)): diff = self.X_ - X[i] dist = np.linalg.norm(diff, axis=1) idx = np.argmin(dist) y_pred[i] = self.y_[idx] - - return y_pred def score(self, X, y): From d00dfcae2572ad5d1d346a366d8212474e581ac2 Mon Sep 17 00:00:00 2001 From: Lucien Descazeaud Date: Thu, 13 Nov 2025 15:41:59 +0100 Subject: [PATCH 3/8] Fix formatting and style issues2 --- sklearn_questions.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/sklearn_questions.py b/sklearn_questions.py index bc921230..d290ffa9 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 @@ -57,8 +58,7 @@ def predict(self, X): check_is_fitted(self) X = check_array(X) y_pred = np.full( - shape=len(X), fill_value=self.classes_[0], - dtype=self.classes_.dtype + shape=len(X), fill_value=self.classes_[0], dtype=self.classes_.dtype ) for i in range(len(X)): @@ -78,5 +78,4 @@ def score(self, X, y): y_pred = self.predict(X) # XXX fix - return (y_pred == y).mean() - + return (y_pred == y).mean() \ No newline at end of file From f2470376aee25b64f1d37c8456109bfc56103566 Mon Sep 17 00:00:00 2001 From: Lucien Descazeaud Date: Thu, 13 Nov 2025 15:49:33 +0100 Subject: [PATCH 4/8] Fix formatting and style issue34 --- sklearn_questions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sklearn_questions.py b/sklearn_questions.py index d290ffa9..3e242c88 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -78,4 +78,4 @@ def score(self, X, y): y_pred = self.predict(X) # XXX fix - return (y_pred == y).mean() \ No newline at end of file + return (y_pred == y).mean() From 43b9ec5f9ec81e4129dc5fc4929bc65b4bed04ab Mon Sep 17 00:00:00 2001 From: Lucien Descazeaud Date: Thu, 13 Nov 2025 15:56:14 +0100 Subject: [PATCH 5/8] final --- numpy_questions.py | 1 - 1 file changed, 1 deletion(-) diff --git a/numpy_questions.py b/numpy_questions.py index 45928f20..4098fd90 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -57,7 +57,6 @@ def max_index(X): def wallis_product(n_terms): - if n_terms == 0: return 1 From cd3e01e8868457410606773c42420def4189e501 Mon Sep 17 00:00:00 2001 From: Lucien Descazeaud Date: Thu, 13 Nov 2025 16:09:33 +0100 Subject: [PATCH 6/8] rev --- sklearn_questions.py | 1 - 1 file changed, 1 deletion(-) diff --git a/sklearn_questions.py b/sklearn_questions.py index 3e242c88..2613add5 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -30,7 +30,6 @@ class OneNearestNeighbor(BaseEstimator, ClassifierMixin): - "OneNearestNeighbor classifier." def __init__(self): # noqa: D107 pass From 53fbe3dd0c421a6da54efd01972dc6899e11514d Mon Sep 17 00:00:00 2001 From: Lucien Descazeaud Date: Thu, 13 Nov 2025 16:15:52 +0100 Subject: [PATCH 7/8] =?UTF-8?q?piti=C3=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- numpy_questions.py | 14 ++++++++++++++ sklearn_questions.py | 8 +++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/numpy_questions.py b/numpy_questions.py index 4098fd90..5f0cae7e 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -57,6 +57,20 @@ def max_index(X): def wallis_product(n_terms): + """ + Compute an approximation of pi using the Wallis product. + + Parameters + ---------- + n_terms : int + Number of terms in the Wallis product. If `n_terms=0`, + the function returns 1. + + Returns + ------- + pi : float + Approximation of pi computed with `n_terms` terms. + """ if n_terms == 0: return 1 diff --git a/sklearn_questions.py b/sklearn_questions.py index 2613add5..a2049623 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -30,7 +30,13 @@ class OneNearestNeighbor(BaseEstimator, ClassifierMixin): - + """ + One-nearest-neighbor classifier. + + This estimator stores the training data during `fit` and predicts + the label of a new sample as the label of the closest training + sample using the Euclidean distance. + """ def __init__(self): # noqa: D107 pass From d1de2e82cee695f13dec6656b1f572910c307417 Mon Sep 17 00:00:00 2001 From: Lucien Descazeaud Date: Thu, 13 Nov 2025 16:18:35 +0100 Subject: [PATCH 8/8] Ipay --- sklearn_questions.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sklearn_questions.py b/sklearn_questions.py index a2049623..0663dfab 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -32,11 +32,12 @@ class OneNearestNeighbor(BaseEstimator, ClassifierMixin): """ One-nearest-neighbor classifier. - + This estimator stores the training data during `fit` and predicts the label of a new sample as the label of the closest training sample using the Euclidean distance. """ + def __init__(self): # noqa: D107 pass