From 227ce9d692dc1f9fb4dd6b2680ed3ca8ab5ad216 Mon Sep 17 00:00:00 2001 From: Camille Valenciennes Date: Tue, 10 Nov 2026 18:01:52 +0100 Subject: [PATCH 1/3] Implement numpy and sklearn --- numpy_questions.py | 22 ++++++++++++--- sklearn_questions.py | 67 +++++++++++++++++++++++++++++++++++--------- 2 files changed, 71 insertions(+), 18 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index 21fcec4b..924edc3f 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -40,7 +40,15 @@ def max_index(X): i = 0 j = 0 - # TODO + if not isinstance(X, np.ndarray): + raise ValueError("Only Numpy arrays are allowed") + elif X.ndim != 2: + raise ValueError("Only 2D arrays are allowed") + elif X.size == 0: + raise ValueError("Only non-empty arrays are allowed") + + i, j = np.unravel_index(X.argmax(), X.shape) + i, j = int(i), int(j) return i, j @@ -62,6 +70,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: + raise ValueError("n_terms must be non-negative") + if n_terms == 0: + return 1 + + squares = 4 * np.arange(1, n_terms+1) ** 2 + prod = np.prod(squares / (squares - 1)) + + return prod * 2 diff --git a/sklearn_questions.py b/sklearn_questions.py index f65038c6..4754a26c 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -28,29 +28,47 @@ 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 + """Fit the one-nearest-neighbor classifier. + + Parameters + ---------- + X : ndarray of shape (n_samples, n_features) + The training samples. + y : ndarray of shape (n_samples, ) + The training labels. + + 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_train_ = X + self.y_train_ = y return self def predict(self, X): - """Write docstring. + """Predict the class of each sample of X. + + Parameters + ---------- + X : ndarray of shape (n_samples, n_features) + The samples to classify. - And describe parameters + Returns + ------- + y_pred : ndarray of shape (p_samples, ) + The predicted class of each sample of X. """ check_is_fitted(self) X = check_array(X) @@ -59,16 +77,37 @@ def predict(self, X): dtype=self.classes_.dtype ) - # 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" + ) + + diff = X[:, np.newaxis, :] - self.X_train_[np.newaxis, :, :] + dist = np.linalg.norm(diff, axis=2) + nearest = dist.argmin(axis=1) + y_pred = self.y_train_[nearest] + return y_pred def score(self, X, y): - """Write docstring. - - And describe parameters + """Return the accuracy of the classifier on (X,y). + + Parameters + ---------- + X : ndarray of shape (n_samples, n_features) + The test samples. + y : ndarray of shape (n_samples, ) + The test labels. + + Returns + ------- + score : float + The fraction of samples accurately classified. """ X, y = check_X_y(X, y) y_pred = self.predict(X) - # XXX fix - return y_pred.sum() + y_pred = y_pred == y + return y_pred.mean() From d1df2540ee9b43a073bc5ffe04140a83b1d34c18 Mon Sep 17 00:00:00 2001 From: Camille Valenciennes Date: Wed, 11 Nov 2026 10:18:36 +0100 Subject: [PATCH 2/3] Fix sklearn_questions --- sklearn_questions.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/sklearn_questions.py b/sklearn_questions.py index ec5c7280..e9eb4ed1 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -25,11 +25,10 @@ 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 -class OneNearestNeighbor(ClassifierMixin, BaseEstimator): +class OneNearestNeighbor(BaseEstimator, ClassifierMixin): "OneNearestNeighbor classifier." def __init__(self): # noqa: D107 From cc19af7738949f2f8f1da86343ada9fc4ab5012c Mon Sep 17 00:00:00 2001 From: Camille Valenciennes Date: Wed, 11 Nov 2026 10:21:58 +0100 Subject: [PATCH 3/3] Fix sklearn_questions --- sklearn_questions.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sklearn_questions.py b/sklearn_questions.py index e9eb4ed1..f55b09cf 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -28,8 +28,8 @@ 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