From 43e5e052e3f9e4f60932a03c4c8f128cdf1f52b9 Mon Sep 17 00:00:00 2001 From: Blanche Date: Tue, 10 Nov 2026 15:56:05 +0100 Subject: [PATCH 1/6] Assignment Blanche Delaunay --- numpy_questions.py | 16 +++++++-- sklearn_questions.py | 84 +++++++++++++++++++++++++++++++++++--------- 2 files changed, 81 insertions(+), 19 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index 21fcec4b..47ec78a5 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -40,7 +40,11 @@ def max_index(X): i = 0 j = 0 - # TODO + if not isinstance(X, np.ndarray) or X.ndim != 2: + raise ValueError("Input must be a 2D numpy array.") + + max_array = np.argmax(X) + i, j = np.unravel_index(max_array, X.shape) return i, j @@ -62,6 +66,14 @@ def wallis_product(n_terms): pi : float The approximation of order `n_terms` of pi using the Wallis product. """ + if not isinstance(n_terms, int) or n_terms < 0: + raise ValueError("n_terms must be a non-negative integer.") + if n_terms == 0: + return 1.0 + k = np.arange(1, n_terms + 1) + product = np.prod((4 * k**2) / (4 * k**2 - 1)) + pi_approximation = 2 * product + # XXX : The n_terms is an int that corresponds to the number of # terms in the product. For example 10000. - return 0. + return pi_approximation diff --git a/sklearn_questions.py b/sklearn_questions.py index f65038c6..cb5b5c3d 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -22,53 +22,103 @@ import numpy as np from sklearn.base import BaseEstimator from sklearn.base import ClassifierMixin -from sklearn.utils.validation import check_X_y -from sklearn.utils.validation import check_array +from sklearn.metrics.pairwise import pairwise_distances +from sklearn.utils.validation import validate_data from sklearn.utils.validation import check_is_fitted from sklearn.utils.multiclass import check_classification_targets -class OneNearestNeighbor(BaseEstimator, ClassifierMixin): - "OneNearestNeighbor classifier." +class OneNearestNeighbor(ClassifierMixin, BaseEstimator): + """ OneNearestNeighbor classifier. + + Parameters + ---------- + None + + Attributes + ---------- + classes_ : ndarray of shape (n_classes,) + The unique class labels observed during fitting. + n_features_in_ : int + Number of features seen during ``fit``. + X_ : ndarray of shape (n_samples, n_features) + The training input samples. + y_ : ndarray of shape (n_samples,) + The target values corresponding to training samples. + """ def __init__(self): # noqa: D107 pass def fit(self, X, y): - """Write docstring. - - And describe parameters + """Fit the OneNearestNeighbor classifier from the training dataset. + + Parameters + ---------- + X : ndarray of shape (n_samples, n_features) + Training data. + y : ndarray of shape (n_samples,) + Target values. + + Returns + ------- + self : OneNearestNeighbor + The fitted estimator. """ - X, y = check_X_y(X, y) + X, y = validate_data(self, X=X, y=y) check_classification_targets(y) self.classes_ = np.unique(y) - self.n_features_in_ = X.shape[1] + + self.X_ = X + self.y_ = y # XXX fix return self def predict(self, X): - """Write docstring. + """Predict the class labels for the provided data. + + Parameters + ---------- + X : ndarray of shape (n_samples, n_features) + Test samples. - And describe parameters + Returns + ------- + y_pred : ndarray of shape (n_samples,) + Class labels for each data sample. """ check_is_fitted(self) - X = check_array(X) + X = validate_data(self, X=X, reset=False) y_pred = np.full( shape=len(X), fill_value=self.classes_[0], dtype=self.classes_.dtype ) + distances = pairwise_distances(X, self.X_, metric='euclidean') + closest_indices = np.argmin(distances, axis=1) + y_pred[:] = self.y_[closest_indices] # XXX fix return y_pred def score(self, X, y): - """Write docstring. - - And describe parameters + """Return the mean accuracy on the given test data and labels. + + Parameters + ---------- + X : array-like of shape (n_samples, n_features) + Test samples. + y : array-like of shape (n_samples,) + True labels for X. + + Returns + ------- + score : float + Mean accuracy of self.predict(X) with respect to y. """ - X, y = check_X_y(X, y) + X, y = validate_data(self, X=X, y=y, reset=False) y_pred = self.predict(X) + y_score = (y_pred == y).sum() / len(y) # XXX fix - return y_pred.sum() + return y_score From 6b3ff97c3245f8f7bc154769287819622bef68a4 Mon Sep 17 00:00:00 2001 From: Blanche Date: Tue, 10 Nov 2026 16:10:28 +0100 Subject: [PATCH 2/6] Assignment Blanche Delaunay --- numpy_questions.py | 1 - 1 file changed, 1 deletion(-) diff --git a/numpy_questions.py b/numpy_questions.py index 47ec78a5..f01c2eb8 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -73,7 +73,6 @@ def wallis_product(n_terms): k = np.arange(1, n_terms + 1) product = np.prod((4 * k**2) / (4 * k**2 - 1)) pi_approximation = 2 * product - # XXX : The n_terms is an int that corresponds to the number of # terms in the product. For example 10000. return pi_approximation From 7d02e2c9f76c4f61ec6f6c03a1914500e4c7963d Mon Sep 17 00:00:00 2001 From: Blanche Date: Tue, 10 Nov 2026 16:15:46 +0100 Subject: [PATCH 3/6] Assignment Blanche Delaunay --- sklearn_questions.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/sklearn_questions.py b/sklearn_questions.py index 15b8787f..856ab9cd 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -46,7 +46,6 @@ class OneNearestNeighbor(ClassifierMixin, BaseEstimator): y_ : ndarray of shape (n_samples,) The target values corresponding to training samples. """ - def __init__(self): # noqa: D107 pass @@ -71,7 +70,6 @@ def fit(self, X, y): self.X_ = X self.y_ = y - # XXX fix return self @@ -97,7 +95,6 @@ def predict(self, X): distances = pairwise_distances(X, self.X_, metric='euclidean') closest_indices = np.argmin(distances, axis=1) y_pred[:] = self.y_[closest_indices] - # XXX fix return y_pred @@ -119,6 +116,5 @@ def score(self, X, y): X, y = validate_data(self, X=X, y=y, reset=False) y_pred = self.predict(X) y_score = (y_pred == y).sum() / len(y) - # XXX fix return y_score From a52fa9ace22b5980d17fd631e56b54c226241f92 Mon Sep 17 00:00:00 2001 From: Blanche Date: Tue, 10 Nov 2026 16:20:30 +0100 Subject: [PATCH 4/6] Assignment Blanche Delaunay --- sklearn_questions.py | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/sklearn_questions.py b/sklearn_questions.py index 856ab9cd..8a198d0b 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -20,16 +20,14 @@ `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 +from sklearn.base import BaseEstimator, ClassifierMixin from sklearn.metrics.pairwise import pairwise_distances -from sklearn.utils.validation import check_is_fitted -from sklearn.utils.validation import validate_data from sklearn.utils.multiclass import check_classification_targets +from sklearn.utils.validation import check_is_fitted, validate_data class OneNearestNeighbor(ClassifierMixin, BaseEstimator): - """ OneNearestNeighbor classifier. + """OneNearestNeighbor classifier. Parameters ---------- @@ -46,6 +44,7 @@ class OneNearestNeighbor(ClassifierMixin, BaseEstimator): y_ : ndarray of shape (n_samples,) The target values corresponding to training samples. """ + def __init__(self): # noqa: D107 pass @@ -70,7 +69,7 @@ def fit(self, X, y): self.X_ = X self.y_ = y - # XXX fix + return self def predict(self, X): @@ -95,7 +94,7 @@ def predict(self, X): distances = pairwise_distances(X, self.X_, metric='euclidean') closest_indices = np.argmin(distances, axis=1) y_pred[:] = self.y_[closest_indices] - # XXX fix + return y_pred def score(self, X, y): @@ -116,5 +115,5 @@ def score(self, X, y): X, y = validate_data(self, X=X, y=y, reset=False) y_pred = self.predict(X) y_score = (y_pred == y).sum() / len(y) - # XXX fix - return y_score + + return y_score \ No newline at end of file From cab780837265b626670f1838a8a645e2343ce49e Mon Sep 17 00:00:00 2001 From: Blanche Date: Tue, 10 Nov 2026 16:22:54 +0100 Subject: [PATCH 5/6] Assignment Blanche Delaunay --- sklearn_questions.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sklearn_questions.py b/sklearn_questions.py index 8a198d0b..d757603c 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -116,4 +116,5 @@ def score(self, X, y): y_pred = self.predict(X) y_score = (y_pred == y).sum() / len(y) - return y_score \ No newline at end of file + return y_score + \ No newline at end of file From 8e472af5239f53070184568e4df8edf8ffe3c51a Mon Sep 17 00:00:00 2001 From: Blanche Date: Tue, 10 Nov 2026 16:28:21 +0100 Subject: [PATCH 6/6] Assignment Blanche Delaunay --- sklearn_questions.py | 1 - 1 file changed, 1 deletion(-) diff --git a/sklearn_questions.py b/sklearn_questions.py index d757603c..8fc4ee8f 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -117,4 +117,3 @@ def score(self, X, y): y_score = (y_pred == y).sum() / len(y) return y_score - \ No newline at end of file