From 995606cdfbb351efb399d1b825e3cdf62495078a Mon Sep 17 00:00:00 2001 From: zhaoxxxy_mac14 Date: Tue, 10 Nov 2026 18:22:03 +0100 Subject: [PATCH 1/2] Xiaoyu ZHAO's numpy and nearest neighbor exercises --- numpy_questions.py | 14 +++++++++++--- sklearn_questions.py | 20 +++++++++++++++++--- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index 21fcec4b..75078fff 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -41,6 +41,11 @@ def max_index(X): j = 0 # TODO + if not isinstance(X, np.ndarray) or X.ndim != 2: + raise ValueError("X must be a 2D numpy array.") + + max_position = np.argmax(X) + return np.unravel_index(max_position, X.shape) return i, j @@ -62,6 +67,9 @@ 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. - return 0. + if n_terms == 0: + return 1.0 + + k = np.arange(1, n_terms + 1, dtype=float) + terms = (2 * k) ** 2 / ((2 * k - 1) * (2 * k + 1)) + return 2.0 * np.prod(terms) diff --git a/sklearn_questions.py b/sklearn_questions.py index f65038c6..98324a2e 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -28,8 +28,8 @@ from sklearn.utils.multiclass import check_classification_targets -class OneNearestNeighbor(BaseEstimator, ClassifierMixin): - "OneNearestNeighbor classifier." +class OneNearestNeighbor(ClassifierMixin, BaseEstimator): + """OneNearestNeighbor classifier.""" def __init__(self): # noqa: D107 pass @@ -43,6 +43,8 @@ def fit(self, X, y): check_classification_targets(y) self.classes_ = np.unique(y) self.n_features_in_ = X.shape[1] + self.X_train_ = X + self.y_train_ = y # XXX fix return self @@ -54,11 +56,23 @@ def predict(self, X): """ 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 OneNearestNeighbor is " + f"expecting {self.n_features_in_} features as input." + ) + y_pred = np.full( shape=len(X), fill_value=self.classes_[0], dtype=self.classes_.dtype ) + for i, sample in enumerate(X): + distances = np.sum((self.X_train_ - sample) ** 2, axis=1) + nearest_index = np.argmin(distances) + y_pred[i] = self.y_train_[nearest_index] + # XXX fix return y_pred @@ -71,4 +85,4 @@ def score(self, X, y): y_pred = self.predict(X) # XXX fix - return y_pred.sum() + return np.mean(y_pred == y) From 31eb248ffe2b222a07f63d2cf25550f07d4a57ec Mon Sep 17 00:00:00 2001 From: zhaoxxxy_mac14 Date: Tue, 10 Nov 2026 18:36:31 +0100 Subject: [PATCH 2/2] remove unused validation import --- sklearn_questions.py | 1 - 1 file changed, 1 deletion(-) diff --git a/sklearn_questions.py b/sklearn_questions.py index 2d72f4cc..98324a2e 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -25,7 +25,6 @@ from sklearn.utils.validation import check_X_y from sklearn.utils.validation import check_array from sklearn.utils.validation import check_is_fitted -from sklearn.utils.validation import validate_data from sklearn.utils.multiclass import check_classification_targets