From fee5c79f777b56d4a676f8b63094d94bb61ba638 Mon Sep 17 00:00:00 2001 From: ethan benhamou Date: Sat, 15 Nov 2025 15:32:36 +0100 Subject: [PATCH 01/10] Complete numpy and sklearn assignment --- numpy_questions.py | 19 +++++++++++++++++- sklearn_questions.py | 47 +++++++++++++++++++++++++++++++++++--------- 2 files changed, 56 insertions(+), 10 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index 21fcec4b..49cd863c 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("Input is not a numpy array") # Check for numpy array + + if X.ndim != 2: + raise ValueError("Input array is not 2D") # Check for 2D shape + + i, j = np.unravel_index(np.argmax(X), X.shape) # Find indices of maximum value + return i, j @@ -64,4 +72,13 @@ 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 # Wallis product with 0 terms is defined as 1, so pi approximation is 2 + + n = np.arange(1, n_terms + 1) # Create an array from 1 to n_terms + terms = (2 * n / (2 * n - 1)) * (2 * n / (2 * n + 1)) + product = np.prod(terms) # Compute the product of all terms + + return 2.0 * product # Return the approximation of pi + diff --git a/sklearn_questions.py b/sklearn_questions.py index f65038c6..aac09f76 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -35,9 +35,20 @@ def __init__(self): # noqa: D107 pass def fit(self, X, y): - """Write docstring. - - And describe parameters + """Fit the OneNearestNeighbor classifier. + + Parameters + ---------- + + X : ndarray of shape (n_samples, n_features) + Training data. + y : ndarray of shape (n_samples,) + Target labels corresponding to X. + + Returns + ------- + self : object + Fitted estimator. """ X, y = check_X_y(X, y) check_classification_targets(y) @@ -48,9 +59,17 @@ def fit(self, X, y): return self def predict(self, X): - """Write docstring. - - And describe parameters + """Predict class labels for the given samples. + + Parameters + ---------- + X : ndarray of shape (n_samples, n_features) + Input data to predict. + + Returns + ------- + y_pred : ndarray of shape (n_samples,) + Predicted class labels for each sample in X. """ check_is_fitted(self) X = check_array(X) @@ -63,9 +82,19 @@ def predict(self, X): return y_pred def score(self, X, y): - """Write docstring. - - And describe parameters + """Compute the accuracy of the classifier. + + Parameters + ---------- + X : ndarray of shape (n_samples, n_features) + Test samples. + y : ndarray of shape (n_samples,) + True labels for X. + + Returns + ------- + score : float + Mean accuracy of the classifier on the provided data. """ X, y = check_X_y(X, y) y_pred = self.predict(X) From 1171f2aed29fb7b656c01f8b099e9292048e7b62 Mon Sep 17 00:00:00 2001 From: ethan benhamou Date: Sat, 15 Nov 2025 15:42:06 +0100 Subject: [PATCH 02/10] Complete numpy and sklearn assignment --- sklearn_questions.py | 1 - 1 file changed, 1 deletion(-) diff --git a/sklearn_questions.py b/sklearn_questions.py index aac09f76..a7c21fe2 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -39,7 +39,6 @@ def fit(self, X, y): Parameters ---------- - X : ndarray of shape (n_samples, n_features) Training data. y : ndarray of shape (n_samples,) From c7bc633a1f5b88862b40e0c1b2c9dbd76803efa0 Mon Sep 17 00:00:00 2001 From: ethan benhamou Date: Sat, 15 Nov 2025 15:49:53 +0100 Subject: [PATCH 03/10] Complete numpy and sklearn assignment --- numpy_questions.py | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index 49cd863c..e1257d2e 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -41,15 +41,15 @@ def max_index(X): j = 0 # TODO - + if not isinstance(X, np.ndarray): - raise ValueError("Input is not a numpy array") # Check for numpy array + raise ValueError("Input is not a numpy array") # Check for numpy array if X.ndim != 2: - raise ValueError("Input array is not 2D") # Check for 2D shape - - i, j = np.unravel_index(np.argmax(X), X.shape) # Find indices of maximum value - + raise ValueError("Input array is not 2D") # Check for 2D shape + + i, j = np.unravel_index(np.argmax(X), X.shape) # Find indices of maximum value + return i, j @@ -74,11 +74,12 @@ def wallis_product(n_terms): # terms in the product. For example 10000. if n_terms == 0: - return 2.0 # Wallis product with 0 terms is defined as 1, so pi approximation is 2 + # Wallis product with 0 terms is defined as 1, so pi approximation is 2 + return 2.0 - n = np.arange(1, n_terms + 1) # Create an array from 1 to n_terms + n = np.arange(1, n_terms + 1) # Create an array from 1 to n_terms terms = (2 * n / (2 * n - 1)) * (2 * n / (2 * n + 1)) product = np.prod(terms) # Compute the product of all terms - - return 2.0 * product # Return the approximation of pi + + return 2.0 * product # Return the approximation of pi From 2afccaf3d324cba426660ca4d1666ee3ce70f023 Mon Sep 17 00:00:00 2001 From: ethan benhamou Date: Sat, 15 Nov 2025 15:53:12 +0100 Subject: [PATCH 04/10] Complete numpy and sklearn assignment --- sklearn_questions.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/sklearn_questions.py b/sklearn_questions.py index a7c21fe2..f433f4b9 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -43,7 +43,7 @@ def fit(self, X, y): Training data. y : ndarray of shape (n_samples,) Target labels corresponding to X. - + Returns ------- self : object @@ -59,7 +59,7 @@ def fit(self, X, y): def predict(self, X): """Predict class labels for the given samples. - + Parameters ---------- X : ndarray of shape (n_samples, n_features) @@ -82,14 +82,14 @@ def predict(self, X): def score(self, X, y): """Compute the accuracy of the classifier. - + Parameters ---------- X : ndarray of shape (n_samples, n_features) Test samples. y : ndarray of shape (n_samples,) True labels for X. - + Returns ------- score : float From 2d81b01db2336204686bd771cd3621bf6c788ce5 Mon Sep 17 00:00:00 2001 From: ethan benhamou Date: Sat, 15 Nov 2025 15:57:31 +0100 Subject: [PATCH 05/10] Complete numpy and sklearn assignment --- numpy_questions.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index e1257d2e..b33fcd80 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -41,13 +41,13 @@ def max_index(X): j = 0 # TODO - + if not isinstance(X, np.ndarray): raise ValueError("Input is not a numpy array") # Check for numpy array - + if X.ndim != 2: raise ValueError("Input array is not 2D") # Check for 2D shape - + i, j = np.unravel_index(np.argmax(X), X.shape) # Find indices of maximum value return i, j @@ -72,14 +72,14 @@ 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: # Wallis product with 0 terms is defined as 1, so pi approximation is 2 return 2.0 - + n = np.arange(1, n_terms + 1) # Create an array from 1 to n_terms terms = (2 * n / (2 * n - 1)) * (2 * n / (2 * n + 1)) product = np.prod(terms) # Compute the product of all terms - + return 2.0 * product # Return the approximation of pi From 378be1796d7a72340676324827b0749f810ca10a Mon Sep 17 00:00:00 2001 From: ethan benhamou Date: Sat, 15 Nov 2025 16:26:34 +0100 Subject: [PATCH 06/10] Complete numpy and sklearn assignment --- numpy_questions.py | 13 +++++++------ sklearn_questions.py | 12 +++++++++++- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index b33fcd80..e7aaf0af 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -48,8 +48,9 @@ def max_index(X): if X.ndim != 2: raise ValueError("Input array is not 2D") # Check for 2D shape - i, j = np.unravel_index(np.argmax(X), X.shape) # Find indices of maximum value - + # Find indices of maximum value + i, j = np.unravel_index(np.argmax(X), X.shape) + return i, j @@ -74,12 +75,12 @@ def wallis_product(n_terms): # terms in the product. For example 10000. if n_terms == 0: - # Wallis product with 0 terms is defined as 1, so pi approximation is 2 - return 2.0 + # Wallis product with 0 terms is defined as 1 + return 1.0 n = np.arange(1, n_terms + 1) # Create an array from 1 to n_terms terms = (2 * n / (2 * n - 1)) * (2 * n / (2 * n + 1)) product = np.prod(terms) # Compute the product of all terms - return 2.0 * product # Return the approximation of pi - + # Return the approximation of pi + return product diff --git a/sklearn_questions.py b/sklearn_questions.py index f433f4b9..9da5e27f 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -55,6 +55,9 @@ 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): @@ -78,6 +81,13 @@ def predict(self, X): ) # XXX fix + # euclidean distances between each point in X and each point in self.X_ + distances = np.linalg.norm( + self.X_[np.newaxis, :, :] - X[:, np.newaxis, :], + axis=2, + ) + nearest_idx = np.argmin(distances, axis=1) + y_pred = self.y_[nearest_idx] return y_pred def score(self, X, y): @@ -99,4 +109,4 @@ def score(self, X, y): y_pred = self.predict(X) # XXX fix - return y_pred.sum() + return np.mean(y_pred == y) From b558806ff0f2f6c33998afc7009afc8c2bce5014 Mon Sep 17 00:00:00 2001 From: ethan benhamou Date: Sat, 15 Nov 2025 16:28:48 +0100 Subject: [PATCH 07/10] Complete numpy and sklearn assignment --- numpy_questions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/numpy_questions.py b/numpy_questions.py index e7aaf0af..2ce5d50e 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -83,4 +83,4 @@ def wallis_product(n_terms): product = np.prod(terms) # Compute the product of all terms # Return the approximation of pi - return product + return 2.0 * product From f6e8112df9b7633695e7326e95c53358e39aed28 Mon Sep 17 00:00:00 2001 From: ethan benhamou Date: Sat, 15 Nov 2025 16:29:52 +0100 Subject: [PATCH 08/10] Complete numpy and sklearn assignment --- numpy_questions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/numpy_questions.py b/numpy_questions.py index 2ce5d50e..88a1b578 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -76,7 +76,7 @@ def wallis_product(n_terms): if n_terms == 0: # Wallis product with 0 terms is defined as 1 - return 1.0 + return 1.0 n = np.arange(1, n_terms + 1) # Create an array from 1 to n_terms terms = (2 * n / (2 * n - 1)) * (2 * n / (2 * n + 1)) From b74a1d0fe79eb8328dc9a2e7fa9fc722023fca5e Mon Sep 17 00:00:00 2001 From: ethan benhamou Date: Sat, 15 Nov 2025 16:32:13 +0100 Subject: [PATCH 09/10] Complete numpy and sklearn assignment --- numpy_questions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/numpy_questions.py b/numpy_questions.py index 88a1b578..c23bd8d6 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -76,7 +76,7 @@ def wallis_product(n_terms): if n_terms == 0: # Wallis product with 0 terms is defined as 1 - return 1.0 + return 1.0 n = np.arange(1, n_terms + 1) # Create an array from 1 to n_terms terms = (2 * n / (2 * n - 1)) * (2 * n / (2 * n + 1)) From c31678367ec73682d6c48b15548c841933a193fa Mon Sep 17 00:00:00 2001 From: ethan benhamou Date: Sat, 15 Nov 2025 16:34:10 +0100 Subject: [PATCH 10/10] Complete numpy and sklearn assignment --- sklearn_questions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sklearn_questions.py b/sklearn_questions.py index 9da5e27f..43373894 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