From cdca8e9b39015500b7652e5e832012d32f1f0636 Mon Sep 17 00:00:00 2001 From: chloe vacca Date: Fri, 14 Nov 2025 20:20:29 +0100 Subject: [PATCH 1/4] filled out answers --- numpy_questions.py | 23 +++++++++++++++++- sklearn_questions.py | 56 +++++++++++++++++++++++++++++++++++++------- 2 files changed, 70 insertions(+), 9 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index 21fcec4b..ac3af6bc 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -37,10 +37,20 @@ 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 a NumPy array.") + + if len(X.shape) != 2: + raise ValueError("Input should be a 2D numpy array.") + i = 0 j = 0 # TODO + i, j = np.unravel_index(np.argmax(X, axis=None), X.shape) + # i = int(ind[0]) + # j = int(ind[1]) return i, j @@ -64,4 +74,15 @@ 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.0 + + product = 1.0 + for i in range(1, n_terms + 1): + numerator = 4 * i**2 + denominator = 4 * i**2 - 1 + product *= numerator / denominator + + pi = 2 * product + return pi diff --git a/sklearn_questions.py b/sklearn_questions.py index f65038c6..f0f66313 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -29,15 +29,26 @@ class OneNearestNeighbor(BaseEstimator, ClassifierMixin): - "OneNearestNeighbor classifier." + """OneNearestNeighbor classifier.""" def __init__(self): # noqa: D107 pass def fit(self, X, y): - """Write docstring. + """Fit the model given the training samples X and training targets y. + + Parameters + ---------- + X : ndarray of shape (n_samples, n_features) + Array of the training samples. + y : ndarray of shape (n_samples,) + Array of the training targets. + + Returns + ------- + self : object + The fitted classifier. - And describe parameters """ X, y = check_X_y(X, y) check_classification_targets(y) @@ -45,12 +56,24 @@ def fit(self, X, y): self.n_features_in_ = X.shape[1] # XXX fix + self.X_ = X + self.y_ = y + return self def predict(self, X): - """Write docstring. + """Find index of closest training sample and assign label to target. + + Parameters + ---------- + X : ndarray of shape (n_samples, n_features) + Array of samples to predict. + + Returns + ------- + y_pred : ndarray of shape (n_samples,) + Array of the predicted targets. - And describe parameters """ check_is_fitted(self) X = check_array(X) @@ -60,15 +83,32 @@ def predict(self, X): ) # XXX fix + for i in range(X.shape[0]): + distances = np.linalg.norm(self.X_ - X[i, :], axis=1) + nearest_index = np.argmin(distances) + y_pred[i] = self.y_[nearest_index] return y_pred def score(self, X, y): - """Write docstring. + """Compute the number of correct predictions. + + Parameters + ---------- + X : ndarray of shape (n_samples, n_features) + The test samples. + y : ndarray of shape (n_samples,) + The true labels. + + Returns + ------- + float + Accuracy of classifier. - And describe parameters """ X, y = check_X_y(X, y) y_pred = self.predict(X) # XXX fix - return y_pred.sum() + accuracy = np.mean(y_pred == y) + + return accuracy From b678855e3ec6c3158a1a6dc54cc79e2b49daac41 Mon Sep 17 00:00:00 2001 From: chloe vacca Date: Fri, 14 Nov 2025 20:35:53 +0100 Subject: [PATCH 2/4] fixed sklearn_questions.py pydocstyle --- sklearn_questions.py | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/sklearn_questions.py b/sklearn_questions.py index f0f66313..a3cf3cb5 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -48,14 +48,12 @@ def fit(self, X, y): ------- self : object The fitted classifier. - """ 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 @@ -73,7 +71,6 @@ def predict(self, X): ------- y_pred : ndarray of shape (n_samples,) Array of the predicted targets. - """ check_is_fitted(self) X = check_array(X) @@ -82,7 +79,6 @@ def predict(self, X): dtype=self.classes_.dtype ) - # XXX fix for i in range(X.shape[0]): distances = np.linalg.norm(self.X_ - X[i, :], axis=1) nearest_index = np.argmin(distances) @@ -103,12 +99,10 @@ def score(self, X, y): ------- float Accuracy of classifier. - """ X, y = check_X_y(X, y) y_pred = self.predict(X) - # XXX fix accuracy = np.mean(y_pred == y) - return accuracy + return accuracy \ No newline at end of file From afc6ca03b93ecc7c6f991b00f5de838142bb5bb0 Mon Sep 17 00:00:00 2001 From: chloe vacca Date: Fri, 14 Nov 2025 21:47:14 +0100 Subject: [PATCH 3/4] fixed numpy_questions.py pydocstyle --- numpy_questions.py | 1 - 1 file changed, 1 deletion(-) diff --git a/numpy_questions.py b/numpy_questions.py index ac3af6bc..9e4a3cd7 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -37,7 +37,6 @@ 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 a NumPy array.") From 6235e93d2f5dd97b76b6469c988a67c3d4426b82 Mon Sep 17 00:00:00 2001 From: chloe vacca Date: Sat, 15 Nov 2025 14:47:06 +0100 Subject: [PATCH 4/4] fixed new line at end of sklearn_questions.py --- sklearn_questions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sklearn_questions.py b/sklearn_questions.py index a3cf3cb5..5f995168 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -105,4 +105,4 @@ def score(self, X, y): accuracy = np.mean(y_pred == y) - return accuracy \ No newline at end of file + return accuracy