From ab2c8e0982803c3026fd0af81d58f9be3cb07513 Mon Sep 17 00:00:00 2001 From: Victor Piana Date: Tue, 10 Nov 2026 16:26:25 +0100 Subject: [PATCH 1/2] Lab4 ended Victor Piana --- numpy_questions.py | 24 ++++++++++++------- sklearn_questions.py | 55 +++++++++++++++++++++++++++++++++++--------- 2 files changed, 60 insertions(+), 19 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index 21fcec4b..18b83e4b 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -37,17 +37,19 @@ 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("L'entrée doit être un tableau numpy") - # TODO + if X.ndim != 2: + raise ValueError("Le tableau doit être de dimension 2") - return i, j + i, j = np.unravel_index(np.argmax(X), X.shape) + + return int(i), int(j) def wallis_product(n_terms): """Implement the Wallis product to compute an approximation of pi. - See: https://en.wikipedia.org/wiki/Wallis_product @@ -62,6 +64,12 @@ 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.0 + + n = np.arange(1, n_terms + 1, dtype=float) + + # Formule mathématique simplifiée du terme n : (4n^2) / (4n^2 - 1) + produit = np.prod((4 * n**2) / (4 * n**2 - 1)) + + return float(2.0 * produit) diff --git a/sklearn_questions.py b/sklearn_questions.py index f65038c6..795c3442 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -35,22 +35,41 @@ def __init__(self): # noqa: D107 pass def fit(self, X, y): - """Write docstring. - - And describe parameters + """ Fit the OneNearestNeighbor model from the training dataset. + + Parameters + ---------- + X : ndarray of shape (n_samples, n_features) + contains the X features of the training set + y : ndarray of shape (n_samples,) + contains the target values of each line + + 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 value of y for the X sample. + + Parameters + ---------- + X : ndarray of shape (n_samples, n_features) + The input samples. - And describe parameters + Returns + ------- + y_pred : ndarray of shape (n_samples,) + Predicted value of y for the X sample. """ check_is_fitted(self) X = check_array(X) @@ -59,16 +78,30 @@ def predict(self, X): dtype=self.classes_.dtype ) - # XXX fix + for i, x_i in enumerate(X): + distance = np.linalg.norm(self.X_ - x_i, axis=1) + min_distance_ind = np.argmin(distance) + y_pred[i] = self.y_[min_distance_ind] + return y_pred def score(self, X, y): - """Write docstring. + """Give a score to our model. + + Parameters + ---------- + X : ndarray of shape (n_samples, n_features) + Contains the X features of the test set. + + y : ndarray of shape (n_samples,) + Contains the target value we want to reach. - And describe parameters + Returns + ------- + score : float + Score of the model. """ X, y = check_X_y(X, y) y_pred = self.predict(X) - # XXX fix - return y_pred.sum() + return float(np.mean(y_pred == y)) From cfc609059b138da08fb5f34b0444b8d1b40122f5 Mon Sep 17 00:00:00 2001 From: Victor Piana Date: Tue, 10 Nov 2026 16:46:23 +0100 Subject: [PATCH 2/2] fix errors --- numpy_questions.py | 1 + sklearn_questions.py | 35 ++++++++++++++++++++--------------- 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index 18b83e4b..3d82794c 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -50,6 +50,7 @@ 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 diff --git a/sklearn_questions.py b/sklearn_questions.py index 795c3442..4f09c51c 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -28,26 +28,26 @@ from sklearn.utils.multiclass import check_classification_targets -class OneNearestNeighbor(BaseEstimator, ClassifierMixin): - "OneNearestNeighbor classifier." +class OneNearestNeighbor(ClassifierMixin, BaseEstimator): + """OneNearestNeighbor classifier.""" def __init__(self): # noqa: D107 pass def fit(self, X, y): - """ Fit the OneNearestNeighbor model from the training dataset. - - Parameters - ---------- - X : ndarray of shape (n_samples, n_features) - contains the X features of the training set - y : ndarray of shape (n_samples,) - contains the target values of each line - - Returns - ------- - self : OneNearestNeighbor - The fitted estimator + """Fit the OneNearestNeighbor model from the training dataset. + + Parameters + ---------- + X : ndarray of shape (n_samples, n_features) + contains the X features of the training set + y : ndarray of shape (n_samples,) + contains the target values of each line + + Returns + ------- + self : OneNearestNeighbor + The fitted estimator """ X, y = check_X_y(X, y) check_classification_targets(y) @@ -73,6 +73,11 @@ def predict(self, X): """ check_is_fitted(self) X = check_array(X) + if X.shape[1] != self.n_features_in_: + raise ValueError( + f"X has {X.shape[1]} features, but OneNearestNeighbor " + f"is expecting {self.n_features_in_} features as input." + ) y_pred = np.full( shape=len(X), fill_value=self.classes_[0], dtype=self.classes_.dtype