From a6b113db518291d3afdafda940c8b07050ee9aa3 Mon Sep 17 00:00:00 2001 From: Alec Seugnet Date: Sun, 16 Nov 2025 19:11:32 +0100 Subject: [PATCH 1/4] changes to numpy.questions --- numpy_questions.py | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index 21fcec4b..6a948a88 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -40,8 +40,19 @@ def max_index(X): i = 0 j = 0 - # TODO + if type(X) != np.ndarray: + raise ValueError("X must be a numpy array") + if len(X.shape) != 2: + raise ValueError('X must be a 2D array') + + value_max = X[0, 0] + + for row in range(X.shape[0]): + for col in range(X.shape[1]): + if X[row, col] > value_max: + value_max = X[row, col] + i, j = row, col return i, j @@ -62,6 +73,12 @@ 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 + + prod = 1.0 + + for n in range(1, n_terms + 1): + prod *= (4 * n ** 2) / (4 * n ** 2 - 1) + + return 2 * prod # * by 2 to get pi From 7b5fa507917c1a1d0e6e5a83d9a467f5dba1c331 Mon Sep 17 00:00:00 2001 From: Alec Seugnet Date: Sun, 16 Nov 2025 19:15:06 +0100 Subject: [PATCH 2/4] quick fix --- numpy_questions.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/numpy_questions.py b/numpy_questions.py index 6a948a88..ede35a35 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -53,6 +53,7 @@ def max_index(X): if X[row, col] > value_max: value_max = X[row, col] i, j = row, col + return i, j @@ -74,6 +75,7 @@ def wallis_product(n_terms): The approximation of order `n_terms` of pi using the Wallis product. """ if n_terms == 0: + return 1.0 prod = 1.0 From b3bca9c51cd270918da8535377cc086ea837f5ba Mon Sep 17 00:00:00 2001 From: Alec Seugnet Date: Sun, 16 Nov 2025 19:19:20 +0100 Subject: [PATCH 3/4] quick fix 2 --- numpy_questions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/numpy_questions.py b/numpy_questions.py index ede35a35..160ff7ea 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -40,7 +40,7 @@ def max_index(X): i = 0 j = 0 - if type(X) != np.ndarray: + if not isinstance(X, np.ndarray): raise ValueError("X must be a numpy array") if len(X.shape) != 2: From 8acc6c0d026674fab23e040e62e968044dc03c8e Mon Sep 17 00:00:00 2001 From: Alec Seugnet Date: Sun, 16 Nov 2025 19:42:40 +0100 Subject: [PATCH 4/4] Changes to sklearn_questions --- sklearn_questions.py | 87 +++++++++++++++++++++++++++++++++++--------- 1 file changed, 69 insertions(+), 18 deletions(-) diff --git a/sklearn_questions.py b/sklearn_questions.py index f65038c6..9bddd601 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -28,47 +28,98 @@ from sklearn.utils.multiclass import check_classification_targets -class OneNearestNeighbor(BaseEstimator, ClassifierMixin): - "OneNearestNeighbor classifier." +class OneNearestNeighbor(ClassifierMixin, BaseEstimator): + """One-nearest-neighbor classifier. + + This classifier predicts (for each input sample) the target value of the + closest training sample according to the Euclidean distance. + """ def __init__(self): # noqa: D107 + pass def fit(self, X, y): - """Write docstring. - - And describe parameters + """ + Fit the OneNearestNeighbor classifier according to X, y. + + Parameters + ---------- + X : ndarray of shape (n_samples, n_features) + Training data, with n_samples the number of samples and + n_features the number of features. + y : ndarray of shape (n_samples,) + Target data, with n_samples the number of samples + + Returns + ------- + self : object + 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. - - And describe parameters + """ + Predict the labels based on X with the NearestNeighbor Estimator. + + Parameters + ---------- + X : ndarray of shape (n_samples, n_features) + Data to predict, with n_samples the number of samples and + n_features the number of features. + + Returns + ------- + y_pred : ndarray of shape (n_samples,) + Predicted values for X. """ check_is_fitted(self) + X = check_array(X) + + if X.shape[1] != self.n_features_in_: + # message must match sklearn's expected regex + raise ValueError( + f"X has {X.shape[1]} features, but OneNearestNeighbor " + 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 in range(len(X)): + d = np.linalg.norm(self.X_ - X[i, :], axis=1) + nearest_index = d.argmin() + y_pred[i] = self.y_[nearest_index] + return y_pred def score(self, X, y): - """Write docstring. - - And describe parameters + """ + Score the prediction with the predict function. + + Parameters + ---------- + X : ndarray of shape (n_sample, n_features) + Data to predict. + y : ndarray of shape (n_sample, ) + Targeted data. + Returns + ------- + score : float + Mean accuracy of the model on the X, y dataset. """ X, y = check_X_y(X, y) y_pred = self.predict(X) - - # XXX fix - return y_pred.sum() + y_pred = (y_pred == y) + return y_pred.sum() / len(y_pred)