From 14e6f8a1319fb6c2122da57c7c2e704a1ec28fb7 Mon Sep 17 00:00:00 2001 From: juliusenderwitz Date: Sat, 15 Nov 2025 17:19:21 +0100 Subject: [PATCH 1/7] sklearn file completed --- sklearn_questions.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/sklearn_questions.py b/sklearn_questions.py index f65038c6..d7cfd68d 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 @@ -45,6 +45,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): @@ -60,6 +63,12 @@ def predict(self, X): ) # XXX fix + # euclidean distances between X and self.X_ + distances = np.linalg.norm( + self.X_[np.newaxis, :, :] - X[:, np.newaxis, :], axis = 2 + ) + nearest_indices = np.argmin(distances, axis=1) + y_pred = self.y_[nearest_indices] return y_pred def score(self, X, y): From faca65a4baffe182bed66254de13f3d0e8585542 Mon Sep 17 00:00:00 2001 From: juliusenderwitz Date: Sat, 15 Nov 2025 17:34:01 +0100 Subject: [PATCH 2/7] numpy file completed --- numpy_questions.py | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index 21fcec4b..3c3e6bc9 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -40,9 +40,16 @@ def max_index(X): i = 0 j = 0 + if not isinstance(X, np.ndarray): + raise ValueError("Input should be numpy array.") + if X.ndim != 2: + raise ValueError("Input should be 2D array.") + # TODO + max_val = X.max() + i, j = np.where(X == max_val) - return i, j + return i[0], j[0] def wallis_product(n_terms): @@ -64,4 +71,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. + pi_approx = 1 + + if n_terms == 0: + return pi_approx + + for n in range(1, n_terms +1): + pi_approx *= (4 * n * n) / (4 * n * n -1) + + return pi_approx * 2 From 36b729694c1be58b5e979bfb7da596a0ca0924b2 Mon Sep 17 00:00:00 2001 From: juliusenderwitz Date: Sat, 15 Nov 2025 17:49:17 +0100 Subject: [PATCH 3/7] edited numpy file, syntax --- numpy_questions.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index 3c3e6bc9..da0bd3a7 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -15,6 +15,7 @@ This will be enforced with `flake8`. You can check that there is no flake8 errors by calling `flake8` at the root of the repo. """ + import numpy as np @@ -37,9 +38,7 @@ def max_index(X): If the input is not a numpy array or if the shape is not 2D. """ - i = 0 - j = 0 - + if not isinstance(X, np.ndarray): raise ValueError("Input should be numpy array.") if X.ndim != 2: @@ -69,6 +68,7 @@ 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. pi_approx = 1 @@ -76,7 +76,7 @@ def wallis_product(n_terms): if n_terms == 0: return pi_approx - for n in range(1, n_terms +1): - pi_approx *= (4 * n * n) / (4 * n * n -1) + for n in range(1, n_terms + 1): + pi_approx *= (4 * n**2) / (4 * n**2 - 1) - return pi_approx * 2 + return 2 * pi_approx From ba6dcf01f9c6bbf7eb5cae214861de329a36c18d Mon Sep 17 00:00:00 2001 From: juliusenderwitz Date: Sat, 15 Nov 2025 17:56:39 +0100 Subject: [PATCH 4/7] edited sklearn syntax --- sklearn_questions.py | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/sklearn_questions.py b/sklearn_questions.py index d7cfd68d..c5d87a03 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -19,6 +19,7 @@ for the methods you code and for the class. The docstring will be checked using `pydocstyle` that you can also call at the root of the repo. """ + import numpy as np from sklearn.base import BaseEstimator from sklearn.base import ClassifierMixin @@ -35,19 +36,20 @@ 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.X_ = X + self.y_ = 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): @@ -55,29 +57,27 @@ def predict(self, X): And describe parameters """ + check_is_fitted(self) X = check_array(X) - y_pred = np.full( - shape=len(X), fill_value=self.classes_[0], - dtype=self.classes_.dtype - ) - - # XXX fix + # euclidean distances between X and self.X_ distances = np.linalg.norm( - self.X_[np.newaxis, :, :] - X[:, np.newaxis, :], axis = 2 + self.X_[np.newaxis, :, :] - X[:, np.newaxis, :], axis=2 ) nearest_indices = np.argmin(distances, axis=1) y_pred = self.y_[nearest_indices] + 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 - return y_pred.sum() + return np.mean(y_pred == y) From c248fa87a086961b4d4a3525fda7547d19a47607 Mon Sep 17 00:00:00 2001 From: juliusenderwitz Date: Sat, 15 Nov 2025 18:01:53 +0100 Subject: [PATCH 5/7] coding pep8 style corrections --- numpy_questions.py | 4 ++-- sklearn_questions.py | 3 +-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index da0bd3a7..f00ff247 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -38,7 +38,7 @@ 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 should be numpy array.") if X.ndim != 2: @@ -75,7 +75,7 @@ def wallis_product(n_terms): if n_terms == 0: return pi_approx - + for n in range(1, n_terms + 1): pi_approx *= (4 * n**2) / (4 * n**2 - 1) diff --git a/sklearn_questions.py b/sklearn_questions.py index c5d87a03..1f246c90 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -49,7 +49,6 @@ def fit(self, X, y): self.classes_ = np.unique(y) self.n_features_in_ = X.shape[1] - return self def predict(self, X): @@ -60,7 +59,7 @@ def predict(self, X): check_is_fitted(self) X = check_array(X) - + # euclidean distances between X and self.X_ distances = np.linalg.norm( self.X_[np.newaxis, :, :] - X[:, np.newaxis, :], axis=2 From 220f074191d8ba2ef8bcc97618d4de4a2febb0a2 Mon Sep 17 00:00:00 2001 From: juliusenderwitz Date: Sat, 15 Nov 2025 18:06:34 +0100 Subject: [PATCH 6/7] pep8 indentation corrections --- numpy_questions.py | 2 -- sklearn_questions.py | 8 +------- 2 files changed, 1 insertion(+), 9 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index f00ff247..4845bc06 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -38,7 +38,6 @@ 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 should be numpy array.") if X.ndim != 2: @@ -68,7 +67,6 @@ 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. pi_approx = 1 diff --git a/sklearn_questions.py b/sklearn_questions.py index 1f246c90..779511e2 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -19,7 +19,6 @@ for the methods you code and for the class. The docstring will be checked using `pydocstyle` that you can also call at the root of the repo. """ - import numpy as np from sklearn.base import BaseEstimator from sklearn.base import ClassifierMixin @@ -36,12 +35,10 @@ 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.X_ = X @@ -56,7 +53,6 @@ def predict(self, X): And describe parameters """ - check_is_fitted(self) X = check_array(X) @@ -70,13 +66,11 @@ 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) - return np.mean(y_pred == y) + return np.mean(y_pred == y) \ No newline at end of file From 6a934b9d6beb3cb82ab0b5baaa8ba757d5441e58 Mon Sep 17 00:00:00 2001 From: juliusenderwitz Date: Sat, 15 Nov 2025 18:09:07 +0100 Subject: [PATCH 7/7] edited new line at the end of the file --- sklearn_questions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sklearn_questions.py b/sklearn_questions.py index 779511e2..53b9efce 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -73,4 +73,4 @@ def score(self, X, y): X, y = check_X_y(X, y) y_pred = self.predict(X) - return np.mean(y_pred == y) \ No newline at end of file + return np.mean(y_pred == y)