From f863fbd29e63d126a5e593cb6817f146bcb69bd6 Mon Sep 17 00:00:00 2001 From: sckoott <64733968+sckoott@users.noreply.github.com> Date: Sun, 16 Nov 2025 12:20:52 +0100 Subject: [PATCH 1/2] Part B complete assignment - Scott Mayette --- numpy_questions.py | 46 ++++++++++++++++--- sklearn_questions.py | 103 ++++++++++++++++++++++++++++++++++--------- 2 files changed, 123 insertions(+), 26 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index 21fcec4b..a87e7e96 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -17,7 +17,6 @@ """ import numpy as np - def max_index(X): """Return the index of the maximum in a numpy array. @@ -37,14 +36,43 @@ 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 X.ndim != 2: + raise ValueError("Input must be a 2D array with shape (n_samples, n_features).") i = 0 j = 0 + temp = 0 + current_max = 0 + max_i, max_j = 0, 0 + for i in range(X.shape[0]): + for j in range(X.shape[1]): + temp = X[i,j] + if temp > current_max: + current_max = X[i, j] + max_i, max_j = i, j + return max_i, max_j - # TODO - return i, j +def wallis_product(n_terms): + """Implement the Wallis product to compute an approximation of pi. + + See: + https://en.wikipedia.org/wiki/Wallis_product + + Parameters + ---------- + n_terms : int + Number of steps in the Wallis product. Note that `n_terms=0` will + consider the product to be `1`. + Returns + ------- + pi : float + The approximation of order `n_terms` of pi using the Wallis product. + """ + def wallis_product(n_terms): """Implement the Wallis product to compute an approximation of pi. @@ -62,6 +90,12 @@ 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 n_terms == 0: + return 1.0 + + product = 1.0 + for i in range(1, n_terms+1): + half_pi = (4*(i**2))/(4*(i**2) -1) + product *= half_pi + + return 2*product \ No newline at end of file diff --git a/sklearn_questions.py b/sklearn_questions.py index f65038c6..d2d83e4d 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -24,51 +24,114 @@ from sklearn.base import ClassifierMixin from sklearn.utils.validation import check_X_y from sklearn.utils.validation import check_array -from sklearn.utils.validation import check_is_fitted +from sklearn.utils.validation import validate_data, check_is_fitted from sklearn.utils.multiclass import check_classification_targets -class OneNearestNeighbor(BaseEstimator, ClassifierMixin): +class OneNearestNeighbor(ClassifierMixin, BaseEstimator): "OneNearestNeighbor classifier." def __init__(self): # noqa: D107 pass def fit(self, X, y): - """Write docstring. + """Fit the OneNearestNeighbor classifier. - And describe parameters + This stores the training data so that predictions can be made + by looking for the closest training sample to each new point. + + Parameters + ---------- + X : array-like of shape (n_samples, n_features) + Training input samples. + + y : array-like of shape (n_samples,) + Target labels for each training sample. + + Returns + ------- + self : OneNearestNeighbor + The fitted classifier. """ - X, y = check_X_y(X, y) + X, y = validate_data( + self, + X, + y, + accept_sparse=False, + ensure_2d=True, + ) check_classification_targets(y) + self.X_ = X + self.y_ = y self.classes_ = np.unique(y) - self.n_features_in_ = X.shape[1] - # XXX fix return self def predict(self, X): - """Write docstring. + """Predict class labels for the given samples. + + For each sample in X, the predicted label is the label of the + closest training sample (in Euclidean distance). - And describe parameters + Parameters + ---------- + X : array-like of shape (n_samples, n_features) + Input samples to classify. + + 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 + + X = validate_data( + self, + X, + accept_sparse=False, + ensure_2d=True, + reset=False, ) - # XXX fix + n_samples = X.shape[0] + y_pred = np.empty(n_samples, dtype=self.y_.dtype) + + for i in range(n_samples): + x = X[i] + dists = np.linalg.norm(self.X_ - x, axis=1) + nn_idx = np.argmin(dists) + y_pred[i] = self.y_[nn_idx] + return y_pred def score(self, X, y): - """Write docstring. + """Compute accuracy of the classifier on the given test data. + + The accuracy is the proportion of correctly classified samples. + + Parameters + ---------- + X : array-like of shape (n_samples, n_features) + Test input samples. + + y : array-like of shape (n_samples,) + True labels for X. - And describe parameters + Returns + ------- + score : float + Mean accuracy of predictions on X compared to y. """ - X, y = check_X_y(X, y) - y_pred = self.predict(X) + X, y = validate_data( + self, + X, + y, + accept_sparse=False, + ensure_2d=True, + reset=False, + ) + check_is_fitted(self) - # XXX fix - return y_pred.sum() + y_pred = self.predict(X) + return np.mean(y_pred == y) From 10b1aeae06451b9aeb94afcb329fb1af6433e628 Mon Sep 17 00:00:00 2001 From: sckoott <64733968+sckoott@users.noreply.github.com> Date: Sun, 16 Nov 2025 12:42:25 +0100 Subject: [PATCH 2/2] Fix code style --- numpy_questions.py | 47 ++++++++++++++------------------------------ sklearn_questions.py | 38 ++++++++++++----------------------- 2 files changed, 28 insertions(+), 57 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index a87e7e96..c6734961 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -17,6 +17,7 @@ """ import numpy as np + def max_index(X): """Return the index of the maximum in a numpy array. @@ -28,7 +29,7 @@ def max_index(X): Returns ------- (i, j) : tuple(int) - The row and columnd index of the maximum. + The row and column index of the maximum. Raises ------ @@ -39,40 +40,22 @@ def max_index(X): if not isinstance(X, np.ndarray): raise ValueError("Input must be a numpy array.") if X.ndim != 2: - raise ValueError("Input must be a 2D array with shape (n_samples, n_features).") - i = 0 - j = 0 - temp = 0 - current_max = 0 + raise ValueError( + "Input must be a 2D array with shape (n_samples, n_features)." + ) + + current_max = X[0, 0] max_i, max_j = 0, 0 + for i in range(X.shape[0]): for j in range(X.shape[1]): - temp = X[i,j] - if temp > current_max: + if X[i, j] > current_max: current_max = X[i, j] max_i, max_j = i, j - return max_i, max_j + return max_i, max_j -def wallis_product(n_terms): - """Implement the Wallis product to compute an approximation of pi. - - See: - https://en.wikipedia.org/wiki/Wallis_product - - Parameters - ---------- - n_terms : int - Number of steps in the Wallis product. Note that `n_terms=0` will - consider the product to be `1`. - - Returns - ------- - pi : float - The approximation of order `n_terms` of pi using the Wallis product. - """ - def wallis_product(n_terms): """Implement the Wallis product to compute an approximation of pi. @@ -94,8 +77,8 @@ def wallis_product(n_terms): return 1.0 product = 1.0 - for i in range(1, n_terms+1): - half_pi = (4*(i**2))/(4*(i**2) -1) - product *= half_pi - - return 2*product \ No newline at end of file + for i in range(1, n_terms + 1): + term = (4 * i**2) / (4 * i**2 - 1) + product *= term + + return 2 * product diff --git a/sklearn_questions.py b/sklearn_questions.py index d2d83e4d..dbcc0152 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -24,12 +24,12 @@ from sklearn.base import ClassifierMixin from sklearn.utils.validation import check_X_y from sklearn.utils.validation import check_array -from sklearn.utils.validation import validate_data, check_is_fitted +from sklearn.utils.validation import check_is_fitted from sklearn.utils.multiclass import check_classification_targets class OneNearestNeighbor(ClassifierMixin, BaseEstimator): - "OneNearestNeighbor classifier." + """OneNearestNeighbor classifier.""" def __init__(self): # noqa: D107 pass @@ -53,17 +53,13 @@ def fit(self, X, y): self : OneNearestNeighbor The fitted classifier. """ - X, y = validate_data( - self, - X, - y, - accept_sparse=False, - ensure_2d=True, - ) + X, y = check_X_y(X, y) check_classification_targets(y) + self.X_ = X self.y_ = y self.classes_ = np.unique(y) + self.n_features_in_ = X.shape[1] return self @@ -83,16 +79,15 @@ def predict(self, X): y_pred : ndarray of shape (n_samples,) Predicted class labels. """ - check_is_fitted(self) + X = check_array(X) - X = validate_data( - self, - X, - accept_sparse=False, - ensure_2d=True, - reset=False, - ) + if X.shape[1] != self.n_features_in_: + raise ValueError( + f"X has {X.shape[1]} features, but " + f"{self.__class__.__name__} is expecting " + f"{self.n_features_in_} features as input" + ) n_samples = X.shape[0] y_pred = np.empty(n_samples, dtype=self.y_.dtype) @@ -123,14 +118,7 @@ def score(self, X, y): score : float Mean accuracy of predictions on X compared to y. """ - X, y = validate_data( - self, - X, - y, - accept_sparse=False, - ensure_2d=True, - reset=False, - ) + X, y = check_X_y(X, y) check_is_fitted(self) y_pred = self.predict(X)