From 7599aced323fff85fbfb23816331c7be4a2bcab2 Mon Sep 17 00:00:00 2001 From: Margaux Jacobelli Date: Tue, 10 Nov 2026 16:04:47 +0100 Subject: [PATCH 1/4] changes to the questions files --- numpy_questions.py | 21 ++++++++++++++- sklearn_questions.py | 64 ++++++++++++++++++++++++++++++++++++-------- 2 files changed, 73 insertions(+), 12 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index 21fcec4b..325b6035 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -42,6 +42,16 @@ def max_index(X): # TODO + if not isinstance(X, np.ndarray): + raise ValueError("The input is not a numpy array.") + if X.ndim != 2: + raise ValueError("The shape is not 2D") + + for a in range (X.shape[0]): + for b in range (X.shape[1]): + if X[a, b] > X[i, j]: + i, j = a, b + return i, j @@ -64,4 +74,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. + + if n_terms == 0: + return 1 + + product = 1 + for i in range(1, n_terms + 1): + four_n_squared = 4 * i * i + product *= four_n_squared / (four_n_squared -1) + + return 2 * product diff --git a/sklearn_questions.py b/sklearn_questions.py index f65038c6..04afc7e6 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -28,29 +28,49 @@ from sklearn.utils.multiclass import check_classification_targets -class OneNearestNeighbor(BaseEstimator, ClassifierMixin): +class OneNearestNeighbor(ClassifierMixin, BaseEstimator): "OneNearestNeighbor classifier." def __init__(self): # noqa: D107 pass def fit(self, X, y): - """Write docstring. - - And describe parameters + """Stores the training data to fit the classifier. + + Parameters + ---------- + X = ndarray of shape (n_samples, n_features) + The training sample, input array. + + y = ndarray of shape (n_samples,) + The training target, input array. + + Returns + ------- + self : OneNearestNeighbor """ 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_train_ = X + self.y_train_ = y return self def predict(self, X): - """Write docstring. + """Predicts the target of each sample. + + Parameters + ---------- + X = ndarray of shape (n_samples, n_features) + The training sample, input array. + + Returns + ------- + y_pred : nd array of shape (n_samples, ) + The target of the closest training sample for each sample. - And describe parameters """ check_is_fitted(self) X = check_array(X) @@ -60,15 +80,37 @@ def predict(self, X): ) # XXX fix + + if X.shape[1] != self.n_features_in_: + raise ValueError( + f"X has {X.shape[1]} features, but " + f"{self.__class__.__name__} is expecting " + f"{self.n_features_in_} features as input" + ) + for i, x in enumerate(X): + distances = np.sum((self.X_train_ - x) ** 2, axis=1) + y_pred[i] = self.y_train_[np.argmin(distances)] + return y_pred def score(self, X, y): - """Write docstring. - - And describe parameters + """Computes the accuracy of the model. + + Parameters + ---------- + X = ndarray of shape (n_samples, n_features) + The training sample, input array. + + y = ndarray of shape (n_samples,) + The training target, input array. + + Returns + ------- + score : float + The fraction of the samples correctly classified """ X, y = check_X_y(X, y) y_pred = self.predict(X) # XXX fix - return y_pred.sum() + return np.mean(y_pred == y) From fcca4f0c7790383f2ec722133c8d4e647ad1e7e2 Mon Sep 17 00:00:00 2001 From: Margaux Jacobelli Date: Tue, 10 Nov 2026 16:25:20 +0100 Subject: [PATCH 2/4] answers to the questions sets --- numpy_questions.py | 18 +++++++++--------- sklearn_questions.py | 7 +++---- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index 325b6035..a01969b6 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -44,14 +44,14 @@ def max_index(X): if not isinstance(X, np.ndarray): raise ValueError("The input is not a numpy array.") - if X.ndim != 2: + if X.ndim != 2: raise ValueError("The shape is not 2D") - for a in range (X.shape[0]): - for b in range (X.shape[1]): + for a in range(X.shape[0]): + for b in range(X.shape[1]): if X[a, b] > X[i, j]: i, j = a, b - + return i, j @@ -77,10 +77,10 @@ def wallis_product(n_terms): if n_terms == 0: return 1 - + product = 1 - for i in range(1, n_terms + 1): + for i in range(1, n_terms + 1): four_n_squared = 4 * i * i - product *= four_n_squared / (four_n_squared -1) - - return 2 * product + product *= four_n_squared / (four_n_squared -1 ) + + return 2 * product \ No newline at end of file diff --git a/sklearn_questions.py b/sklearn_questions.py index 4ee906d4..55f3914d 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -25,7 +25,6 @@ from sklearn.utils.validation import check_X_y from sklearn.utils.validation import check_array from sklearn.utils.validation import check_is_fitted -from sklearn.utils.validation import validate_data from sklearn.utils.multiclass import check_classification_targets @@ -45,7 +44,7 @@ def fit(self, X, y): y = ndarray of shape (n_samples,) The training target, input array. - + Returns ------- self : OneNearestNeighbor @@ -101,14 +100,14 @@ def score(self, X, y): ---------- X = ndarray of shape (n_samples, n_features) The training sample, input array. - + y = ndarray of shape (n_samples,) The training target, input array. Returns ------- score : float - The fraction of the samples correctly classified + The fraction of the samples correctly classified """ X, y = check_X_y(X, y) y_pred = self.predict(X) From 0408f4da23f0db3d2a891e0156432527b56ccd69 Mon Sep 17 00:00:00 2001 From: Margaux Jacobelli Date: Tue, 10 Nov 2026 16:28:18 +0100 Subject: [PATCH 3/4] answers to the questions sets --- numpy_questions.py | 4 ++-- sklearn_questions.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index a01969b6..90f64811 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -81,6 +81,6 @@ def wallis_product(n_terms): product = 1 for i in range(1, n_terms + 1): four_n_squared = 4 * i * i - product *= four_n_squared / (four_n_squared -1 ) + product *= four_n_squared / (four_n_squared - 1) - return 2 * product \ No newline at end of file + return 2 * product diff --git a/sklearn_questions.py b/sklearn_questions.py index 55f3914d..0aaee7e1 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -44,7 +44,7 @@ def fit(self, X, y): y = ndarray of shape (n_samples,) The training target, input array. - + Returns ------- self : OneNearestNeighbor From ae974fbb070583374700c2b6f1c4cb16eed3c8b6 Mon Sep 17 00:00:00 2001 From: Margaux Jacobelli Date: Tue, 10 Nov 2026 16:40:06 +0100 Subject: [PATCH 4/4] answers to the questions sets --- sklearn_questions.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/sklearn_questions.py b/sklearn_questions.py index 0aaee7e1..424df9de 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -29,13 +29,17 @@ class OneNearestNeighbor(ClassifierMixin, BaseEstimator): - "OneNearestNeighbor classifier." + """OneNearestNeighbor classifier. + + Predict for each sample the target of the closest training sample, + using the Euclidean distance. + """ def __init__(self): # noqa: D107 pass def fit(self, X, y): - """Stores the training data to fit the classifier. + """Store the training data to fit the classifier. Parameters ---------- @@ -59,7 +63,7 @@ def fit(self, X, y): return self def predict(self, X): - """Predicts the target of each sample. + """Predict the target of each sample. Parameters ---------- @@ -94,7 +98,7 @@ def predict(self, X): return y_pred def score(self, X, y): - """Computes the accuracy of the model. + """Compute the accuracy of the model. Parameters ----------