From f97b267298b1408617b9983c9e2b6f0d31ec8002 Mon Sep 17 00:00:00 2001 From: Ion de Merlis Date: Fri, 14 Nov 2025 23:47:10 +0100 Subject: [PATCH 1/3] Questions Answered --- numpy_questions.py | 18 +++++++++++++----- sklearn_questions.py | 37 ++++++++++++++++++++++++------------- 2 files changed, 37 insertions(+), 18 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index 21fcec4b..6bc7c93a 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -37,10 +37,14 @@ 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") + if X.ndim != 2: + raise ValueError("X must be a 2D array") i = 0 j = 0 - - # TODO + index = np.argmax(X) + i, j = np.unravel_index(index, X.shape) return i, j @@ -62,6 +66,10 @@ 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. + product = 1 + + for i in range(1, n_terms + 1): + numerator = 4 * (i ** 2) + denominator = numerator - 1 + product *= numerator / denominator + return 2 * product diff --git a/sklearn_questions.py b/sklearn_questions.py index f65038c6..244b544a 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -29,38 +29,50 @@ class OneNearestNeighbor(BaseEstimator, ClassifierMixin): - "OneNearestNeighbor classifier." + """OneNearestNeighbor classifier.""" def __init__(self): # noqa: D107 pass def fit(self, X, y): - """Write docstring. + """ + Fits data. - And describe parameters + Parameters: + X: ndarray shape (n_samples, n_features) + y: ndarray shape (n_samples,) + + Returns: + 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 = X + self.y = y return self def predict(self, X): - """Write docstring. + """ + Make predictions. - And describe parameters + Parameters: + X: ndarray of shape (n_samples, n_features) + + Returns: + predicted labels """ check_is_fitted(self) X = check_array(X) - y_pred = np.full( - shape=len(X), fill_value=self.classes_[0], - dtype=self.classes_.dtype + distances = np.linalg.norm( + self.X[None, :, :] - X[:, None, :], axis=2 ) - # XXX fix - return y_pred + nearest_neighbour = np.argmin(distances, axis=1) + + return self.y[nearest_neighbour] def score(self, X, y): """Write docstring. @@ -70,5 +82,4 @@ def score(self, X, 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 5e1244c18527b800245e8bb39d8a861eab36a239 Mon Sep 17 00:00:00 2001 From: Ion de Merlis Date: Sat, 15 Nov 2025 00:05:52 +0100 Subject: [PATCH 2/3] Fixed pytests errors --- numpy_questions.py | 3 +++ sklearn_questions.py | 16 +++++++++------- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index 6bc7c93a..a41c211c 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -72,4 +72,7 @@ def wallis_product(n_terms): numerator = 4 * (i ** 2) denominator = numerator - 1 product *= numerator / denominator + + if n_terms == 0: + return 1.0 return 2 * product diff --git a/sklearn_questions.py b/sklearn_questions.py index 244b544a..7001abed 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -23,12 +23,12 @@ 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.utils.multiclass import check_classification_targets +from sklearn.utils.validation import validate_data -class OneNearestNeighbor(BaseEstimator, ClassifierMixin): +class OneNearestNeighbor(ClassifierMixin, BaseEstimator): """OneNearestNeighbor classifier.""" def __init__(self): # noqa: D107 @@ -47,11 +47,13 @@ def fit(self, X, y): """ X, y = check_X_y(X, y) check_classification_targets(y) + + X = validate_data(self, X, reset=True) self.classes_ = np.unique(y) self.n_features_in_ = X.shape[1] - self.X = X - self.y = y + self.X_ = X + self.y_ = y return self def predict(self, X): @@ -65,14 +67,14 @@ def predict(self, X): predicted labels """ check_is_fitted(self) - X = check_array(X) + X = validate_data(self, X, reset=False) distances = np.linalg.norm( - self.X[None, :, :] - X[:, None, :], axis=2 + self.X_[None, :, :] - X[:, None, :], axis=2 ) nearest_neighbour = np.argmin(distances, axis=1) - return self.y[nearest_neighbour] + return self.y_[nearest_neighbour] def score(self, X, y): """Write docstring. From e895b36c78f45716e12dd79a9052de7fb2c5526e Mon Sep 17 00:00:00 2001 From: Ion de Merlis Date: Sat, 15 Nov 2025 11:03:26 +0100 Subject: [PATCH 3/3] Fixed sklearn validation --- sklearn_questions.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/sklearn_questions.py b/sklearn_questions.py index 7001abed..1610dfc6 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -22,10 +22,9 @@ 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_X_y, check_array from sklearn.utils.validation import check_is_fitted from sklearn.utils.multiclass import check_classification_targets -from sklearn.utils.validation import validate_data class OneNearestNeighbor(ClassifierMixin, BaseEstimator): @@ -48,7 +47,6 @@ def fit(self, X, y): X, y = check_X_y(X, y) check_classification_targets(y) - X = validate_data(self, X, reset=True) self.classes_ = np.unique(y) self.n_features_in_ = X.shape[1] @@ -67,7 +65,7 @@ def predict(self, X): predicted labels """ check_is_fitted(self) - X = validate_data(self, X, reset=False) + X = check_array(X) distances = np.linalg.norm( self.X_[None, :, :] - X[:, None, :], axis=2 )