From 32e6a52bb3a555700e1563bd46500dde04fae67b Mon Sep 17 00:00:00 2001 From: Alexandre-Le-Bacon Date: Fri, 14 Nov 2025 15:13:35 +0100 Subject: [PATCH 1/4] Alexandre Le Bacon assignment: numpy + sklearn solutions --- numpy_questions.py | 18 ++++++++- sklearn_questions.py | 95 +++++++++++++++++++++++++++++++++----------- 2 files changed, 88 insertions(+), 25 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index 21fcec4b..69d1cc07 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -40,7 +40,12 @@ def max_index(X): i = 0 j = 0 - # TODO + if not isinstance(X, np.ndarray): + raise ValueError("X must be a numpy ndarray.") + if X.ndim != 2: + raise ValueError("X must be a 2D array.") + flat_index = np.argmax(X) + i, j = np.unravel_index(flat_index, X.shape) return i, j @@ -64,4 +69,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: + raise ValueError("The number of terms must be positive") + if n_terms == 0: + return 1.0 + n = np.arange(1, n_terms+1, dtype=float) + terms = 4*n**2/(4*n**2-1) + product = np.prod(terms) + pi_approx = 2*product + return pi_approx diff --git a/sklearn_questions.py b/sklearn_questions.py index f65038c6..c49181d2 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -20,55 +20,104 @@ `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.utils.validation import check_X_y -from sklearn.utils.validation import check_array -from sklearn.utils.validation import check_is_fitted +from sklearn.base import BaseEstimator, ClassifierMixin +from sklearn.utils.validation import check_X_y, check_is_fitted, validate_data from sklearn.utils.multiclass import check_classification_targets -class OneNearestNeighbor(BaseEstimator, ClassifierMixin): - "OneNearestNeighbor classifier." +class OneNearestNeighbor(ClassifierMixin, BaseEstimator): + """One-nearest-neighbor classifier.""" def __init__(self): # noqa: D107 pass def fit(self, X, y): - """Write docstring. - - And describe parameters + """Fit the OneNearestNeighbor classifier. + + This method stores the training data X and y inside the estimator + so that predictions can be made based on the nearest neighbor rule. + + Parameters + ---------- + X : ndarray of shape (n_samples, n_features) + Training data. + y : ndarray of shape (n_samples,) + Target labels corresponding to X. + + Returns + ------- + self : object + Fitted estimator. """ - X, y = check_X_y(X, y) + # I used validate_data to satisfy scikit-learn's estimator checks. + # It validates X and y and sets the n_features_in_ attribute, + # which is required by check_estimator() tests. + X, y = validate_data(self, 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 class labels for the given samples. + + For each sample in X, this method finds the closest training sample + stored during ``fit`` using the Euclidean distance, and returns its + corresponding label. - And describe parameters + Parameters + ---------- + X : array-like of shape (n_samples, n_features) + Input samples for which to predict class labels. + + Returns + ------- + y_pred : ndarray of shape (n_samples,) + Predicted class labels for each sample in X. """ check_is_fitted(self) - X = check_array(X) + # Ensure that X has the same number of features as during fit. + X = validate_data(self, X, reset=False) + 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, ) - # XXX fix + for idx, x_i in enumerate(X): + diff = self.X_ - x_i + distances = np.sqrt(np.sum(diff**2, axis=1)) + nearest = np.argmin(distances) + y_pred[idx] = self.y_[nearest] + return y_pred def score(self, X, y): - """Write docstring. - - And describe parameters + """Compute the accuracy of the classifier. + + This method compares the predicted labels for X with the true labels y + and returns the proportion of correctly classified samples. + + Parameters + ---------- + X : ndarray of shape (n_samples, n_features) + Test samples. + y : ndarray of shape (n_samples,) + True labels for X. + + Returns + ------- + accuracy : float + Mean accuracy of the classifier on the given test data. """ X, y = check_X_y(X, y) y_pred = self.predict(X) + correct = y_pred == y + accuracy = np.mean(correct) - # XXX fix - return y_pred.sum() + return accuracy From 0a23d6b9ae0b4e6412596c39690d8dec8a315016 Mon Sep 17 00:00:00 2001 From: Alexandre-Le-Bacon Date: Fri, 14 Nov 2025 15:25:29 +0100 Subject: [PATCH 2/4] Fix estimator to pass sklearn CI tests --- sklearn_questions.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/sklearn_questions.py b/sklearn_questions.py index c49181d2..add5b83b 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -21,7 +21,7 @@ """ import numpy as np from sklearn.base import BaseEstimator, ClassifierMixin -from sklearn.utils.validation import check_X_y, check_is_fitted, validate_data +from sklearn.utils.validation import check_X_y, check_is_fitted, check_array from sklearn.utils.multiclass import check_classification_targets @@ -49,10 +49,7 @@ def fit(self, X, y): self : object Fitted estimator. """ - # I used validate_data to satisfy scikit-learn's estimator checks. - # It validates X and y and sets the n_features_in_ attribute, - # which is required by check_estimator() tests. - X, y = validate_data(self, X, y) + X, y = check_X_y(X, y) check_classification_targets(y) self.classes_ = np.unique(y) self.n_features_in_ = X.shape[1] @@ -80,8 +77,13 @@ def predict(self, X): Predicted class labels for each sample in X. """ check_is_fitted(self) - # Ensure that X has the same number of features as during fit. - X = validate_data(self, X, reset=False) + X = check_array(X) + + if X.shape[1] != self.n_features_in_: + raise ValueError( + f"X has {X.shape[1]} features, but OneNearestNeighbor is expecting " + f"{self.n_features_in_} features as input." + ) y_pred = np.full( shape=len(X), @@ -120,4 +122,4 @@ def score(self, X, y): correct = y_pred == y accuracy = np.mean(correct) - return accuracy + return accuracy \ No newline at end of file From 5861f5587c76deed844be43ef21da075e9448917 Mon Sep 17 00:00:00 2001 From: Alexandre-Le-Bacon Date: Fri, 14 Nov 2025 15:28:31 +0100 Subject: [PATCH 3/4] Fix flake8 style issues --- sklearn_questions.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/sklearn_questions.py b/sklearn_questions.py index add5b83b..e716b970 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -81,9 +81,9 @@ def predict(self, X): if X.shape[1] != self.n_features_in_: raise ValueError( - f"X has {X.shape[1]} features, but OneNearestNeighbor is expecting " - f"{self.n_features_in_} features as input." - ) + 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), @@ -122,4 +122,5 @@ def score(self, X, y): correct = y_pred == y accuracy = np.mean(correct) - return accuracy \ No newline at end of file + return accuracy + \ No newline at end of file From 745c5a0bcdf4e06f58169e6ab7587f8dfaff11bf Mon Sep 17 00:00:00 2001 From: Alexandre-Le-Bacon Date: Fri, 14 Nov 2025 15:32:59 +0100 Subject: [PATCH 4/4] Final clean version of OneNearestNeighbor (flake8 + CI compliant) --- sklearn_questions.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/sklearn_questions.py b/sklearn_questions.py index e716b970..d8f893fa 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -83,7 +83,7 @@ def predict(self, X): 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), @@ -121,6 +121,4 @@ def score(self, X, y): y_pred = self.predict(X) correct = y_pred == y accuracy = np.mean(correct) - return accuracy - \ No newline at end of file