From ad5326fd0b16d5928f4d2904defacf6fa23bdf68 Mon Sep 17 00:00:00 2001 From: MaximeDespreaux Date: Thu, 13 Nov 2025 16:31:15 +0100 Subject: [PATCH 1/5] Finished Assignment + Sanity CI check --- numpy_questions.py | 22 ++++++++++++--- sklearn_questions.py | 66 ++++++++++++++++++++++++++++++++++---------- 2 files changed, 70 insertions(+), 18 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index 21fcec4b..97a7edfc 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -40,7 +40,14 @@ def max_index(X): i = 0 j = 0 - # TODO + if not isinstance(X, np.ndarray): + raise ValueError("Input should be a numpy array.") + + if X.ndim != 2: + raise ValueError("Input array should be 2D.") + + flat_idx = np.argmax(X) + i, j = flat_idx // X.shape[1], flat_idx % X.shape[1] return i, j @@ -62,6 +69,13 @@ 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. + + pi = 2. + + if n_terms == 0: + return 1. + + for n in range(1, n_terms + 1): + pi *= (4 * n ** 2) / (4 * n ** 2 - 1) + + return pi diff --git a/sklearn_questions.py b/sklearn_questions.py index f65038c6..b6ecdd97 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -35,40 +35,78 @@ def __init__(self): # noqa: D107 pass def fit(self, X, y): - """Write docstring. - - And describe parameters + """ + Checks X and y for consistent length, enforces X to be 2D and y 1D. + Ensure that target y is of a non-regression type. + The above was taken from sklearn's documentation. + + Fit the OneNearestNeighbor model according to the given training data. + Parameters: + X : ndarray of shape (n_samples, n_features) + Training data. + + y : ndarray of shape (n_samples,) + Target values. + + Returns: Model fit on the training data. """ 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_ = X + self.y_ = y + return self def predict(self, X): - """Write docstring. - - And describe parameters """ - check_is_fitted(self) + Checks that the model is fitted and X is 2D. + By default, the input is checked to be a non-empty 2D array containing + only finite values. + The above was taken from sklearn's documentation. + + Perform classification on test data X. + Parameters: + X : ndarray of shape (n_samples, n_features) + Test samples. + + Returns: + y_pred : ndarray of shape (n_samples,) + Predicted class labels for samples in X. + """ X = check_array(X) + check_is_fitted(self) + y_pred = np.full( shape=len(X), fill_value=self.classes_[0], dtype=self.classes_.dtype ) - # XXX fix + distances = np.linalg.norm(X[:, np.newaxis] - self.X_, axis=2) + nearest_neighbor_idx = np.argmin(distances, axis=1) + y_pred = self.y_[nearest_neighbor_idx] + return y_pred def score(self, X, y): - """Write docstring. - - And describe parameters + """ + Checks that the model is fitted and X and y have consistent length. + The above was taken from sklearn's documentation. + + Returns the accuracy on the given test data and labels. + Parameters: + X : ndarray of shape (n_samples, n_features) + Test samples. + + y : ndarray of shape (n_samples,) + True labels for X. + Returns: + score : float + Accuracy of self.predict(X) wrt. 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) \ No newline at end of file From 2979b7b1f51028ccee59e22b6c92b599a6c24742 Mon Sep 17 00:00:00 2001 From: MaximeDespreaux Date: Thu, 13 Nov 2025 16:39:49 +0100 Subject: [PATCH 2/5] Fixed Linting --- numpy_questions.py | 6 +++--- sklearn_questions.py | 16 ++++++++-------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index 97a7edfc..aac4cdcd 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -42,7 +42,7 @@ def max_index(X): if not isinstance(X, np.ndarray): raise ValueError("Input should be a numpy array.") - + if X.ndim != 2: raise ValueError("Input array should be 2D.") @@ -69,12 +69,12 @@ def wallis_product(n_terms): pi : float The approximation of order `n_terms` of pi using the Wallis product. """ - + pi = 2. if n_terms == 0: return 1. - + for n in range(1, n_terms + 1): pi *= (4 * n ** 2) / (4 * n ** 2 - 1) diff --git a/sklearn_questions.py b/sklearn_questions.py index b6ecdd97..3094ccc0 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -40,14 +40,14 @@ def fit(self, X, y): Ensure that target y is of a non-regression type. The above was taken from sklearn's documentation. - Fit the OneNearestNeighbor model according to the given training data. + Fit OneNearestNeighbor model according to the given training data. Parameters: X : ndarray of shape (n_samples, n_features) Training data. y : ndarray of shape (n_samples,) Target values. - + Returns: Model fit on the training data. """ X, y = check_X_y(X, y) @@ -63,15 +63,15 @@ def fit(self, X, y): def predict(self, X): """ Checks that the model is fitted and X is 2D. - By default, the input is checked to be a non-empty 2D array containing - only finite values. + By default, the input is checked to be a non-empty 2D array + containing only finite values. The above was taken from sklearn's documentation. Perform classification on test data X. Parameters: X : ndarray of shape (n_samples, n_features) Test samples. - + Returns: y_pred : ndarray of shape (n_samples,) Predicted class labels for samples in X. @@ -84,9 +84,9 @@ def predict(self, X): dtype=self.classes_.dtype ) - distances = np.linalg.norm(X[:, np.newaxis] - self.X_, axis=2) + distances = np.linalg.norm(X[:, np.newaxis] - self.X_, axis=2) nearest_neighbor_idx = np.argmin(distances, axis=1) - y_pred = self.y_[nearest_neighbor_idx] + y_pred = self.y_[nearest_neighbor_idx] return y_pred @@ -109,4 +109,4 @@ def score(self, X, y): X, y = check_X_y(X, y) y_pred = self.predict(X) - return np.mean(y_pred == y) \ No newline at end of file + return np.mean(y_pred == y) From 8386f65e22c4e17fb1b2670c58206fdf10bf71ac Mon Sep 17 00:00:00 2001 From: MaximeDespreaux Date: Thu, 13 Nov 2025 19:23:06 +0100 Subject: [PATCH 3/5] Fixed Linting for pydocstyle --- numpy_questions.py | 1 - sklearn_questions.py | 75 ++++++++++++++++++++++---------------------- 2 files changed, 38 insertions(+), 38 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index aac4cdcd..7df4639e 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -54,7 +54,6 @@ def max_index(X): def wallis_product(n_terms): """Implement the Wallis product to compute an approximation of pi. - See: https://en.wikipedia.org/wiki/Wallis_product diff --git a/sklearn_questions.py b/sklearn_questions.py index 3094ccc0..a4c6225a 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -29,26 +29,26 @@ class OneNearestNeighbor(BaseEstimator, ClassifierMixin): - "OneNearestNeighbor classifier." + """OneNearestNeighbor classifier.""" def __init__(self): # noqa: D107 pass def fit(self, X, y): """ - Checks X and y for consistent length, enforces X to be 2D and y 1D. - Ensure that target y is of a non-regression type. - The above was taken from sklearn's documentation. + Checks X and y for consistent length, enforces X to be 2D and y 1D. + Ensure that target y is of a non-regression type. + The above was taken from sklearn's documentation. - Fit OneNearestNeighbor model according to the given training data. - Parameters: - X : ndarray of shape (n_samples, n_features) - Training data. + Fit OneNearestNeighbor model according to the given training data. + Parameters: + X : ndarray of shape (n_samples, n_features) + Training data. - y : ndarray of shape (n_samples,) - Target values. + y : ndarray of shape (n_samples,) + Target values. - Returns: Model fit on the training data. + Returns: Model fit on the training data. """ X, y = check_X_y(X, y) check_classification_targets(y) @@ -62,19 +62,19 @@ def fit(self, X, y): def predict(self, X): """ - Checks that the model is fitted and X is 2D. - By default, the input is checked to be a non-empty 2D array - containing only finite values. - The above was taken from sklearn's documentation. - - Perform classification on test data X. - Parameters: - X : ndarray of shape (n_samples, n_features) - Test samples. - - Returns: - y_pred : ndarray of shape (n_samples,) - Predicted class labels for samples in X. + Check that the model is fitted and X is 2D. + By default, the input is checked to be a non-empty 2D array + containing only finite values. + The above was taken from sklearn's documentation. + + Perform classification on test data X. + Parameters: + X : ndarray of shape (n_samples, n_features) + Test samples. + + Returns: + y_pred : ndarray of shape (n_samples,) + Predicted class labels for samples in X. """ X = check_array(X) check_is_fitted(self) @@ -92,19 +92,20 @@ def predict(self, X): def score(self, X, y): """ - Checks that the model is fitted and X and y have consistent length. - The above was taken from sklearn's documentation. - - Returns the accuracy on the given test data and labels. - Parameters: - X : ndarray of shape (n_samples, n_features) - Test samples. - - y : ndarray of shape (n_samples,) - True labels for X. - Returns: - score : float - Accuracy of self.predict(X) wrt. y. + Check that the model is fitted and X and y have consistent length. + The above was taken from sklearn's documentation. + + Returns the accuracy on the given test data and labels. + Parameters: + X : ndarray of shape (n_samples, n_features) + Test samples. + + y : ndarray of shape (n_samples,) + True labels for X. + + Returns: + score : float + Accuracy of self.predict(X) wrt. y. """ X, y = check_X_y(X, y) y_pred = self.predict(X) From ab9303bd88c0dd351bd0d1a734e34b387c6b2204 Mon Sep 17 00:00:00 2001 From: MaximeDespreaux Date: Thu, 13 Nov 2025 19:27:06 +0100 Subject: [PATCH 4/5] Fixed Linting for pydocstyle again --- numpy_questions.py | 1 + sklearn_questions.py | 58 +++++++++++++++++++++++++------------------- 2 files changed, 34 insertions(+), 25 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index 7df4639e..aac4cdcd 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -54,6 +54,7 @@ def max_index(X): def wallis_product(n_terms): """Implement the Wallis product to compute an approximation of pi. + See: https://en.wikipedia.org/wiki/Wallis_product diff --git a/sklearn_questions.py b/sklearn_questions.py index a4c6225a..bef72c12 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -35,20 +35,24 @@ def __init__(self): # noqa: D107 pass def fit(self, X, y): - """ - Checks X and y for consistent length, enforces X to be 2D and y 1D. - Ensure that target y is of a non-regression type. - The above was taken from sklearn's documentation. + """Fit OneNearestNeighbor model according to the given training data. + + Check X and y for consistent length, enforce X to be 2D and y 1D, + and ensure that target y is of a non-regression type. + (The above is consistent with sklearn's documentation.) - Fit OneNearestNeighbor model according to the given training data. - Parameters: + Parameters + ---------- X : ndarray of shape (n_samples, n_features) - Training data. + Training data. y : ndarray of shape (n_samples,) - Target values. + Target values. - Returns: Model fit on the training data. + Returns + ------- + self : object + Model fit on the training data. """ X, y = check_X_y(X, y) check_classification_targets(y) @@ -61,20 +65,22 @@ def fit(self, X, y): return self def predict(self, X): - """ + """Perform classification on test data X. + Check that the model is fitted and X is 2D. By default, the input is checked to be a non-empty 2D array containing only finite values. - The above was taken from sklearn's documentation. + (The above was taken from sklearn's documentation.) - Perform classification on test data X. - Parameters: + Parameters + ---------- X : ndarray of shape (n_samples, n_features) - Test samples. + Test samples. - Returns: + Returns + ------- y_pred : ndarray of shape (n_samples,) - Predicted class labels for samples in X. + Predicted class labels for samples in X. """ X = check_array(X) check_is_fitted(self) @@ -91,21 +97,23 @@ def predict(self, X): return y_pred def score(self, X, y): - """ - Check that the model is fitted and X and y have consistent length. - The above was taken from sklearn's documentation. + """Return the accuracy on the given test data and labels. + + Check that the model is fitted and that X and y have consistent length. + (The above was taken from sklearn's documentation.) - Returns the accuracy on the given test data and labels. - Parameters: + Parameters + ---------- X : ndarray of shape (n_samples, n_features) - Test samples. + Test samples. y : ndarray of shape (n_samples,) - True labels for X. + True labels for X. - Returns: + Returns + ------- score : float - Accuracy of self.predict(X) wrt. y. + Accuracy of self.predict(X) with respect to y. """ X, y = check_X_y(X, y) y_pred = self.predict(X) From a648bf808445f01af11b43cb0b98d99c8a54d898 Mon Sep 17 00:00:00 2001 From: MaximeDespreaux Date: Thu, 13 Nov 2025 19:28:06 +0100 Subject: [PATCH 5/5] Fixed Linting for pydocstyle again2 --- numpy_questions.py | 1 - 1 file changed, 1 deletion(-) diff --git a/numpy_questions.py b/numpy_questions.py index aac4cdcd..ff36a3a0 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -69,7 +69,6 @@ def wallis_product(n_terms): pi : float The approximation of order `n_terms` of pi using the Wallis product. """ - pi = 2. if n_terms == 0: