From 296b0b3b609af1ba46fe38614f93fef4c4f8728a Mon Sep 17 00:00:00 2001 From: Ziqi Meng <140048727+TabErica@users.noreply.github.com> Date: Thu, 13 Nov 2025 16:07:54 +0100 Subject: [PATCH 1/4] part 2 --- numpy_questions.py | 10 ++++++++-- sklearn_questions.py | 9 ++++++++- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index 21fcec4b..db815601 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -41,7 +41,8 @@ def max_index(X): j = 0 # TODO - + flat_index = np.argmax(X) + i, j = np.unravel_index(flat_index, X.shape) return i, j @@ -64,4 +65,9 @@ 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 2.0 + n = np.arange(1, n_terms + 1) + term = term = (4 * n ** 2) / (4 * n ** 2 - 1) + product = np.prod(term) + return 2.0 * product diff --git a/sklearn_questions.py b/sklearn_questions.py index f65038c6..cec95690 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -45,6 +45,8 @@ def fit(self, X, y): self.n_features_in_ = X.shape[1] # XXX fix + self.X_ = X + self.y_ = y return self def predict(self, X): @@ -60,6 +62,11 @@ def predict(self, X): ) # XXX fix + for x_test in X: + distances = np.sum((self.X_ - x_test)**2, axis=1) + closest_idx = np.argmin(distances) + y_pred.append(self.y_[closest_idx]) + return y_pred def score(self, X, y): @@ -71,4 +78,4 @@ def score(self, X, y): y_pred = self.predict(X) # XXX fix - return y_pred.sum() + return np.mean(y_pred == y) From a290d76c65ad42a0dbae0b277e9053ad8dcc031c Mon Sep 17 00:00:00 2001 From: Ziqi Meng <140048727+TabErica@users.noreply.github.com> Date: Thu, 13 Nov 2025 16:14:01 +0100 Subject: [PATCH 2/4] 1 --- sklearn_questions.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/sklearn_questions.py b/sklearn_questions.py index cec95690..17438cea 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -66,7 +66,6 @@ def predict(self, X): distances = np.sum((self.X_ - x_test)**2, axis=1) closest_idx = np.argmin(distances) y_pred.append(self.y_[closest_idx]) - return y_pred def score(self, X, y): @@ -76,6 +75,5 @@ def score(self, X, y): """ X, y = check_X_y(X, y) y_pred = self.predict(X) - # XXX fix return np.mean(y_pred == y) From 59a78aa5c25f4dfc8dc624547f8c3d1590bd743f Mon Sep 17 00:00:00 2001 From: Ziqi Meng <140048727+TabErica@users.noreply.github.com> Date: Thu, 13 Nov 2025 16:21:25 +0100 Subject: [PATCH 3/4] 2 --- numpy_questions.py | 5 +++-- sklearn_questions.py | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index db815601..a0e76487 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -66,8 +66,9 @@ 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. if n_terms == 0: - return 2.0 + return 2.0 n = np.arange(1, n_terms + 1) term = term = (4 * n ** 2) / (4 * n ** 2 - 1) - product = np.prod(term) + product = np.prod(term) + return 2.0 * product diff --git a/sklearn_questions.py b/sklearn_questions.py index 17438cea..e6faf523 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -29,7 +29,7 @@ class OneNearestNeighbor(BaseEstimator, ClassifierMixin): - "OneNearestNeighbor classifier." + """OneNearestNeighbor classifier.""" def __init__(self): # noqa: D107 pass From d1d1f32e05d039e68d4fcf055789968d669924af Mon Sep 17 00:00:00 2001 From: Ziqi Meng <140048727+TabErica@users.noreply.github.com> Date: Thu, 13 Nov 2025 16:39:50 +0100 Subject: [PATCH 4/4] 3 --- numpy_questions.py | 7 ++++++- sklearn_questions.py | 4 ++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index a0e76487..58427bc2 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -41,6 +41,11 @@ def max_index(X): j = 0 # TODO + if not isinstance(X, np.ndarray): + raise ValueError("Input must be a numpy array") + if X.ndim != 2: + raise ValueError("Input must be 2D") + flat_index = np.argmax(X) i, j = np.unravel_index(flat_index, X.shape) return i, j @@ -66,7 +71,7 @@ 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. if n_terms == 0: - return 2.0 + return 1.0 n = np.arange(1, n_terms + 1) term = term = (4 * n ** 2) / (4 * n ** 2 - 1) product = np.prod(term) diff --git a/sklearn_questions.py b/sklearn_questions.py index e6faf523..86f3f5aa 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -62,10 +62,10 @@ def predict(self, X): ) # XXX fix - for x_test in X: + for i, x_test in enumerate(X): distances = np.sum((self.X_ - x_test)**2, axis=1) closest_idx = np.argmin(distances) - y_pred.append(self.y_[closest_idx]) + y_pred[i] = self.y_[closest_idx] return y_pred def score(self, X, y):