From 6ec7671d76e9391d957deaa87c31a3ae088bce56 Mon Sep 17 00:00:00 2001 From: Cyprien Date: Tue, 10 Nov 2026 15:21:52 +0100 Subject: [PATCH 1/2] Numpy questions resolved --- numpy_questions.py | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index 21fcec4b..0040d3c6 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -37,15 +37,30 @@ 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 numpy array") + if X.ndim != 2: + raise ValueError("X must be a 2D array") i = 0 j = 0 - - # TODO - + n_samples, n_features = np.shape(X) + maxi = 0 + for k in range(n_samples): + for l in range(n_features): + if X[k][l] > maxi: + maxi = X[k][l] + i = k + j = l return i, j def wallis_product(n_terms): + if n_terms == 0: + return 1 + pi = 1 + for k in range(1, n_terms+1): + pi *= (4*(k**2))/((4*(k**2))-1) + return 2*pi """Implement the Wallis product to compute an approximation of pi. See: From 434113f64ba17f03c1839e4e2c01f57700f55a1f Mon Sep 17 00:00:00 2001 From: Cyprien Date: Tue, 10 Nov 2026 16:57:53 +0100 Subject: [PATCH 2/2] Sklearn questions completed --- sklearn_questions.py | 58 +++++++++++++++++++++++++++++++++----------- 1 file changed, 44 insertions(+), 14 deletions(-) diff --git a/sklearn_questions.py b/sklearn_questions.py index f65038c6..862a440e 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -35,22 +35,40 @@ def __init__(self): # noqa: D107 pass def fit(self, X, y): - """Write docstring. - - And describe parameters + """Fit the classifier on the training data. + + Parameters + ---------- + X : ndarray of shape (n_samples, n_features) + The training input samples. + y : ndarray of shape (n_samples,) + The target labels. + + Returns + ------- + self : object + The fitted estimator. """ 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 + self.X_train_ = X + self.y_train_ = y return self def predict(self, X): - """Write docstring. + """Predict the class labels for the given samples. + + Parameters + ---------- + X : ndarray of shape (n_samples, n_features) + The input samples. - And describe parameters + Returns + ------- + y_pred : ndarray of shape (n_samples,) + The predicted class for each sample. """ check_is_fitted(self) X = check_array(X) @@ -59,16 +77,28 @@ def predict(self, X): dtype=self.classes_.dtype ) - # XXX fix + for i, x in enumerate(X): + distances = np.linalg.norm(self.X_train_ - x, axis=1) + nearest = np.argmin(distances) + y_pred[i] = self.y_train_[nearest] + return y_pred def score(self, X, y): - """Write docstring. - - And describe parameters + """Return the accuracy of the classifier on the given data. + + Parameters + ---------- + X : ndarray of shape (n_samples, n_features) + The input samples. + y : ndarray of shape (n_samples,) + The true labels. + + Returns + ------- + score : float + The fraction of correctly classified samples. """ X, y = check_X_y(X, y) y_pred = self.predict(X) - - # XXX fix - return y_pred.sum() + return np.mean(y_pred == y)