From b71f7ee799d3760cc3b66147eda1628573461617 Mon Sep 17 00:00:00 2001 From: vsoehnchen Date: Thu, 13 Nov 2025 16:34:25 +0100 Subject: [PATCH 1/2] Vincent Soehnchen assignment --- numpy_questions.py | 18 +++++++++++--- sklearn_questions.py | 58 +++++++++++++++++++++++++++++++++----------- 2 files changed, 58 insertions(+), 18 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index 21fcec4b..e9d4bfe7 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -15,6 +15,7 @@ This will be enforced with `flake8`. You can check that there is no flake8 errors by calling `flake8` at the root of the repo. """ + import numpy as np @@ -39,9 +40,11 @@ def max_index(X): """ i = 0 j = 0 - - # TODO - + if not isinstance(X, np.ndarray): + raise ValueError("This value is not a numpy array") + if X.ndim != 2: + raise ValueError("This value has the wrong dimensions") + i, j = np.unravel_index(np.argmax(X, axis=None), X.shape) return i, j @@ -62,6 +65,13 @@ def wallis_product(n_terms): pi : float The approximation of order `n_terms` of pi using the Wallis product. """ + if n_terms < 0: + raise ValueError + if n_terms == 0: + return 1.0 + current = 1.0 + for i in range(1, n_terms+1): + current *= (4 * i**2) / ((4 * i**2)-1) # XXX : The n_terms is an int that corresponds to the number of # terms in the product. For example 10000. - return 0. + return current * 2 diff --git a/sklearn_questions.py b/sklearn_questions.py index f65038c6..7941c040 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -35,22 +35,41 @@ def __init__(self): # noqa: D107 pass def fit(self, X, y): - """Write docstring. + """Fit the One-Nearest-Neighbor classifier. - And describe parameters + Parameters + ---------- + X : array-like of shape (n_samples, n_features) + Training data. + + y : array-like of shape (n_samples,) + Target labels. + + Returns + ------- + self : OneNearestNeighbor + 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] - - # XXX fix + self.X_ = X + self.y_ = y return self def predict(self, X): - """Write docstring. + """Predict class labels for samples in X. + + Parameters + ---------- + X : array-like of shape (n_samples, n_features) + Input samples. - And describe parameters + Returns + ------- + y_pred : ndarray of shape (n_samples,) + Predicted class labels. """ check_is_fitted(self) X = check_array(X) @@ -58,17 +77,28 @@ def predict(self, X): shape=len(X), fill_value=self.classes_[0], dtype=self.classes_.dtype ) - - # XXX fix + X_sq = np.sum(X * X, axis=1, keepdims=True) + Xtr_sq = np.sum(self.X_ * self.X_, axis=1, keepdims=True) + cross = X @ self.X_.T + d2 = X_sq - 2.0 * cross + Xtr_sq.T + nn_idx = np.argmin(d2, axis=1) + y_pred = self.y_[nn_idx] return y_pred def score(self, X, y): - """Write docstring. - - And describe parameters + """Compute the mean accuracy on the given test data and labels. + + Parameters + ---------- + X : array-like of shape (n_samples, n_features) + Test samples. + y : array-like of shape (n_samples,) + True labels. + Returns + ------- + score : float + Mean accuracy of predictions on X compared to y. """ 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 8b566f696822d6ef35890c18d0b226e7cf19a317 Mon Sep 17 00:00:00 2001 From: vsoehnchen Date: Thu, 13 Nov 2025 16:40:09 +0100 Subject: [PATCH 2/2] Vincent Soehnchen assignment --- sklearn_questions.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/sklearn_questions.py b/sklearn_questions.py index 7941c040..3e8ff8b0 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -29,7 +29,25 @@ class OneNearestNeighbor(BaseEstimator, ClassifierMixin): - "OneNearestNeighbor classifier." + """One-Nearest-Neighbor classifier using Euclidean distance. + + The classifier predicts, for each input sample, the label of the single + closest training sample in Euclidean distance. + + Attributes + ---------- + classes_ : ndarray of shape (n_classes,) + Unique class labels seen during `fit`. + + n_features_in_ : int + Number of features seen during `fit`. + + X_ : ndarray of shape (n_samples, n_features) + Training data stored after fitting. + + y_ : ndarray of shape (n_samples,) + Training labels stored after fitting. + """ def __init__(self): # noqa: D107 pass