From b9ee0cb8463342dd8a4b5b472b9d21c237677545 Mon Sep 17 00:00:00 2001 From: HitaishiD Date: Thu, 13 Nov 2025 15:29:49 +0100 Subject: [PATCH 1/3] Hitaishi Dhoowooah - Part B --- numpy_questions.py | 19 +++++++++++--- sklearn_questions.py | 59 ++++++++++++++++++++++++++++++++++++-------- 2 files changed, 65 insertions(+), 13 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index 21fcec4b..025c7ed2 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -40,9 +40,15 @@ def max_index(X): i = 0 j = 0 - # TODO + if not isinstance(X, np.ndarray): + raise ValueError("Input must be a numpy array.") + if X.ndim != 2: + raise ValueError("Input array must be 2D.") - return i, j + flat_index = np.argmax(X) + i, j = np.unravel_index(flat_index, X.shape) + + return i.item(), j.item() def wallis_product(n_terms): @@ -64,4 +70,11 @@ 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 + else: + product = 1 + for n in range(1, n_terms+1): + product *= (4*n**2) / (4*n**2 - 1) + return product*2 diff --git a/sklearn_questions.py b/sklearn_questions.py index f65038c6..23cf958d 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -35,40 +35,79 @@ def __init__(self): # noqa: D107 pass def fit(self, X, y): - """Write docstring. + """Fit the OneNearestNeighbot classifier. + + Parameters + ---------- + X : ndarray of shape (n_samples, n_features) + The matrix containing training input samples. + y : ndarray of shape (n_samples) + The matrix of true labels for the input samples. + + Returns + ------- + self : object + The fitted estimator - And describe parameters """ 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. + """Return the predicted target for an input - And describe parameters + Parameters + ---------- + X : ndarray of shape (n_samples, n_features) + The input array whose target is being predicted + + Returns + ------- + y_pred : ndarray of shape (n_samples) + The predicted target for each sample in X. """ - check_is_fitted(self) + check_is_fitted(self), ["X_", "y_"] X = check_array(X) y_pred = np.full( shape=len(X), fill_value=self.classes_[0], dtype=self.classes_.dtype ) - # XXX fix + distances = np.sqrt( + ((X[:, np.newaxis, :] - self.X_[np.newaxis, :, :] + ) ** 2).sum(axis=2) + ) + + nearest_idx = np.argmin(distances, axis=1) + + y_pred = self.y_[nearest_idx] + return y_pred def score(self, X, y): - """Write docstring. + """Return the mean accuracy on the given test data and labels. + + Parameters + ---------- + X : ndarray of shape (n_samples, n_features) + Test samples. + + y : ndarray of shape (n_samples,) + True labels for X. - And describe parameters + Returns + ------- + score : float + Mean accuracy of self.predict(X) with respect to y. """ X, y = check_X_y(X, y) y_pred = self.predict(X) # XXX fix - return y_pred.sum() + return np.mean(y_pred == y) From dd33ce4bf70a842491e8171f20f10a0ee3d2c1c9 Mon Sep 17 00:00:00 2001 From: HitaishiD Date: Thu, 13 Nov 2025 15:36:39 +0100 Subject: [PATCH 2/3] fixed code style --- sklearn_questions.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sklearn_questions.py b/sklearn_questions.py index 23cf958d..06d8bde3 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -29,7 +29,7 @@ class OneNearestNeighbor(BaseEstimator, ClassifierMixin): - "OneNearestNeighbor classifier." + """ OneNearestNeighbor classifier.""" def __init__(self): # noqa: D107 pass @@ -60,7 +60,7 @@ def fit(self, X, y): return self def predict(self, X): - """Return the predicted target for an input + """Return the predicted target for an input. Parameters ---------- From 08a4dcf9451761efddb0213595dde92211019099 Mon Sep 17 00:00:00 2001 From: HitaishiD Date: Thu, 13 Nov 2025 15:39:57 +0100 Subject: [PATCH 3/3] fixed code style --- sklearn_questions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sklearn_questions.py b/sklearn_questions.py index 06d8bde3..961de8b5 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -29,7 +29,7 @@ class OneNearestNeighbor(BaseEstimator, ClassifierMixin): - """ OneNearestNeighbor classifier.""" + """OneNearestNeighbor classifier.""" def __init__(self): # noqa: D107 pass