From 1ddca38cc33bd4c2d754c5f4df6af89d884f35ca Mon Sep 17 00:00:00 2001 From: Eric XU Date: Tue, 10 Nov 2026 15:50:02 +0100 Subject: [PATCH 1/2] I finished haha --- numpy_questions.py | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index 21fcec4b..866204c1 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,12 +38,24 @@ 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 must be a numpy array.") + if X.ndim != 2: + raise ValueError("Input must be a 2D array.") - # TODO + i_max = 0 + j_max = 0 + max_value = 0 + n_samples = len(X) + n_features = len(X[0]) + for i in range(n_samples): + for j in range(n_features): + if X[i, j] > max_value: + max_value = X[i, j] + i_max = i + j_max = j - return i, j + return i_max, j_max def wallis_product(n_terms): @@ -57,11 +70,17 @@ def wallis_product(n_terms): Number of steps in the Wallis product. Note that `n_terms=0` will consider the product to be `1`. + + Returns ------- 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. - return 0. + if n_terms == 0: + return 1 + product = 1 + for n in range(n_terms): + quatre_n_2 = 4 * ((n + 1) ** 2) + product *= quatre_n_2 / (quatre_n_2 - 1) + return product * 2 From 79513c66ec64c2a0de1201315c0e7927ce4b72b1 Mon Sep 17 00:00:00 2001 From: Eric XU Date: Tue, 10 Nov 2026 16:59:09 +0100 Subject: [PATCH 2/2] finished sklearn_questions --- .vscode/settings.json | 7 +++++++ numpy_questions.py | 2 +- sklearn_questions.py | 38 +++++++++++++++++++++++++++++--------- 3 files changed, 37 insertions(+), 10 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 00000000..3e99ede3 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,7 @@ +{ + "python.testing.pytestArgs": [ + "." + ], + "python.testing.unittestEnabled": false, + "python.testing.pytestEnabled": true +} \ No newline at end of file diff --git a/numpy_questions.py b/numpy_questions.py index 866204c1..84f1fa57 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -45,7 +45,7 @@ def max_index(X): i_max = 0 j_max = 0 - max_value = 0 + max_value = -np.inf n_samples = len(X) n_features = len(X[0]) for i in range(n_samples): diff --git a/sklearn_questions.py b/sklearn_questions.py index f65038c6..4cb0f356 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 @@ -28,7 +29,7 @@ from sklearn.utils.multiclass import check_classification_targets -class OneNearestNeighbor(BaseEstimator, ClassifierMixin): +class OneNearestNeighbor(ClassifierMixin, BaseEstimator): "OneNearestNeighbor classifier." def __init__(self): # noqa: D107 @@ -43,23 +44,38 @@ def fit(self, X, y): check_classification_targets(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): """Write docstring. + And describe parameters """ check_is_fitted(self) + X = check_array(X) + + 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 + shape=len(X), fill_value=self.classes_[0], dtype=self.classes_.dtype ) - - # XXX fix + for i, x_i in enumerate(X): + min_d = np.inf + j_min = np.inf + for j, x_j in enumerate(self.X_): + d = np.linalg.norm(x_i - x_j) + if d < min_d: + min_d = d + j_min = j + y_pred[i] = self.y_[j_min] return y_pred def score(self, X, y): @@ -67,8 +83,12 @@ def score(self, X, y): And describe parameters """ + n = len(y) + accuracy = 0 X, y = check_X_y(X, y) y_pred = self.predict(X) + for i in range(n): + if y_pred[i] == y[i]: + accuracy += 1 - # XXX fix - return y_pred.sum() + return accuracy / n