diff --git a/numpy_questions.py b/numpy_questions.py index 21fcec4b..3d82794c 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -37,12 +37,15 @@ def max_index(X): If the input is not a numpy array or if the shape is not 2D. """ - i = 0 - j = 0 + if not isinstance(X, np.ndarray): + raise ValueError("L'entrée doit être un tableau numpy") - # TODO + if X.ndim != 2: + raise ValueError("Le tableau doit être de dimension 2") - return i, j + i, j = np.unravel_index(np.argmax(X), X.shape) + + return int(i), int(j) def wallis_product(n_terms): @@ -62,6 +65,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 + + n = np.arange(1, n_terms + 1, dtype=float) + + # Formule mathématique simplifiée du terme n : (4n^2) / (4n^2 - 1) + produit = np.prod((4 * n**2) / (4 * n**2 - 1)) + + return float(2.0 * produit) diff --git a/sklearn_questions.py b/sklearn_questions.py index 948c2e54..4f09c51c 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -25,51 +25,88 @@ 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 from sklearn.utils.multiclass import check_classification_targets class OneNearestNeighbor(ClassifierMixin, BaseEstimator): - "OneNearestNeighbor classifier." + """OneNearestNeighbor classifier.""" def __init__(self): # noqa: D107 pass def fit(self, X, y): - """Write docstring. - - And describe parameters + """Fit the OneNearestNeighbor model from the training dataset. + + Parameters + ---------- + X : ndarray of shape (n_samples, n_features) + contains the X features of the training set + y : ndarray of shape (n_samples,) + contains the target values of each line + + Returns + ------- + self : OneNearestNeighbor + 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_ = X + self.y_ = y return self def predict(self, X): - """Write docstring. + """Predict the value of y for the X sample. + + Parameters + ---------- + X : ndarray of shape (n_samples, n_features) + The input samples. - And describe parameters + Returns + ------- + y_pred : ndarray of shape (n_samples,) + Predicted value of y for the X sample. """ 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 " + f"is expecting {self.n_features_in_} features as input." + ) y_pred = np.full( shape=len(X), fill_value=self.classes_[0], dtype=self.classes_.dtype ) - # XXX fix + for i, x_i in enumerate(X): + distance = np.linalg.norm(self.X_ - x_i, axis=1) + min_distance_ind = np.argmin(distance) + y_pred[i] = self.y_[min_distance_ind] + return y_pred def score(self, X, y): - """Write docstring. + """Give a score to our model. + + Parameters + ---------- + X : ndarray of shape (n_samples, n_features) + Contains the X features of the test set. + + y : ndarray of shape (n_samples,) + Contains the target value we want to reach. - And describe parameters + Returns + ------- + score : float + Score of the model. """ X, y = check_X_y(X, y) y_pred = self.predict(X) - # XXX fix - return y_pred.sum() + return float(np.mean(y_pred == y))