From 5724fd950cf2bc019035baf383145370d8579bd0 Mon Sep 17 00:00:00 2001 From: akilanthevaraja-max Date: Sat, 15 Nov 2025 17:41:53 +0100 Subject: [PATCH 1/3] Part B done Akilan-Thevaraja --- numpy_questions.py | 18 ++++++++++++++++-- sklearn_questions.py | 17 +++++++++++------ 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index 21fcec4b..70d51b60 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -41,8 +41,15 @@ def max_index(X): j = 0 # TODO + if not isinstance(X, np.ndarray): + raise ValueError("Input is not a numpy array.") + if X.ndim != 2: + raise ValueError("Input array is not 2D") - return i, j + flat_idx = np.argmax(X) + i, j = np.unravel_index(flat_idx, X.shape) + + return int(i), int(j) def wallis_product(n_terms): @@ -64,4 +71,11 @@ 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 + + n = np.arange(1, n_terms + 1, dtype=float) + terms =(2 * n / (2 * n -1)) * (2 * n /(2 * n +1)) + product = np.prod(terms) + + return 2.0 * product diff --git a/sklearn_questions.py b/sklearn_questions.py index f65038c6..71909ede 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 @@ -41,6 +41,8 @@ def fit(self, X, y): """ X, y = check_X_y(X, y) check_classification_targets(y) + self.X_train_ = X + self.y_train_ = y self.classes_ = np.unique(y) self.n_features_in_ = X.shape[1] @@ -54,12 +56,15 @@ def predict(self, X): """ check_is_fitted(self) X = check_array(X) - y_pred = np.full( - shape=len(X), fill_value=self.classes_[0], - dtype=self.classes_.dtype - ) + self._check_n_features(X, reset=False) + y_pred = np.empty(X.shape[0], dtype=self.classes_.dtype) # XXX fix + for i in range(len(X)): + distances = np.linalg.norm(self.X_train_ - X[i], axis=1) + nearest_index = np.argmin(distances) + y_pred[i] = self.y_train_[nearest_index] + return y_pred def score(self, X, y): @@ -71,4 +76,4 @@ def score(self, X, y): y_pred = self.predict(X) # XXX fix - return y_pred.sum() + return np.mean(y_pred == y) From 289c07c49a91d8fb05e5b2f37aacd31f90503f5c Mon Sep 17 00:00:00 2001 From: akilanthevaraja-max Date: Sat, 15 Nov 2025 17:53:00 +0100 Subject: [PATCH 2/3] error fixed --- numpy_questions.py | 2 +- sklearn_questions.py | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index 70d51b60..7a26ca59 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -75,7 +75,7 @@ def wallis_product(n_terms): return 1.0 n = np.arange(1, n_terms + 1, dtype=float) - terms =(2 * n / (2 * n -1)) * (2 * n /(2 * n +1)) + terms = (2 * n / (2 * n - 1)) * (2 * n / (2 * n + 1)) product = np.prod(terms) return 2.0 * product diff --git a/sklearn_questions.py b/sklearn_questions.py index 71909ede..51d0bd98 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -42,7 +42,7 @@ def fit(self, X, y): X, y = check_X_y(X, y) check_classification_targets(y) self.X_train_ = X - self.y_train_ = y + self.y_train_ = y self.classes_ = np.unique(y) self.n_features_in_ = X.shape[1] @@ -64,7 +64,6 @@ def predict(self, X): distances = np.linalg.norm(self.X_train_ - X[i], axis=1) nearest_index = np.argmin(distances) y_pred[i] = self.y_train_[nearest_index] - return y_pred def score(self, X, y): From d073f1ede51c0d0d2eb509a2b206a431caba7be7 Mon Sep 17 00:00:00 2001 From: akilanthevaraja-max Date: Sat, 15 Nov 2025 17:56:12 +0100 Subject: [PATCH 3/3] pydocstyle errors fixed --- sklearn_questions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sklearn_questions.py b/sklearn_questions.py index 51d0bd98..229a06ec 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -29,7 +29,7 @@ class OneNearestNeighbor(ClassifierMixin, BaseEstimator): - "OneNearestNeighbor classifier." + """OneNearestNeighbor classifier.""" def __init__(self): # noqa: D107 pass