From 0dd64e05a5f5d16a709c1bee69dd9c4568163224 Mon Sep 17 00:00:00 2001 From: Brendan O'Sullivan Date: Tue, 10 Nov 2026 16:33:43 +0100 Subject: [PATCH 1/2] Brendan O' Sullivan: assignment complete --- numpy_questions.py | 19 +++++++------ sklearn_questions.py | 66 ++++++++++++++++++++++++++++++++++---------- 2 files changed, 63 insertions(+), 22 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index 21fcec4b..9f67bde0 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -29,7 +29,7 @@ def max_index(X): Returns ------- (i, j) : tuple(int) - The row and columnd index of the maximum. + The row and column index of the maximum. Raises ------ @@ -37,12 +37,12 @@ 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) or X.ndim != 2: + raise ValueError("X must be a 2D numpy array") - # TODO + i, j = np.unravel_index(np.argmax(X), X.shape) - return i, j + return int(i), int(j) def wallis_product(n_terms): @@ -62,6 +62,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 + n = np.arange(1, n_terms + 1, dtype=float) + pi = 2 * np.prod(4 * n ** 2/(4 * n ** 2 - 1)) + + return pi diff --git a/sklearn_questions.py b/sklearn_questions.py index f65038c6..643488d5 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -28,47 +28,85 @@ 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 def fit(self, X, y): - """Write docstring. - - And describe parameters + """Fit the classifier by using the training data. + + Parameters + ---------- + X : ndarray of shape (n_samples, n_features) + The input array. + y : ndarray of shape (n_samples,) + The target array (class labels). + + Returns + ------- + self : OneNearestNeighbor + The fitted estimator. """ X, y = check_X_y(X, y) check_classification_targets(y) self.classes_ = np.unique(y) self.n_features_in_ = X.shape[1] + self.X_ = X + self.y_ = y - # XXX fix return self def predict(self, X): - """Write docstring. + """Predict the class of each sample from its nearest neighbor. + + Parameters + ---------- + X : ndarray of shape (n_samples, n_features) + The input array. - And describe parameters + Returns + ------- + y : ndarray of shape(n_samples,) + The predicted class labels """ 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 ) - # XXX fix + diffs = X[:, np.newaxis, :] - self.X_[np.newaxis, :, :] + distances = np.sqrt((diffs ** 2).sum(axis=2)) + nearest = np.argmin(distances, axis=1) + y_pred[:] = self.y_[nearest] + return y_pred def score(self, X, y): - """Write docstring. - - And describe parameters + """Compute the accuracy of the classifier. + + Parameters + ---------- + X : ndarray of shape (n_samples, n_features) + The input array. + y : ndarray of shape (n_samples,) + The target array (class labels). + + Returns + ------- + score : float + The fraction of correctly classified samples. """ X, y = check_X_y(X, y) y_pred = self.predict(X) - # XXX fix - return y_pred.sum() + return float(np.mean(y_pred == y)) From aafe6e9294b237e5a2ea8a5f45af4e120cc09861 Mon Sep 17 00:00:00 2001 From: Brendan O'Sullivan Date: Tue, 10 Nov 2026 17:09:19 +0100 Subject: [PATCH 2/2] Ajdusted sklearn_questions --- sklearn_questions.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/sklearn_questions.py b/sklearn_questions.py index 1f55be00..46ca5dd1 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -30,11 +30,7 @@ class OneNearestNeighbor(ClassifierMixin, BaseEstimator): -<<<<<<< HEAD """OneNearestNeighbor classifier.""" -======= - "OneNearestNeighbor classifier." ->>>>>>> b2d89059424004be5fd3b7f1d02a049f9ce720dc def __init__(self): # noqa: D107 pass @@ -54,12 +50,12 @@ def fit(self, X, y): self : OneNearestNeighbor The fitted estimator. """ - X, y = check_X_y(X, y) + X, y = validate_data(self, X, y) check_classification_targets(y) self.classes_ = np.unique(y) - self.n_features_in_ = X.shape[1] self.X_ = X self.y_ = y + return self return self