From 094e1baba286dc8a03b4abbc3f341243da011eca Mon Sep 17 00:00:00 2001 From: Gaspard Baumelou Date: Thu, 13 Nov 2025 20:29:58 +0100 Subject: [PATCH 1/3] answering numpy question --- numpy_questions.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/numpy_questions.py b/numpy_questions.py index 21fcec4b..d9ab5247 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -41,6 +41,14 @@ def max_index(X): j = 0 # TODO + if not isinstance(X, np.ndarray): + raise ValueError("The input is not a numpy array") + + if X.ndim != 2: + raise ValueError("The shape is not 2D") + + id = np.argmax(X) + i, j = np.unravel_index(id, X.shape) return i, j @@ -64,4 +72,5 @@ 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. + res = np.prod(np.array([4*n**2/(4*n**2 - 1) for n in range(1, n_terms+1)])) + return 2 * res From 63d72736489366480aa6ecc176b78aab3a1d3069 Mon Sep 17 00:00:00 2001 From: Gaspard Baumelou Date: Thu, 13 Nov 2025 22:31:58 +0100 Subject: [PATCH 2/3] numpy questions adjustments --- numpy_questions.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/numpy_questions.py b/numpy_questions.py index d9ab5247..5560f383 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -72,5 +72,7 @@ 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. + if n_terms == 0: + return 1 res = np.prod(np.array([4*n**2/(4*n**2 - 1) for n in range(1, n_terms+1)])) return 2 * res From dea063701c408d4ac05b4a52c024da9fbc0a0a60 Mon Sep 17 00:00:00 2001 From: Gaspard Baumelou Date: Fri, 14 Nov 2025 00:52:44 +0100 Subject: [PATCH 3/3] answers for sklearn_questions --- sklearn_questions.py | 73 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 64 insertions(+), 9 deletions(-) diff --git a/sklearn_questions.py b/sklearn_questions.py index f65038c6..4d5c0821 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -28,16 +28,28 @@ from sklearn.utils.multiclass import check_classification_targets -class OneNearestNeighbor(BaseEstimator, ClassifierMixin): - "OneNearestNeighbor classifier." +class OneNearestNeighbor(ClassifierMixin, BaseEstimator): + """OneNearestNeighbor classifier.""" def __init__(self): # noqa: D107 pass def fit(self, X, y): - """Write docstring. + """Implement the fitting of a nearest neighbor classifier. + + Parameters + ---------- + X : numpy.darray of shape (number of samples, number of features) + The input array we want to fit on. + + y : numpy.darray of shape (number of samples) + The target of the samples present in X. + + Returns + ------- + self : OneNearestNeighbor + Fitted estimator. - And describe parameters """ X, y = check_X_y(X, y) check_classification_targets(y) @@ -45,12 +57,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. - - And describe parameters + """Implement the prediction of a target y according to the entry X. + + Parameters + ---------- + X : numpy.darray of shape (number of samples, number of features) + The input array we want to predict. + + Returns + ------- + y : numpy.darray of shape (number of samples) + The prediction of the samples in X according to the + nearest neighbor classifier. """ check_is_fitted(self) X = check_array(X) @@ -60,15 +84,46 @@ def predict(self, X): ) # XXX fix + + if X.shape[1] != self.n_features_in_: + raise ValueError( + "X has {} features, but {} is expecting {} features as input" + .format( + X.shape[1], + self.__class__.__name__, + self.n_features_in_, + ) + ) + + diffs = X[:, np.newaxis, :] - self.X_[np.newaxis, :, :] + dist_sq = np.sum(diffs ** 2, axis=2) + + nearest_indices = np.argmin(dist_sq, axis=1) + + y_pred = self.y_[nearest_indices] + return y_pred def score(self, X, y): - """Write docstring. + """Give the score of a prediction. + + Parameters + ---------- + X : numpy.darray of shape (number of samples, number of features) + The samples we test. + + y : numpy.darray of shape (number of samples) + The true labels of the X we try to predict with the classifier. + Returns + ------- + score : float + The mean accuracy of the predictor on the given data and target. - And describe parameters """ X, y = check_X_y(X, y) y_pred = self.predict(X) # XXX fix + y_pred = (y_pred == y).astype(float) / len(y) + return y_pred.sum()