From 9d0aadb718d5c9d09a6dfd1a49493492cff75576 Mon Sep 17 00:00:00 2001 From: leopardorque Date: Thu, 13 Nov 2025 16:04:35 +0100 Subject: [PATCH 1/2] first commit --- numpy_questions.py | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index 21fcec4b..de3e4397 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -37,10 +37,16 @@ def max_index(X): If the input is not a numpy array or if the shape is not 2D. """ + if not isinstance(X, np.ndarray): + raise ValueError("Input must be an array") + if X.ndim != 2: + raise ValueError("Array must be 2D") + i = 0 j = 0 - # TODO + i = np.argmax(np.max(X, axis=1)) # index of row with the largest value + j = np.argmax(X[i, :]) # index of max within that row return i, j @@ -64,4 +70,18 @@ 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 + + product = 1 + + + for n in range(1, n_terms + 1): + t = (2 * n / (2 * n - 1)) * (2 * n / (2 * n + 1)) + product *= t + + pi = 2 * product + + return pi + From 5a5d8481c3286777fbb003130e90e2bc8aa14da8 Mon Sep 17 00:00:00 2001 From: leopardorque Date: Sat, 15 Nov 2025 18:49:03 +0100 Subject: [PATCH 2/2] second commit Tristan Khaw --- numpy_questions.py | 6 ++-- sklearn_questions.py | 86 ++++++++++++++++++++++++++++++++++---------- 2 files changed, 70 insertions(+), 22 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index de3e4397..57b53492 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -41,7 +41,7 @@ def max_index(X): raise ValueError("Input must be an array") if X.ndim != 2: raise ValueError("Array must be 2D") - + i = 0 j = 0 @@ -73,9 +73,8 @@ def wallis_product(n_terms): if n_terms == 0: return 1 - - product = 1 + product = 1 for n in range(1, n_terms + 1): t = (2 * n / (2 * n - 1)) * (2 * n / (2 * n + 1)) @@ -84,4 +83,3 @@ def wallis_product(n_terms): pi = 2 * product return pi - diff --git a/sklearn_questions.py b/sklearn_questions.py index f65038c6..4ff0d153 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -29,46 +29,96 @@ class OneNearestNeighbor(BaseEstimator, ClassifierMixin): - "OneNearestNeighbor classifier." + """One-Nearest-Neighbor classifier. + + This classifier assigns to each sample the label of the closest + training point in Euclidean distance. Only a single neighbor is + considered. + """ def __init__(self): # noqa: D107 pass def fit(self, X, y): - """Write docstring. - - And describe parameters + """Fit the OneNearestNeighbor classifier. + + Parameters + ---------- + X : ndarray of shape (n_samples, n_features) + Training data. + + y : ndarray of shape (n_samples,) + Target labels. + + Returns + ------- + self : object + Fitted estimator. + + Raises + ------ + ValueError + If `X` and `y` have incompatible shapes or if `y` + is not a valid classification target. """ 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. - - And describe parameters + """Predict class labels for given samples. + + Parameters + ---------- + X : ndarray of shape (n_samples, n_features) + Input samples. + + Returns + ------- + y_pred : ndarray of shape (n_samples,) + Predicted class labels. + + Raises + ------ + ValueError + If estimator has not been fitted. """ check_is_fitted(self) X = check_array(X) - y_pred = np.full( - shape=len(X), fill_value=self.classes_[0], - dtype=self.classes_.dtype + + distances = np.linalg.norm( + X[:, np.newaxis, :] - self.X_[np.newaxis, :, :], + axis=2, ) - # XXX fix - return y_pred + nearest_idx = np.argmin(distances, axis=1) + + return self.y_[nearest_idx] def score(self, X, y): - """Write docstring. + """Return the accuracy of the classifier. + + Parameters + ---------- + X : ndarray of shape (n_samples, n_features) + Test samples. - And describe parameters + y : ndarray of shape (n_samples,) + True labels. + + Returns + ------- + score : float + Classification accuracy, i.e. the proportion of correct + predictions. """ X, y = check_X_y(X, y) y_pred = self.predict(X) - - # XXX fix - return y_pred.sum() + return np.mean(y_pred == y)