From 9675372a9660d37589f0bf5dff39e5d04b60de2b Mon Sep 17 00:00:00 2001 From: Zixiao Guo Date: Thu, 13 Nov 2025 17:23:55 +0100 Subject: [PATCH 01/17] numpy_questions.py --- numpy_questions.py | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index 21fcec4b..577771b0 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -37,11 +37,17 @@ 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 must be a numpy array") + + if X.ndim != 2: + raise ValueError("Input array must be 2D") + + flat_index = np.argmax(X, axis=None) + + i, j = np.unravel_index(flat_index, X.shape) return i, j @@ -64,4 +70,16 @@ 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.0 + + product = 1.0 + + for n in range(1, n_terms + 1): + term1 = (2.0 * n) / (2.0 * n - 1.0) + term2 = (2.0 * n) / (2.0 * n + 1.0) + product = product * term1 * term2 + + pi_approx = 2.0 * product + + return pi_approx \ No newline at end of file From fea94283ab1f163666f1fc3924dd10e48483a517 Mon Sep 17 00:00:00 2001 From: Zixiao Guo Date: Thu, 13 Nov 2025 17:25:25 +0100 Subject: [PATCH 02/17] sklearn_questions.py --- sklearn_questions.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/sklearn_questions.py b/sklearn_questions.py index f65038c6..599e3be9 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -43,7 +43,8 @@ def fit(self, X, y): check_classification_targets(y) self.classes_ = np.unique(y) self.n_features_in_ = X.shape[1] - + self.X_ = X + self.y_ = y # XXX fix return self @@ -58,7 +59,10 @@ def predict(self, X): shape=len(X), fill_value=self.classes_[0], dtype=self.classes_.dtype ) - + for i, x_test in enumerate(X): + distances = np.sqrt(np.sum((self.X_ - x_test) ** 2, axis=1)) + nearest_idx = np.argmin(distances) + y_pred[i] = self.y_[nearest_idx] # XXX fix return y_pred @@ -71,4 +75,6 @@ def score(self, X, y): y_pred = self.predict(X) # XXX fix - return y_pred.sum() + accuracy = np.mean(y_pred == y) + + return accuracy From 1b1d778db5b19ffc0c613af68fa407cacb2dcd39 Mon Sep 17 00:00:00 2001 From: Zixiao Guo Date: Thu, 13 Nov 2025 20:06:34 +0100 Subject: [PATCH 03/17] sklearn_questions.py --- sklearn_questions.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/sklearn_questions.py b/sklearn_questions.py index 599e3be9..b189e00e 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -28,7 +28,7 @@ from sklearn.utils.multiclass import check_classification_targets -class OneNearestNeighbor(BaseEstimator, ClassifierMixin): +class OneNearestNeighbor(ClassifierMixin, BaseEstimator): "OneNearestNeighbor classifier." def __init__(self): # noqa: D107 @@ -36,7 +36,7 @@ def __init__(self): # noqa: D107 def fit(self, X, y): """Write docstring. - + And describe parameters """ X, y = check_X_y(X, y) @@ -45,16 +45,21 @@ def fit(self, X, y): self.n_features_in_ = X.shape[1] self.X_ = X self.y_ = y - # XXX fix return self def predict(self, X): """Write docstring. - + And describe parameters """ check_is_fitted(self) - X = check_array(X) + X = check_array(X, ensure_2d=True) + if X.shape[1] != self.n_features_in_: + raise ValueError( + f'X has {X.shape[1]} features, but {self.__class__.__name__} ' + f'is expecting {self.n_features_in_} features as input.' + ) + y_pred = np.full( shape=len(X), fill_value=self.classes_[0], dtype=self.classes_.dtype @@ -68,13 +73,11 @@ def predict(self, X): def score(self, X, y): """Write docstring. - + And describe parameters """ X, y = check_X_y(X, y) y_pred = self.predict(X) - # XXX fix accuracy = np.mean(y_pred == y) - return accuracy From a34a5a2b3b95907e8f26022df7c59622e63a28eb Mon Sep 17 00:00:00 2001 From: Zixiao Guo Date: Thu, 13 Nov 2025 20:08:09 +0100 Subject: [PATCH 04/17] numpy_questions.py --- numpy_questions.py | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index 577771b0..dbf2cdb3 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -41,12 +41,9 @@ def max_index(X): # TODO if not isinstance(X, np.ndarray): raise ValueError("Input must be a numpy array") - if X.ndim != 2: raise ValueError("Input array must be 2D") - flat_index = np.argmax(X, axis=None) - i, j = np.unravel_index(flat_index, X.shape) return i, j @@ -72,14 +69,11 @@ def wallis_product(n_terms): # terms in the product. For example 10000. if n_terms == 0: return 1.0 - product = 1.0 - for n in range(1, n_terms + 1): term1 = (2.0 * n) / (2.0 * n - 1.0) term2 = (2.0 * n) / (2.0 * n + 1.0) product = product * term1 * term2 - pi_approx = 2.0 * product - - return pi_approx \ No newline at end of file + return pi_approx + \ No newline at end of file From 7f24370c3520c48050f8c87302856af471adf0d2 Mon Sep 17 00:00:00 2001 From: Zixiao Guo Date: Thu, 13 Nov 2025 20:12:14 +0100 Subject: [PATCH 05/17] numpy_questions.py --- numpy_questions.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index dbf2cdb3..2b184753 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -75,5 +75,4 @@ def wallis_product(n_terms): term2 = (2.0 * n) / (2.0 * n + 1.0) product = product * term1 * term2 pi_approx = 2.0 * product - return pi_approx - \ No newline at end of file + return pi_approx \ No newline at end of file From 4fbd8ef2ce856a8456872cb7a0a422e18061ae6b Mon Sep 17 00:00:00 2001 From: Zixiao Guo Date: Thu, 13 Nov 2025 20:12:43 +0100 Subject: [PATCH 06/17] numpy_questions.py --- sklearn_questions.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sklearn_questions.py b/sklearn_questions.py index b189e00e..592651c1 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -54,7 +54,7 @@ def predict(self, X): """ check_is_fitted(self) X = check_array(X, ensure_2d=True) - if X.shape[1] != self.n_features_in_: + if X.shape[1] != self.n_features_in_: raise ValueError( f'X has {X.shape[1]} features, but {self.__class__.__name__} ' f'is expecting {self.n_features_in_} features as input.' @@ -80,4 +80,4 @@ def score(self, X, y): y_pred = self.predict(X) # XXX fix accuracy = np.mean(y_pred == y) - return accuracy + return accuracy \ No newline at end of file From 6e5ba4b35568e36537d7b8ecf6e34a06f0832c9f Mon Sep 17 00:00:00 2001 From: Zixiao Guo Date: Thu, 13 Nov 2025 20:18:42 +0100 Subject: [PATCH 07/17] numpy_questions.py --- numpy_questions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/numpy_questions.py b/numpy_questions.py index 2b184753..f37111e2 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -75,4 +75,4 @@ def wallis_product(n_terms): term2 = (2.0 * n) / (2.0 * n + 1.0) product = product * term1 * term2 pi_approx = 2.0 * product - return pi_approx \ No newline at end of file + return pi_approx From c21fe5b41fdc216acd8218f8941135da0b9658bc Mon Sep 17 00:00:00 2001 From: Zixiao Guo Date: Thu, 13 Nov 2025 20:19:39 +0100 Subject: [PATCH 08/17] sklearn_questions.py --- sklearn_questions.py | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/sklearn_questions.py b/sklearn_questions.py index 592651c1..4ee623cc 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -35,10 +35,6 @@ def __init__(self): # noqa: D107 pass def fit(self, X, y): - """Write docstring. - - And describe parameters - """ X, y = check_X_y(X, y) check_classification_targets(y) self.classes_ = np.unique(y) @@ -48,10 +44,6 @@ def fit(self, X, y): return self def predict(self, X): - """Write docstring. - - And describe parameters - """ check_is_fitted(self) X = check_array(X, ensure_2d=True) if X.shape[1] != self.n_features_in_: @@ -59,7 +51,6 @@ def predict(self, X): f'X has {X.shape[1]} features, but {self.__class__.__name__} ' f'is expecting {self.n_features_in_} features as input.' ) - y_pred = np.full( shape=len(X), fill_value=self.classes_[0], dtype=self.classes_.dtype @@ -73,11 +64,10 @@ def predict(self, X): def score(self, X, y): """Write docstring. - And describe parameters """ X, y = check_X_y(X, y) - y_pred = self.predict(X) + y_pred=self.predict(X) # XXX fix accuracy = np.mean(y_pred == y) - return accuracy \ No newline at end of file + return accuracy From 2d9a9a2911d98dc4a962c3fdb214bf8322da46db Mon Sep 17 00:00:00 2001 From: Zixiao Guo Date: Thu, 13 Nov 2025 20:22:52 +0100 Subject: [PATCH 09/17] sklearn_questions.py --- sklearn_questions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sklearn_questions.py b/sklearn_questions.py index 4ee623cc..75e7bb2c 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -67,7 +67,7 @@ def score(self, X, y): And describe parameters """ X, y = check_X_y(X, y) - y_pred=self.predict(X) + y_pred = self.predict(X) # XXX fix accuracy = np.mean(y_pred == y) return accuracy From 6f2187f6bdafb1f3809d4f0378432df3fa49b31e Mon Sep 17 00:00:00 2001 From: Zixiao Guo Date: Thu, 13 Nov 2025 20:27:27 +0100 Subject: [PATCH 10/17] sklearn_questions.py --- sklearn_questions.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/sklearn_questions.py b/sklearn_questions.py index 75e7bb2c..1a81f5d7 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -29,8 +29,6 @@ class OneNearestNeighbor(ClassifierMixin, BaseEstimator): - "OneNearestNeighbor classifier." - def __init__(self): # noqa: D107 pass @@ -63,9 +61,6 @@ def predict(self, X): return y_pred def score(self, X, y): - """Write docstring. - And describe parameters - """ X, y = check_X_y(X, y) y_pred = self.predict(X) # XXX fix From 52d41bb56489a40688b6393122dff62956c7c3cc Mon Sep 17 00:00:00 2001 From: Zixiao Guo Date: Thu, 13 Nov 2025 20:34:34 +0100 Subject: [PATCH 11/17] numpy_questions.py --- numpy_questions.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index f37111e2..f331d194 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -20,24 +20,20 @@ def max_index(X): """Return the index of the maximum in a numpy array. - Parameters ---------- X : ndarray of shape (n_samples, n_features) The input array. - Returns ------- (i, j) : tuple(int) The row and columnd index of the maximum. - Raises ------ ValueError If the input is not a numpy array or if the shape is not 2D. """ - # TODO if not isinstance(X, np.ndarray): raise ValueError("Input must be a numpy array") From 8d17098de6bbc39ef9c316f92a5f7f3c7add2744 Mon Sep 17 00:00:00 2001 From: Zixiao Guo Date: Thu, 13 Nov 2025 20:34:53 +0100 Subject: [PATCH 12/17] sklearn_questions.py --- sklearn_questions.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/sklearn_questions.py b/sklearn_questions.py index 1a81f5d7..9e0aec89 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -29,10 +29,14 @@ class OneNearestNeighbor(ClassifierMixin, BaseEstimator): + "OneNearestNeighbor classifier." def __init__(self): # noqa: D107 pass def fit(self, X, y): + """Write docstring. + And describe parameters + """ X, y = check_X_y(X, y) check_classification_targets(y) self.classes_ = np.unique(y) @@ -42,6 +46,9 @@ def fit(self, X, y): return self def predict(self, X): + """Write docstring. + And describe parameters + """ check_is_fitted(self) X = check_array(X, ensure_2d=True) if X.shape[1] != self.n_features_in_: @@ -61,6 +68,9 @@ def predict(self, X): return y_pred def score(self, X, y): + """Write docstring. + And describe parameters + """ X, y = check_X_y(X, y) y_pred = self.predict(X) # XXX fix From ae579714ba543ad31d9a3fdb7c8baf263a2e12f6 Mon Sep 17 00:00:00 2001 From: Zixiao Guo Date: Thu, 13 Nov 2025 20:39:38 +0100 Subject: [PATCH 13/17] numpy_questions.py --- numpy_questions.py | 1 + sklearn_questions.py | 6 +++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/numpy_questions.py b/numpy_questions.py index f331d194..48629fb1 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -19,6 +19,7 @@ def max_index(X): + """Return the index of the maximum in a numpy array. Parameters ---------- diff --git a/sklearn_questions.py b/sklearn_questions.py index 9e0aec89..03393491 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -29,12 +29,14 @@ class OneNearestNeighbor(ClassifierMixin, BaseEstimator): - "OneNearestNeighbor classifier." + """OneNearestNeighbor classifier.""" + def __init__(self): # noqa: D107 pass def fit(self, X, y): """Write docstring. + And describe parameters """ X, y = check_X_y(X, y) @@ -47,6 +49,7 @@ def fit(self, X, y): def predict(self, X): """Write docstring. + And describe parameters """ check_is_fitted(self) @@ -69,6 +72,7 @@ def predict(self, X): def score(self, X, y): """Write docstring. + And describe parameters """ X, y = check_X_y(X, y) From 9f3ed7140ca83f8dd90ca1bcaaa787cdb6ffc92b Mon Sep 17 00:00:00 2001 From: Zixiao Guo Date: Thu, 13 Nov 2025 20:41:05 +0100 Subject: [PATCH 14/17] numpy_questions.py --- numpy_questions.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index 48629fb1..8cf60c1b 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -16,11 +16,10 @@ errors by calling `flake8` at the root of the repo. """ import numpy as np - - def max_index(X): """Return the index of the maximum in a numpy array. + Parameters ---------- X : ndarray of shape (n_samples, n_features) From c3733b35a62f2acd523f6f6ed0adfc8b4133ca18 Mon Sep 17 00:00:00 2001 From: Zixiao Guo Date: Thu, 13 Nov 2025 20:42:59 +0100 Subject: [PATCH 15/17] numpy_questions.py --- numpy_questions.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/numpy_questions.py b/numpy_questions.py index 8cf60c1b..acdf285f 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -24,10 +24,12 @@ def max_index(X): ---------- X : ndarray of shape (n_samples, n_features) The input array. + Returns ------- (i, j) : tuple(int) The row and columnd index of the maximum. + Raises ------ ValueError From 0b5db3e9d79d432c7135a84344d7d22fd25b0c95 Mon Sep 17 00:00:00 2001 From: Zixiao Guo Date: Thu, 13 Nov 2025 20:44:37 +0100 Subject: [PATCH 16/17] numpy_questions.py --- numpy_questions.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/numpy_questions.py b/numpy_questions.py index acdf285f..e1bf930d 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -16,6 +16,8 @@ errors by calling `flake8` at the root of the repo. """ import numpy as np + + def max_index(X): """Return the index of the maximum in a numpy array. From 1bf3e997a16d0956f63e5fb1915fd5296f5165dd Mon Sep 17 00:00:00 2001 From: Zixiao Guo Date: Thu, 13 Nov 2025 20:46:23 +0100 Subject: [PATCH 17/17] numpy_questions.py --- numpy_questions.py | 1 - 1 file changed, 1 deletion(-) diff --git a/numpy_questions.py b/numpy_questions.py index e1bf930d..60a8609f 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -19,7 +19,6 @@ def max_index(X): - """Return the index of the maximum in a numpy array. Parameters