From af0b51b6513b3db369906e97e01c67ecdd49f4d2 Mon Sep 17 00:00:00 2001 From: Nikolai Date: Thu, 13 Nov 2025 23:47:05 +0100 Subject: [PATCH 1/3] Fixed pytest and flake8 errors --- numpy_questions.py | 30 +++++++++++++--- sklearn_questions.py | 83 +++++++++++++++++++++++++++++++++----------- 2 files changed, 88 insertions(+), 25 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index 21fcec4b..46957746 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -15,6 +15,7 @@ This will be enforced with `flake8`. You can check that there is no flake8 errors by calling `flake8` at the root of the repo. """ + import numpy as np @@ -37,12 +38,18 @@ def max_index(X): If the input is not a numpy array or if the shape is not 2D. """ - i = 0 - j = 0 - # TODO + if not isinstance(X, np.ndarray): + raise ValueError("Input is not a numpy array") + + if X.ndim != 2: + raise ValueError("Input must be 2D") + + idx_1d = np.argmax(X) - return i, j + (i, j) = np.unravel_index(idx_1d, X.shape) + + return (i, j) def wallis_product(n_terms): @@ -62,6 +69,19 @@ 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. + + result = 1 + + if n_terms == 0: + pass + + else: + for n in range(1, n_terms + 1): + result *= 4 * n**2 / (4 * n**2 - 1) + + result *= 2 + + return result diff --git a/sklearn_questions.py b/sklearn_questions.py index f65038c6..6ca8a01c 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -19,6 +19,7 @@ for the methods you code and for the class. The docstring will be checked using `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 @@ -28,47 +29,89 @@ from sklearn.utils.multiclass import check_classification_targets -class OneNearestNeighbor(BaseEstimator, ClassifierMixin): - "OneNearestNeighbor classifier." +class OneNearestNeighbor(ClassifierMixin, BaseEstimator): + """ + OneNearestNeighbor Classifier. + Estimator that implements the 1-Nearest Neighbor algorithm to predict a new + sample's label based on the closest training sample. + + Parameters + ---------- + None + """ + + def __init__(self): + """ + Init function. + + Returns + ------- + None. - def __init__(self): # noqa: D107 + """ pass def fit(self, X, y): - """Write docstring. - - And describe parameters + """ + Parameters + ---------- + X: array of shape (n_samples, n_features) + y: array of shape (n_samples,), holds the labels to predict + + Returns + ------- + Self: itself """ X, y = check_X_y(X, y) check_classification_targets(y) - self.classes_ = np.unique(y) + self.n_features_in_ = X.shape[1] + self.X_ = X + self.y_ = y + self.classes_ = np.unique(y) - # XXX fix return self def predict(self, X): - """Write docstring. + """ + Parameters + ---------- + X: array of shape (n_samples, n_features) - And describe parameters + Returns + ------- + y_pred: the predicted labels for each sample in X """ check_is_fitted(self) X = check_array(X) - y_pred = np.full( - shape=len(X), fill_value=self.classes_[0], - dtype=self.classes_.dtype - ) - # XXX fix + 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." + ) + + difference = X[:, None, :] - self.X_[None, :, :] + distances = np.linalg.norm(difference, axis=2) + + closest_indx = np.argmin(distances, axis=1) + y_pred = self.y_[closest_indx] + return y_pred def score(self, X, y): - """Write docstring. - - And describe parameters + """ + Parameters + ---------- + X: array of shape (n_samples, n_features) + y: array of shape (n_samples,), holds the labels to predict + + Returns + ------- + score: float, + the mean accuracy of the prediction against the true labels y """ 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 84b9340d5a3a95bae27bbab9d79b9d38845a3f56 Mon Sep 17 00:00:00 2001 From: Nikolai Date: Thu, 13 Nov 2025 23:57:10 +0100 Subject: [PATCH 2/3] Fixed pydocstyle errors --- numpy_questions.py | 5 ----- sklearn_questions.py | 8 ++++++-- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index 46957746..ac5115e4 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -38,7 +38,6 @@ def max_index(X): If the input is not a numpy array or if the shape is not 2D. """ - if not isinstance(X, np.ndarray): raise ValueError("Input is not a numpy array") @@ -69,10 +68,6 @@ 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. - result = 1 if n_terms == 0: diff --git a/sklearn_questions.py b/sklearn_questions.py index 6ca8a01c..151c6176 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -30,8 +30,8 @@ class OneNearestNeighbor(ClassifierMixin, BaseEstimator): - """ - OneNearestNeighbor Classifier. + """OneNearestNeighbor Classifier. + Estimator that implements the 1-Nearest Neighbor algorithm to predict a new sample's label based on the closest training sample. @@ -42,6 +42,7 @@ class OneNearestNeighbor(ClassifierMixin, BaseEstimator): def __init__(self): """ + Init function. Returns @@ -53,6 +54,7 @@ def __init__(self): def fit(self, X, y): """ + Parameters ---------- X: array of shape (n_samples, n_features) @@ -74,6 +76,7 @@ def fit(self, X, y): def predict(self, X): """ + Parameters ---------- X: array of shape (n_samples, n_features) @@ -101,6 +104,7 @@ def predict(self, X): def score(self, X, y): """ + Parameters ---------- X: array of shape (n_samples, n_features) From 6fc0bf17d4dee02e3f79167c4adf15d17786e5b2 Mon Sep 17 00:00:00 2001 From: Nikolai Date: Fri, 14 Nov 2025 00:04:04 +0100 Subject: [PATCH 3/3] Fixed pydocstyle errors 2 --- sklearn_questions.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/sklearn_questions.py b/sklearn_questions.py index 151c6176..6994d950 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -53,7 +53,7 @@ def __init__(self): pass def fit(self, X, y): - """ + """Fit function. Parameters ---------- @@ -75,7 +75,7 @@ def fit(self, X, y): return self def predict(self, X): - """ + """Predict function. Parameters ---------- @@ -103,7 +103,7 @@ def predict(self, X): return y_pred def score(self, X, y): - """ + """Scoring function. Parameters ----------