From 8b2a332d316d40466c888f39fde250ebf72405ee Mon Sep 17 00:00:00 2001 From: Augustin Hourquet Date: Thu, 13 Nov 2025 16:29:48 +0100 Subject: [PATCH 1/4] Augustin Hourquet assignment PR --- numpy_questions.py | 19 ++++++++++--- sklearn_questions.py | 67 +++++++++++++++++++++++++++++++++----------- 2 files changed, 66 insertions(+), 20 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index 21fcec4b..acf8e2a3 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -39,9 +39,12 @@ def max_index(X): """ i = 0 j = 0 - - # TODO - + if not isinstance(X, np.ndarray): + raise ValueError("The X input must be an Array!") + if X.ndim != 2: + raise ValueError("The X input must be 2-Dimensional!") + indexes = np.argmax(X) + i, j = np.unravel_index(indexes, X.shape) return i, j @@ -64,4 +67,12 @@ 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. + wp = 1.0 + + if n_terms == 0: + return wp + else: + for n in range(1, n_terms + 1): + wp = wp * (4*(n**2))/(4*(n**2) - 1) + pi = wp * 2 + return pi diff --git a/sklearn_questions.py b/sklearn_questions.py index f65038c6..dfd43621 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -23,52 +23,87 @@ from sklearn.base import BaseEstimator 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.multiclass import check_classification_targets +from sklearn.utils.validation import validate_data +from sklearn.metrics import pairwise_distances_argmin_min -class OneNearestNeighbor(BaseEstimator, ClassifierMixin): +class OneNearestNeighbor(ClassifierMixin, BaseEstimator): "OneNearestNeighbor classifier." def __init__(self): # noqa: D107 pass def fit(self, X, y): - """Write docstring. - - And describe parameters + """Fit the classifier. This function stores training data X and the + labels y. + + Parameters + ---------- + X : Training data (n_samples, n_features) + y : Target labels (n_samples) + + Returns + ------- + self : returns the fitted classifier + + Raises + ------ + ValueError + If X and y have different numbers of samples """ - X, y = check_X_y(X, y) + X, y = validate_data(self, X, y) check_classification_targets(y) self.classes_ = np.unique(y) self.n_features_in_ = X.shape[1] - # XXX fix + self.X_ = np.asarray(X) + self.y_ = np.asarray(y) return self def predict(self, X): - """Write docstring. + """Return the predicted class for a data set in an numpy array. + + Parameters + ---------- + X : ndarray of shape (n_samples, n_features) + The input array. - And describe parameters + Returns + ------- + y_pred : ndarray of shape (n_samples) + The predicted classes for the n_samples. """ + check_is_fitted(self) - X = check_array(X) + X = validate_data(self, X, reset=False) y_pred = np.full( shape=len(X), fill_value=self.classes_[0], dtype=self.classes_.dtype ) - # XXX fix + indexes, _ = pairwise_distances_argmin_min(X, self.X_) + y_pred = self.y_[indexes] return y_pred def score(self, X, y): - """Write docstring. - - And describe parameters + """Return the score of the OneNearestNeighbor on a data set + + Parameters + ---------- + X : ndarray of shape (n_samples, n_features) + The input array. + y : ndarray of shape (n_samples) + The true classes of the samples. + + Returns + ------- + score : float + The percentage of samples accurately predicted. """ X, y = check_X_y(X, y) y_pred = self.predict(X) - # XXX fix - return y_pred.sum() + score = np.mean(y_pred == y) + return score From aa15a9c961f4a73f6a56a7dc5bc8fb1e6f9a5b09 Mon Sep 17 00:00:00 2001 From: Augustin Hourquet Date: Fri, 14 Nov 2025 22:41:25 +0100 Subject: [PATCH 2/4] Augustin Hourquet assignment submission, corrected validate_data for check_x_y and check_array --- numpy_questions.py | 1 - sklearn_questions.py | 26 ++++++++++++++------------ 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index acf8e2a3..d4e2dfd9 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -68,7 +68,6 @@ 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. wp = 1.0 - if n_terms == 0: return wp else: diff --git a/sklearn_questions.py b/sklearn_questions.py index dfd43621..6391a1c9 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -25,35 +25,38 @@ from sklearn.utils.validation import check_X_y from sklearn.utils.validation import check_is_fitted from sklearn.utils.multiclass import check_classification_targets -from sklearn.utils.validation import validate_data +from sklearn.utils.validation import check_array from sklearn.metrics import pairwise_distances_argmin_min class OneNearestNeighbor(ClassifierMixin, BaseEstimator): - "OneNearestNeighbor classifier." + """OneNearestNeighbor classifier.""" def __init__(self): # noqa: D107 pass def fit(self, X, y): - """Fit the classifier. This function stores training data X and the - labels y. + """Fit the OneNearestNeighbor classifier. + + This function stores training data X and the labels y. Parameters ---------- - X : Training data (n_samples, n_features) - y : Target labels (n_samples) + X : Training data (n_samples, n_features). + + y : Target labels (n_samples). Returns ------- - self : returns the fitted classifier + self : returns the fitted classifier. Raises ------ ValueError If X and y have different numbers of samples """ - X, y = validate_data(self, X, y) + + X, y = check_X_y(X, y) check_classification_targets(y) self.classes_ = np.unique(y) self.n_features_in_ = X.shape[1] @@ -63,7 +66,7 @@ def fit(self, X, y): return self def predict(self, X): - """Return the predicted class for a data set in an numpy array. + """Returns the predicted class for a data set in an numpy array. Parameters ---------- @@ -77,7 +80,7 @@ def predict(self, X): """ check_is_fitted(self) - X = validate_data(self, X, reset=False) + X = check_array(X) y_pred = np.full( shape=len(X), fill_value=self.classes_[0], dtype=self.classes_.dtype @@ -88,7 +91,7 @@ def predict(self, X): return y_pred def score(self, X, y): - """Return the score of the OneNearestNeighbor on a data set + """Returns the score of the OneNearestNeighbor on a data set. Parameters ---------- @@ -104,6 +107,5 @@ def score(self, X, y): """ X, y = check_X_y(X, y) y_pred = self.predict(X) - score = np.mean(y_pred == y) return score From a1618ec433cec9752546980eabc18808d448a3e6 Mon Sep 17 00:00:00 2001 From: Augustin Hourquet Date: Fri, 14 Nov 2025 22:45:24 +0100 Subject: [PATCH 3/4] Augustin Hourquet assignment submission, adjusted style. --- sklearn_questions.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/sklearn_questions.py b/sklearn_questions.py index 6391a1c9..ca61ad07 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -36,8 +36,8 @@ def __init__(self): # noqa: D107 pass def fit(self, X, y): - """Fit the OneNearestNeighbor classifier. - + """Fit the OneNearestNeighbor classifier. + This function stores training data X and the labels y. Parameters @@ -55,7 +55,7 @@ def fit(self, X, y): ValueError If X and y have different numbers of samples """ - + X, y = check_X_y(X, y) check_classification_targets(y) self.classes_ = np.unique(y) From d25f9a3e5960b5c78d191b74276cffa1545cc672 Mon Sep 17 00:00:00 2001 From: Augustin Hourquet Date: Fri, 14 Nov 2025 22:49:31 +0100 Subject: [PATCH 4/4] Augustin Hourquet assignment submission, adjusted docstring. --- sklearn_questions.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/sklearn_questions.py b/sklearn_questions.py index ca61ad07..525bf14f 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -55,7 +55,6 @@ def fit(self, X, y): ValueError If X and y have different numbers of samples """ - X, y = check_X_y(X, y) check_classification_targets(y) self.classes_ = np.unique(y) @@ -66,7 +65,7 @@ def fit(self, X, y): return self def predict(self, X): - """Returns the predicted class for a data set in an numpy array. + """Return the predicted class for a data set in an numpy array. Parameters ---------- @@ -78,7 +77,6 @@ def predict(self, X): y_pred : ndarray of shape (n_samples) The predicted classes for the n_samples. """ - check_is_fitted(self) X = check_array(X) y_pred = np.full( @@ -91,7 +89,7 @@ def predict(self, X): return y_pred def score(self, X, y): - """Returns the score of the OneNearestNeighbor on a data set. + """Return the score of the OneNearestNeighbor on a data set. Parameters ----------