From 24d24a83946e49af68313b7d3d8f6bf158f9a2bc Mon Sep 17 00:00:00 2001 From: Martin Dias Pinto Date: Thu, 13 Nov 2025 21:02:43 +0100 Subject: [PATCH 1/4] Martin Dias Pinto - Part B --- numpy_questions.py | 24 +++++++++-- sklearn_questions.py | 100 ++++++++++++++++++++++++++++++++----------- 2 files changed, 95 insertions(+), 29 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index 21fcec4b..d3d815ff 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -40,7 +40,13 @@ def max_index(X): i = 0 j = 0 - # TODO + if not isinstance(X, np.ndarray): + raise ValueError("Error: input not a numpy array") + if X.ndim != 2: + raise ValueError("Error: input array must be 2D") + + idx = np.argmax(X) + i, j = np.unravel_index(idx, X.shape) return i, j @@ -62,6 +68,16 @@ 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) or n_terms < 0: + raise ValueError("Error: Input invalid") + + if n_terms == 0: + return 1.0 + + product = 1.0 + for k in range(1, n_terms + 1): + num = (2 * k) ** 2 + denom = num - 1 + product *= num / denom + + return 2.0 * product diff --git a/sklearn_questions.py b/sklearn_questions.py index f65038c6..2eda4f0b 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -24,51 +24,101 @@ 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 from sklearn.utils.validation import check_is_fitted from sklearn.utils.multiclass import check_classification_targets +class OneNearestNeighbor(ClassifierMixin, BaseEstimator): + """ + OneNearestNeighbor Classifier. -class OneNearestNeighbor(BaseEstimator, ClassifierMixin): - "OneNearestNeighbor classifier." - - def __init__(self): # noqa: D107 - pass + This estimator implements the 1-Nearest Neighbor classification algorithm. + It predicts the label of a new sample based on the label of the single + closest training sample (nearest neighbor) using Euclidean distance. + Parameters + ---------- + No parameters are needed for this simple implementation. + """ def fit(self, X, y): - """Write docstring. - - And describe parameters """ - X, y = check_X_y(X, y) + Fit the OneNearestNeighbor classifier. + + The fitting process for 1NN simply involves storing the training data, + as this is a non-parametric, lazy learning algorithm. + + Parameters + ---------- + X : array-like of shape (n_samples, n_features) + The training input samples. + y : array-like of shape (n_samples,) + The target values (class labels). + + Returns + ------- + self : object + Returns the instance itself. + """ + + X, y = check_X_y(X, y, ensure_all_finite=True, dtype=np.float64, + estimator=self) check_classification_targets(y) - self.classes_ = np.unique(y) + + self.n_features_in_ = X.shape[1] + self.X_ = X + self.y_ = y + self.classes_ = np.unique(y) - # XXX fix return self def predict(self, X): - """Write docstring. + """ + Predict the class label for the provided data. - And describe parameters + The prediction is the target value of the single nearest neighbor + in the training data, based on Euclidean distance. + + Parameters + ---------- + X : array-like of shape (n_samples, n_features) + The input samples to predict. + + Returns + ------- + y_pred : ndarray of shape (n_samples,) + The predicted class labels for the input samples. """ + 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=X, ensure_all_finite=True, dtype=np.float64, + reset=False) + diff = X[:, np.newaxis, :] - self.X_[np.newaxis, :, :] + + distances_sq = np.sum(diff ** 2, axis=2) + nearest_idx = np.argmin(distances_sq, axis=1) + y_pred = self.y_[nearest_idx] - # XXX fix return y_pred def score(self, X, y): - """Write docstring. - - And describe parameters """ - X, y = check_X_y(X, y) + Return 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 for X. + + Returns + ------- + score : float + Mean accuracy of self.predict(X) vs. y. + """ + y_pred = self.predict(X) + y = check_array(y, ensure_2d=False, ensure_all_finite=True, dtype=None) - # XXX fix - return y_pred.sum() + return np.mean(y_pred == y) \ No newline at end of file From 61d546a26e657bcc05cc92ee466231c5baffa9ac Mon Sep 17 00:00:00 2001 From: Martin Dias Pinto Date: Thu, 13 Nov 2025 22:03:47 +0100 Subject: [PATCH 2/4] Fixed version --- sklearn_questions.py | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/sklearn_questions.py b/sklearn_questions.py index 2eda4f0b..93272b98 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -24,10 +24,10 @@ 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 from sklearn.utils.validation import check_is_fitted from sklearn.utils.multiclass import check_classification_targets + class OneNearestNeighbor(ClassifierMixin, BaseEstimator): """ OneNearestNeighbor Classifier. @@ -40,13 +40,14 @@ class OneNearestNeighbor(ClassifierMixin, BaseEstimator): ---------- No parameters are needed for this simple implementation. """ + + def __init__(self): + pass + def fit(self, X, y): """ Fit the OneNearestNeighbor classifier. - The fitting process for 1NN simply involves storing the training data, - as this is a non-parametric, lazy learning algorithm. - Parameters ---------- X : array-like of shape (n_samples, n_features) @@ -59,12 +60,9 @@ def fit(self, X, y): self : object Returns the instance itself. """ - - X, y = check_X_y(X, y, ensure_all_finite=True, dtype=np.float64, - estimator=self) + X, y = check_X_y(X, y) check_classification_targets(y) - self.n_features_in_ = X.shape[1] self.X_ = X self.y_ = y @@ -76,8 +74,6 @@ def predict(self, X): """ Predict the class label for the provided data. - The prediction is the target value of the single nearest neighbor - in the training data, based on Euclidean distance. Parameters ---------- @@ -89,14 +85,19 @@ def predict(self, X): y_pred : ndarray of shape (n_samples,) The predicted class labels for the input samples. """ - check_is_fitted(self) - X = validate_data(self, X=X, ensure_all_finite=True, dtype=np.float64, - reset=False) - diff = X[:, np.newaxis, :] - self.X_[np.newaxis, :, :] + X = check_array(X) + + if X.shape[1] != self.n_features_in_: + raise ValueError( + f"X has {X.shape[1]} features, but OneNearestNeighbor " + f"is expecting {self.n_features_in_} features as input." + ) - distances_sq = np.sum(diff ** 2, axis=2) - nearest_idx = np.argmin(distances_sq, axis=1) + diff = X[:, None, :] - self.X_[None, :, :] + distances = np.linalg.norm(diff, axis=2) + + nearest_idx = np.argmin(distances, axis=1) y_pred = self.y_[nearest_idx] return y_pred @@ -117,8 +118,7 @@ def score(self, X, y): score : float Mean accuracy of self.predict(X) vs. y. """ - + X, y = check_X_y(X, y) y_pred = self.predict(X) - y = check_array(y, ensure_2d=False, ensure_all_finite=True, dtype=None) - return np.mean(y_pred == y) \ No newline at end of file + return np.mean(y_pred == y) From ef8c082b45f3767d8558ea5ffaeb565df48f8f5b Mon Sep 17 00:00:00 2001 From: Martin Dias Pinto Date: Thu, 13 Nov 2025 22:06:03 +0100 Subject: [PATCH 3/4] Flake8 fix --- sklearn_questions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sklearn_questions.py b/sklearn_questions.py index 93272b98..1e43ab97 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -87,7 +87,7 @@ def predict(self, X): """ check_is_fitted(self) X = check_array(X) - + if X.shape[1] != self.n_features_in_: raise ValueError( f"X has {X.shape[1]} features, but OneNearestNeighbor " From 4d93d04fccd5ae6ab31592fcb11c4417a0cd98c7 Mon Sep 17 00:00:00 2001 From: Martin Dias Pinto Date: Thu, 13 Nov 2025 22:11:23 +0100 Subject: [PATCH 4/4] Pydoc fix --- sklearn_questions.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/sklearn_questions.py b/sklearn_questions.py index 1e43ab97..faa20350 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -42,6 +42,14 @@ class OneNearestNeighbor(ClassifierMixin, BaseEstimator): """ def __init__(self): + """ + Init function. + + Returns + ------- + None. + + """ pass def fit(self, X, y): @@ -74,7 +82,6 @@ def predict(self, X): """ Predict the class label for the provided data. - Parameters ---------- X : array-like of shape (n_samples, n_features)