From f47a505cfd9475a39c5df9ceefcb4d1f0a6f50e8 Mon Sep 17 00:00:00 2001 From: Anna Docheva Date: Thu, 13 Nov 2025 15:41:16 +0100 Subject: [PATCH 1/3] completed assignment --- numpy_questions.py | 11 +++++++-- sklearn_questions.py | 58 +++++++++++++++++++++++++++++++++++--------- 2 files changed, 55 insertions(+), 14 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index 21fcec4b..ef2143df 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -41,7 +41,11 @@ 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") + i, j = np.unravel_index(np.argmax(X), X.shape) return i, j @@ -64,4 +68,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. - return 0. + product = 1 + for i in range(1, n_terms + 1): + product *= (4 * i**2) / (4 * i**2 - 1) + return product * 2 diff --git a/sklearn_questions.py b/sklearn_questions.py index f65038c6..5d4ea560 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -29,28 +29,47 @@ class OneNearestNeighbor(BaseEstimator, ClassifierMixin): - "OneNearestNeighbor classifier." + """OneNearestNeighbor classifier.""" def __init__(self): # noqa: D107 pass def fit(self, X, y): - """Write docstring. + """Fit the OneNearestNeighbor classifier. - And describe parameters + Parameters + ---------- + X : ndarray of shape (n_samples, n_features) + The input array. + + y : ndarray of shape (n_samples,) + Target labels for the training samples. + + Returns + ------- + self : OneNearestNeighbor + Fitted classifier. """ X, y = check_X_y(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. + """Predict class labels for the input samples. + + Parameters + ---------- + X : ndarray of shape (n_samples, n_features) + Samples for which to predict labels. - And describe parameters + Returns + ------- + y_pred : ndarray of shape (n_samples,) + Predicted class labels. """ check_is_fitted(self) X = check_array(X) @@ -59,16 +78,31 @@ def predict(self, X): dtype=self.classes_.dtype ) - # XXX fix + for i in range(len(X)): + dist = np.linalg.norm(self.X_ - X[i], axis=1) + nearest_idx = np.argmin(dist) + y_pred[i] = self.y_[nearest_idx] return y_pred def score(self, X, y): - """Write docstring. + """Compute the accuracy of the classifier. - And describe parameters + The accuracy is the proportion of correctly classified samples. + + Parameters + ---------- + X : ndarray of shape (n_samples, n_features) + Test samples. + + y : ndarray of shape (n_samples,) + True labels corresponding to the test samples. + + Returns + ------- + score : + Classification accuracy. """ X, y = check_X_y(X, y) y_pred = self.predict(X) - - # XXX fix + y_pred = (y_pred == y).astype(int)/len(y) return y_pred.sum() From 2d819040bbab07915181407b28fc6d3b3829b258 Mon Sep 17 00:00:00 2001 From: Anna Docheva Date: Thu, 13 Nov 2025 15:48:46 +0100 Subject: [PATCH 2/3] numpy questions solved --- numpy_questions.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/numpy_questions.py b/numpy_questions.py index ef2143df..8baf7f77 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -68,6 +68,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 1.0 + product = 1 for i in range(1, n_terms + 1): product *= (4 * i**2) / (4 * i**2 - 1) From 69b893dfe5405005deb0744bbf4969a95393dc00 Mon Sep 17 00:00:00 2001 From: Anna Docheva Date: Thu, 13 Nov 2025 16:09:29 +0100 Subject: [PATCH 3/3] sklearn questions --- sklearn_questions.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/sklearn_questions.py b/sklearn_questions.py index 5d4ea560..138fe9d1 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 @@ -73,6 +73,7 @@ def predict(self, X): """ check_is_fitted(self) X = check_array(X) + self._check_n_features(X, reset=False) y_pred = np.full( shape=len(X), fill_value=self.classes_[0], dtype=self.classes_.dtype @@ -102,6 +103,7 @@ def score(self, X, y): score : Classification accuracy. """ + self._check_n_features(X, reset=False) X, y = check_X_y(X, y) y_pred = self.predict(X) y_pred = (y_pred == y).astype(int)/len(y)