From e92e033555f746827efa37cfa8687e84fa73e125 Mon Sep 17 00:00:00 2001 From: Benedikt Watzinger Date: Thu, 13 Nov 2025 16:39:59 +0100 Subject: [PATCH 1/8] Benedikt Watzinger pull --- numpy_questions.py | 18 ++++++++++- sklearn_questions.py | 72 +++++++++++++++++++++++++++++++++++--------- 2 files changed, 75 insertions(+), 15 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index 21fcec4b..7beb5367 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -42,6 +42,14 @@ def max_index(X): # TODO + if not isinstance(X, np.ndarray): + raise ValueError + if X.ndim != 2: + raise ValueError + + index = np.argmax(X) + i, j = np.unravel_index(index, X.shape) + return i, j @@ -64,4 +72,12 @@ 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: + return 1 + wp = 1 + + for n in range(1, n_terms+1): + wp = wp * ((4 * n**2) / (4 * n**2 - 1)) + pi = wp * 2 + return pi diff --git a/sklearn_questions.py b/sklearn_questions.py index f65038c6..bfb33ff4 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -23,52 +23,96 @@ 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.metrics import pairwise_distances_argmin_min +from sklearn.utils.validation import validate_data -class OneNearestNeighbor(BaseEstimator, ClassifierMixin): +class OneNearestNeighbor(ClassifierMixin, BaseEstimator): "OneNearestNeighbor classifier." def __init__(self): # noqa: D107 pass def fit(self, X, y): - """Write docstring. + """Fit the classifier. This function stores training data X + and the labels y. - And describe parameters + Parameters + ---------- + X : Training data (n_samples, n_features) + + Y : Target labels (n_samples) + + Returns + ------- + + self : returns the fitted classifier + + Raises + ------ + ValueError + If X and y have different numbers of samples """ - X, y = check_X_y(X, y) + # X, y = check_X_y(X, y) + 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_ = np.asarray(X) + self.y_ = np.asarray(y) + return self def predict(self, X): - """Write docstring. + """Return the predicted class for a data set in an numpy array. + + Parameters + ---------- + X : ndarray of shape (n_samples, n_features) + The input array. - And describe parameters + Returns + ------- + + y_pred : ndarray of shape (n_samples) + The predicted classes for the n_samples. """ check_is_fitted(self) - X = check_array(X) + X = validate_data(self, X, reset=False) + # X = check_array(X, ensure_min_features=self.n_features_in_) y_pred = np.full( shape=len(X), fill_value=self.classes_[0], dtype=self.classes_.dtype ) - # XXX fix + argmin, _ = pairwise_distances_argmin_min(X, self.X_) + y_pred = self.y_[argmin] + return y_pred def score(self, X, y): - """Write docstring. + """Return the score of the OneNearestNeighbor on a data set + + Parameters + ---------- - And describe parameters + X : ndarray of shape (n_samples, n_features) + The input array. + y : ndarray of shape (n_samples) + The true classes of the samples. + + Returns + ------- + + score : float + The percentage of samples accurately predicted. """ X, y = check_X_y(X, y) y_pred = self.predict(X) - # XXX fix - return y_pred.sum() + score = np.mean(y_pred == y) + + return score From 314ca7cc7ecb519c2a89688297829c96bcd9c5fd Mon Sep 17 00:00:00 2001 From: Benedikt Watzinger Date: Thu, 13 Nov 2025 16:48:10 +0100 Subject: [PATCH 2/8] Benedikt Watzinger pull --- sklearn_questions.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/sklearn_questions.py b/sklearn_questions.py index bfb33ff4..7361d563 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -26,7 +26,7 @@ from sklearn.utils.validation import check_is_fitted from sklearn.utils.multiclass import check_classification_targets from sklearn.metrics import pairwise_distances_argmin_min -from sklearn.utils.validation import validate_data +from sklearn.utils.validation import check_array class OneNearestNeighbor(ClassifierMixin, BaseEstimator): @@ -55,8 +55,7 @@ def fit(self, X, y): ValueError If X and y have different numbers of samples """ - # X, y = check_X_y(X, y) - 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] @@ -82,7 +81,7 @@ def predict(self, X): """ check_is_fitted(self) X = validate_data(self, X, reset=False) - # X = check_array(X, ensure_min_features=self.n_features_in_) + X = check_array(X) y_pred = np.full( shape=len(X), fill_value=self.classes_[0], dtype=self.classes_.dtype From d359f84a01e52a6cbcbb1e6aff9a0a1856867c5d Mon Sep 17 00:00:00 2001 From: Benedikt Watzinger Date: Thu, 13 Nov 2025 17:14:51 +0100 Subject: [PATCH 3/8] Fix validate data --- sklearn_questions.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/sklearn_questions.py b/sklearn_questions.py index 7361d563..5baec183 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -80,13 +80,12 @@ def predict(self, X): The predicted classes for the n_samples. """ check_is_fitted(self) - X = validate_data(self, X, reset=False) X = check_array(X) y_pred = np.full( shape=len(X), fill_value=self.classes_[0], dtype=self.classes_.dtype ) - + argmin, _ = pairwise_distances_argmin_min(X, self.X_) y_pred = self.y_[argmin] From 01dc7d16098ae9c70724f9aed35144603888d57b Mon Sep 17 00:00:00 2001 From: Benedikt Watzinger Date: Thu, 13 Nov 2025 17:21:41 +0100 Subject: [PATCH 4/8] Benedikt Watzinger pull format --- sklearn_questions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sklearn_questions.py b/sklearn_questions.py index 5baec183..21444a06 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -85,7 +85,7 @@ def predict(self, X): shape=len(X), fill_value=self.classes_[0], dtype=self.classes_.dtype ) - + argmin, _ = pairwise_distances_argmin_min(X, self.X_) y_pred = self.y_[argmin] From 2a41b691456991678999d9a367440dda4e49386e Mon Sep 17 00:00:00 2001 From: Benedikt Watzinger Date: Thu, 13 Nov 2025 17:32:04 +0100 Subject: [PATCH 5/8] Benedikt Watzinger pull format --- sklearn_questions.py | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/sklearn_questions.py b/sklearn_questions.py index 21444a06..1f797d8b 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -30,30 +30,30 @@ class OneNearestNeighbor(ClassifierMixin, BaseEstimator): - "OneNearestNeighbor classifier." + """OneNearestNeighbor classifier.""" def __init__(self): # noqa: D107 pass def fit(self, X, y): - """Fit the classifier. This function stores training data X + """Fit the OneNearestNeighbor classifier. This function stores training data X + and the labels y. Parameters ---------- - X : Training data (n_samples, n_features) + X : Training data (n_samples, n_features). - Y : Target labels (n_samples) + Y : Target labels (n_samples). Returns ------- - - self : returns the fitted classifier + self : returns the fitted classifier. Raises ------ ValueError - If X and y have different numbers of samples + If X and y have different numbers of samples. """ X, y = check_X_y(X, y) check_classification_targets(y) @@ -75,7 +75,6 @@ def predict(self, X): Returns ------- - y_pred : ndarray of shape (n_samples) The predicted classes for the n_samples. """ @@ -92,11 +91,10 @@ def predict(self, X): return y_pred def score(self, X, y): - """Return the score of the OneNearestNeighbor on a data set + """Return the score of the OneNearestNeighbor on a data set. Parameters ---------- - X : ndarray of shape (n_samples, n_features) The input array. y : ndarray of shape (n_samples) @@ -104,7 +102,6 @@ def score(self, X, y): Returns ------- - score : float The percentage of samples accurately predicted. """ From 8ea36cddccc0e04cbda14442012e628c869ba4ca Mon Sep 17 00:00:00 2001 From: Benedikt Watzinger Date: Thu, 13 Nov 2025 17:35:38 +0100 Subject: [PATCH 6/8] Benedikt Watzinger format fix --- sklearn_questions.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/sklearn_questions.py b/sklearn_questions.py index 1f797d8b..2d2159cd 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -36,9 +36,8 @@ def __init__(self): # noqa: D107 pass def fit(self, X, y): - """Fit the OneNearestNeighbor classifier. This function stores training data X - - and the labels y. + """Fit the OneNearestNeighbor classifier - + This function stores training data X and the labels y. Parameters ---------- From 8147b618559164080b6bc73f5601c68b1a592b89 Mon Sep 17 00:00:00 2001 From: Benedikt Watzinger Date: Thu, 13 Nov 2025 17:38:51 +0100 Subject: [PATCH 7/8] Benedikt Watzinger flake8 fix --- sklearn_questions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sklearn_questions.py b/sklearn_questions.py index 2d2159cd..f35b8edb 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -36,7 +36,7 @@ def __init__(self): # noqa: D107 pass def fit(self, X, y): - """Fit the OneNearestNeighbor classifier - + """Fit the OneNearestNeighbor classifier This function stores training data X and the labels y. Parameters From 5548d859e8804b254082b82c2e2cdf0ef24a6119 Mon Sep 17 00:00:00 2001 From: Benedikt Watzinger Date: Thu, 13 Nov 2025 17:43:16 +0100 Subject: [PATCH 8/8] Benedikt Watzinger pydocstyle fix --- sklearn_questions.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sklearn_questions.py b/sklearn_questions.py index f35b8edb..192ae662 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -36,7 +36,8 @@ def __init__(self): # noqa: D107 pass def fit(self, X, y): - """Fit the OneNearestNeighbor classifier + """Fit the OneNearestNeighbor classifier. + This function stores training data X and the labels y. Parameters