From 0f86c6aebf995da5a1bd734bf6ecfefd137d3a1a Mon Sep 17 00:00:00 2001 From: NelsonTEJO Date: Thu, 13 Nov 2025 17:59:32 +0100 Subject: [PATCH 1/9] Modification des deux fichiers avec les fonctions valides --- numpy_questions.py | 23 ++++++++++++++++- sklearn_questions.py | 59 ++++++++++++++++++++++++++++++++++++++------ 2 files changed, 73 insertions(+), 9 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index 21fcec4b..a84ea0c3 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -41,6 +41,18 @@ def max_index(X): j = 0 # TODO + if not isinstance(X, np.ndarray) or X.ndim!=2: + raise ValueError("Input array should be a 2-dimensional numpy array") + + i,j=0,0 + max=X[0][0] + + for l in range(X.shape[0]): + for c in range(X.shape[1]): + if X[l][c]>max: + i,j=l,c + max=X[l][c] + continue return i, j @@ -64,4 +76,13 @@ 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. + + res=1 + if n_terms==0: + return 1 + + for k in range(1,n_terms+1): + res*=(4*k**2)/(4*k**2 - 1) + return 2*res + + diff --git a/sklearn_questions.py b/sklearn_questions.py index f65038c6..c638bda3 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -35,9 +35,20 @@ def __init__(self): # noqa: D107 pass def fit(self, X, y): - """Write docstring. - - And describe parameters + """ + Train the OneNearestNeighbor predictor. + + Parameters + ---------- + X : array-like of shape (n_samples, n_features) + Train set. + y : array-like of shape (n_samples,) + Target variable. + + Returns + ------- + self : object + """ X, y = check_X_y(X, y) check_classification_targets(y) @@ -45,12 +56,25 @@ def fit(self, X, y): self.n_features_in_ = X.shape[1] # XXX fix + + self.X_train_ = X + self.y_train_ = y + return self def predict(self, X): - """Write docstring. + """ + Predict the class of each sample of X. - And describe parameters + Parameters + ---------- + X : array-like of shape (n_samples, n_features) + test set. + + Returns + ------- + y_pred : ndarray of shape (n_samples,) + predictions """ check_is_fitted(self) X = check_array(X) @@ -60,15 +84,34 @@ def predict(self, X): ) # XXX fix + for i, x in enumerate(X): + + distances = np.sqrt(np.sum((self.X_train_ - x) ** 2, axis=1)) + + idx_min = np.argmin(distances) + + y_pred[i] = self.y_train_[idx_min] + return y_pred def score(self, X, y): - """Write docstring. - - And describe parameters + """ + Evaluate the performance of the model + + Parameters + ---------- + X : array-like of shape (n_samples, n_features) + test set. + y : array-like of shape (n_samples,) + Target variable. + + Returns + ------- + accuracy: percentage of good predictions """ X, y = check_X_y(X, y) y_pred = self.predict(X) # XXX fix + y_pred=(y_pred==y)/len(y) return y_pred.sum() From c48bcae88fd73d6069ac398fa4079d3964064f53 Mon Sep 17 00:00:00 2001 From: NelsonTEJO Date: Thu, 13 Nov 2025 18:14:43 +0100 Subject: [PATCH 2/9] corrige les erreurs --- numpy_questions.py | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index a84ea0c3..d3f53a87 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -43,20 +43,15 @@ def max_index(X): # TODO if not isinstance(X, np.ndarray) or X.ndim!=2: raise ValueError("Input array should be a 2-dimensional numpy array") - i,j=0,0 max=X[0][0] - - for l in range(X.shape[0]): + for line in range(X.shape[0]): for c in range(X.shape[1]): - if X[l][c]>max: - i,j=l,c - max=X[l][c] + if X[line][c]>max: + i,j=line,c + max=X[line][c] continue - return i, j - - def wallis_product(n_terms): """Implement the Wallis product to compute an approximation of pi. @@ -76,11 +71,9 @@ 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. - res=1 if n_terms==0: return 1 - for k in range(1,n_terms+1): res*=(4*k**2)/(4*k**2 - 1) return 2*res From c7d2b92062113f45f8812f7685ce87951db22025 Mon Sep 17 00:00:00 2001 From: NelsonTEJO Date: Thu, 13 Nov 2025 18:25:28 +0100 Subject: [PATCH 3/9] nouvelle modification --- numpy_questions.py | 37 +++++++++++++++++-------------------- 1 file changed, 17 insertions(+), 20 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index d3f53a87..eefb741c 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -37,21 +37,20 @@ def max_index(X): If the input is not a numpy array or if the shape is not 2D. """ - i = 0 - j = 0 - - # TODO - if not isinstance(X, np.ndarray) or X.ndim!=2: + if not isinstance(X, np.ndarray) or X.ndim != 2: raise ValueError("Input array should be a 2-dimensional numpy array") - i,j=0,0 - max=X[0][0] - for line in range(X.shape[0]): - for c in range(X.shape[1]): - if X[line][c]>max: - i,j=line,c - max=X[line][c] - continue + + i, j = 0, 0 + max_val = X[0][0] + for row in range(X.shape[0]): + for col in range(X.shape[1]): + if X[row][col] > max_val: + i, j = row, col + max_val = X[row][col] + return i, j + + def wallis_product(n_terms): """Implement the Wallis product to compute an approximation of pi. @@ -69,13 +68,11 @@ 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. - res=1 - if n_terms==0: + res = 1 + if n_terms == 0: return 1 - for k in range(1,n_terms+1): - res*=(4*k**2)/(4*k**2 - 1) - return 2*res + for k in range(1, n_terms + 1): + res *= (4 * k**2) / (4 * k**2 - 1) + return 2 * res From 03611d0e666019ac417470aba7b4429e22ee8c2d Mon Sep 17 00:00:00 2001 From: NelsonTEJO Date: Thu, 13 Nov 2025 18:33:56 +0100 Subject: [PATCH 4/9] Modification de sklearn_questions --- sklearn_questions.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sklearn_questions.py b/sklearn_questions.py index c638bda3..47d47eff 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -113,5 +113,5 @@ def score(self, X, y): y_pred = self.predict(X) # XXX fix - y_pred=(y_pred==y)/len(y) - return y_pred.sum() + + return float(np.mean(y_pred==y)) From d059d65d09379ef96be7dacdf1db6a65c23ffeda Mon Sep 17 00:00:00 2001 From: NelsonTEJO Date: Thu, 13 Nov 2025 18:37:00 +0100 Subject: [PATCH 5/9] nouvelle modif --- sklearn_questions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sklearn_questions.py b/sklearn_questions.py index 47d47eff..9135a9f2 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -114,4 +114,4 @@ def score(self, X, y): # XXX fix - return float(np.mean(y_pred==y)) + return float(np.mean(y_pred == y)) From b4fccf0dbd8e1b4eaca7f34329d6f7994cd5b0f7 Mon Sep 17 00:00:00 2001 From: NelsonTEJO Date: Thu, 13 Nov 2025 18:43:09 +0100 Subject: [PATCH 6/9] nouvelle modif --- sklearn_questions.py | 8 -------- 1 file changed, 8 deletions(-) diff --git a/sklearn_questions.py b/sklearn_questions.py index 9135a9f2..e135cb88 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -55,8 +55,6 @@ def fit(self, X, y): self.classes_ = np.unique(y) self.n_features_in_ = X.shape[1] - # XXX fix - self.X_train_ = X self.y_train_ = y @@ -83,13 +81,9 @@ def predict(self, X): dtype=self.classes_.dtype ) - # XXX fix for i, x in enumerate(X): - distances = np.sqrt(np.sum((self.X_train_ - x) ** 2, axis=1)) - idx_min = np.argmin(distances) - y_pred[i] = self.y_train_[idx_min] return y_pred @@ -112,6 +106,4 @@ def score(self, X, y): X, y = check_X_y(X, y) y_pred = self.predict(X) - # XXX fix - return float(np.mean(y_pred == y)) From 69933480340c52c6458dba3f22fc775aa26f32ff Mon Sep 17 00:00:00 2001 From: NelsonTEJO Date: Thu, 13 Nov 2025 18:46:18 +0100 Subject: [PATCH 7/9] nouvelle modif --- sklearn_questions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sklearn_questions.py b/sklearn_questions.py index e135cb88..9ed375c9 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -48,7 +48,7 @@ def fit(self, X, y): Returns ------- self : object - + """ X, y = check_X_y(X, y) check_classification_targets(y) From d3c6a35b4b0ea6aa532d95b189135e5182af2a9a Mon Sep 17 00:00:00 2001 From: NelsonTEJO Date: Thu, 13 Nov 2025 18:48:17 +0100 Subject: [PATCH 8/9] nouvelle modif --- sklearn_questions.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/sklearn_questions.py b/sklearn_questions.py index 9ed375c9..a66c3ad4 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -36,7 +36,7 @@ def __init__(self): # noqa: D107 def fit(self, X, y): """ - Train the OneNearestNeighbor predictor. + Train the OneNearestNeighbor predictor. Parameters ---------- @@ -48,7 +48,6 @@ def fit(self, X, y): Returns ------- self : object - """ X, y = check_X_y(X, y) check_classification_targets(y) @@ -72,7 +71,7 @@ def predict(self, X): Returns ------- y_pred : ndarray of shape (n_samples,) - predictions + predictions """ check_is_fitted(self) X = check_array(X) From eda87c384cad08b37cee0e8c62d29d8b75e0e3e7 Mon Sep 17 00:00:00 2001 From: NelsonTEJO Date: Thu, 13 Nov 2025 18:50:29 +0100 Subject: [PATCH 9/9] nouvelle modif --- sklearn_questions.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sklearn_questions.py b/sklearn_questions.py index a66c3ad4..ee07880f 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -29,7 +29,7 @@ class OneNearestNeighbor(BaseEstimator, ClassifierMixin): - "OneNearestNeighbor classifier." + """OneNearestNeighbor classifier.""" def __init__(self): # noqa: D107 pass @@ -89,7 +89,7 @@ def predict(self, X): def score(self, X, y): """ - Evaluate the performance of the model + Evaluate the performance of the model. Parameters ----------