From b1faef2acea55747ece3c25a10552019b4a6ee2b Mon Sep 17 00:00:00 2001 From: omar-karim Date: Thu, 13 Nov 2025 18:18:55 +0100 Subject: [PATCH 1/4] implement max_index and Wallis product --- numpy_questions.py | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index 21fcec4b..7fc4ab45 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 @@ -37,10 +38,21 @@ 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("X must be a 2D array") + if X.ndim != 2: + raise ValueError("X must be a 2D array") + + n_rows, n_cols = X.shape i = 0 j = 0 - - # TODO + max_val = X[0, 0] + for row in range(n_rows): + for col in range(n_cols): + if X[row, col] > max_val: + max_val = X[row, col] + i = row + j = col return i, j @@ -62,6 +74,18 @@ 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 not isinstance(n_terms, (int, np.integer)): + raise ValueError("n_terms must be an integer") + if n_terms < 0: + raise ValueError("n_terms must be non-negative") + + product = 1.0 + + # Wallis product + + for n in range(1, n_terms + 1): + numerator = 4.0 * n * n + denominator = numerator - 1 + product *= numerator / denominator # this approximates pi/2 + + return 2.0 * product From 3a22157801ddb5053b0cb43cd37c9fa702faffa1 Mon Sep 17 00:00:00 2001 From: omar-karim Date: Thu, 13 Nov 2025 20:57:46 +0100 Subject: [PATCH 2/4] complete numpy and sklearn assignment --- numpy_questions.py | 4 ++- sklearn_questions.py | 59 +++++++++++++++++++++++++++++++++++--------- 2 files changed, 50 insertions(+), 13 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index 7fc4ab45..93b6cdfe 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -78,7 +78,9 @@ def wallis_product(n_terms): raise ValueError("n_terms must be an integer") if n_terms < 0: raise ValueError("n_terms must be non-negative") - + if n_terms ==0: + return 1.0 + product = 1.0 # Wallis product diff --git a/sklearn_questions.py b/sklearn_questions.py index f65038c6..f76a2913 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -19,6 +19,7 @@ for the methods you code and for the class. The docstring will be checked using `pydocstyle` that you can also call at the root of the repo. """ + import numpy as np from sklearn.base import BaseEstimator from sklearn.base import ClassifierMixin @@ -35,40 +36,74 @@ def __init__(self): # noqa: D107 pass def fit(self, X, y): - """Write docstring. + """Fit the 1-NN classifier. + + Parameters + + X : array-like of shape (n_samples, n_features) + Training data + y: array-like of shape (n_samples,) + Target labels. - And describe parameters + Returns + + self : OneNearestNeighbor + 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 + # Store training data + 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) 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 + # compute distances to all training samples + diff = X[:, np.newaxis, :] - self.X_[np.newaxis, :, :] + distances = np.linalg.norm(diff, axis=2) + nearest_idx = np.argmin(distances, axis=1) + + # fill y_pred with nearest-neighbor labels + y_pred[:] = self.y_[nearest_idx] return y_pred def score(self, X, y): - """Write docstring. + """Compute mean accuracy of the classifier. + + Parameters + + X: array-like of shape (n_samples, n_features) + Test samples. + y : array-like of shape (n_samples, ) + True labels for X + + Returns - And describe parameters + score : float + Mean accuracy of predictions on X compared to true labels 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 76bca11cef644d8a041b590f01ae8dba072a43fc Mon Sep 17 00:00:00 2001 From: omar-karim Date: Thu, 13 Nov 2025 21:09:36 +0100 Subject: [PATCH 3/4] fix flake8 in numpy_questions.py --- numpy_questions.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index 93b6cdfe..9a9002eb 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -78,9 +78,9 @@ def wallis_product(n_terms): raise ValueError("n_terms must be an integer") if n_terms < 0: raise ValueError("n_terms must be non-negative") - if n_terms ==0: + if n_terms == 0: return 1.0 - + product = 1.0 # Wallis product From 912b5bbe29417e820fbf70df6dbf22bd51a5e8ec Mon Sep 17 00:00:00 2001 From: omar-karim Date: Thu, 13 Nov 2025 21:15:10 +0100 Subject: [PATCH 4/4] white line removals --- sklearn_questions.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/sklearn_questions.py b/sklearn_questions.py index f76a2913..ac2be681 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -30,7 +30,7 @@ class OneNearestNeighbor(BaseEstimator, ClassifierMixin): - "OneNearestNeighbor classifier." + """OneNearestNeighbor classifier.""" def __init__(self): # noqa: D107 pass @@ -39,14 +39,14 @@ def fit(self, X, y): """Fit the 1-NN classifier. 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 classifier. """ @@ -64,12 +64,12 @@ def predict(self, X): """Predict class labels for samples in X. Parameters - + ---------- X : array-like of shape (n_samples, n_features) Input samples. Returns - + ---------- y_pred : ndarray of shape (n_samples,) Predicted class labels. """ @@ -92,14 +92,14 @@ def score(self, X, y): """Compute mean accuracy of the classifier. Parameters - + ---------- X: array-like of shape (n_samples, n_features) Test samples. y : array-like of shape (n_samples, ) True labels for X Returns - + ---------- score : float Mean accuracy of predictions on X compared to true labels y """