diff --git a/numpy_questions.py b/numpy_questions.py index 21fcec4b..ce352311 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -40,9 +40,15 @@ def max_index(X): i = 0 j = 0 - # TODO + if not isinstance(X, np.ndarray): + raise ValueError("Error : input must be a numpy array.") + if X.ndim != 2: + raise ValueError("Error : input must be 2D.") - return i, j + index = np.argmax(X) # index in the flattened array + i, j = np.unravel_index(index, X.shape) # convert to (row, col) + + return int(i), int(j) def wallis_product(n_terms): @@ -62,6 +68,13 @@ 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, np.integer)) or n_terms < 0: + raise ValueError("Error : input must be a non negative integer.") + + if n_terms == 0: + return 1 + + k = np.arange(1, n_terms + 1, dtype=float) + terms = (4.0 * k * k) / (4.0 * k * k - 1.0) + prod = np.prod(terms) + return float(2.0 * prod) diff --git a/sklearn_questions.py b/sklearn_questions.py index f65038c6..5cea98f3 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -28,47 +28,85 @@ from sklearn.utils.multiclass import check_classification_targets -class OneNearestNeighbor(BaseEstimator, ClassifierMixin): - "OneNearestNeighbor classifier." +class OneNearestNeighbor(ClassifierMixin, BaseEstimator): + """One-nearest-neighbor classifier. + + This estimator predicts the label of each input sample as the label of + the single closest training sample under the Euclidean distance. + """ def __init__(self): # noqa: D107 pass def fit(self, X, y): - """Write docstring. - - And describe parameters + """Fit the classifier. + + The fitting process for OneNearestNeighbor only means storing + the training data, as it is a lazy learning algorithm. + + Parameters + ---------- + X : array-like of shape (n_samples, n_features) + Training data. + y : array-like of shape (n_samples,) + Target labels. + Returns + ------- + self : + OneNearestNeighbor 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 + self.n_features_in_ = X.shape[1] + self.classes_ = np.unique(y) return self def predict(self, X): - """Write docstring. + """Predict class labels for the provided data. + + Parameters + ---------- + X : array-like of shape (n_samples, n_features) + Input samples. - And describe parameters + Returns + ------- + y_pred : ndarray of shape (n_samples,) + Predicted class labels. """ - check_is_fitted(self) - X = check_array(X) - y_pred = np.full( - shape=len(X), fill_value=self.classes_[0], - dtype=self.classes_.dtype - ) - - # XXX fix + check_is_fitted(self, attributes=("X_", "y_")) + X = self._validate_data(X, reset=False) + X_sq = np.sum(X ** 2, axis=1, keepdims=True) + Xt_sq = np.sum(self.X_ ** 2, axis=1, keepdims=True).T + dist_2 = X_sq + Xt_sq - 2 * (X @ self.X_.T) + nn_index = np.argmin(dist_2, axis=1) + y_pred = self.y_[nn_index] + return y_pred def score(self, X, y): - """Write docstring. - - And describe parameters + """Return 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. + + Returns + ------- + float + Accuracy of ``self.predict(X)`` vs ``y``. """ X, y = check_X_y(X, y) + y = check_array(y, ensure_2d=False) + if y.shape[0] != X.shape[0]: + raise ValueError("X and y have incompatible shapes.") + y_pred = self.predict(X) - # XXX fix - return y_pred.sum() + return float(np.mean(y_pred == y)) diff --git a/students.txt b/students.txt index 393f42dd..d358425e 100644 --- a/students.txt +++ b/students.txt @@ -15,7 +15,7 @@ Burtin Léo Chaabouni Kenza Chauhan Bhavesh Chou Wei-Chieh X -Clot Augustin +Clot Augustin X De Sauvan D'Aramon Ithier X Descazeaud Lucien X Despréaux Maxime X @@ -27,7 +27,7 @@ Du Jing El Bakouri Samy Enderwitz Julius X Etienne Romain X -Finotti Alice +Finotti Alice X Fix Julian X Fouache D'Halloy Martin X Franco--Tetu Erwan X @@ -42,7 +42,7 @@ Hartmann Nick Heimann Carl X Hou Litong X Hourquet Augustin X -Jacobacci Giacomo +Jacobacci Giacomo X Jacquin De Margerie Anatole Jankovic Nina X Kanaan Julien @@ -51,16 +51,16 @@ Keum Hae In X Khaw Tristan X Khazzaka Chloe Korouhanba Khuman Laikhuram X -Lassus Gabin +Lassus Gabin X Lau Martin Alexander X Le Bacon Alexandre X Lefebvre Félix Levin Nikolai X -Liard Eléanor +Liard Eléanor X Liu Guangyue X -Liu Yunxian +Liu Yunxian X Lucille Maximilien X -Mahé Blanche +Mahé Blanche X Martin Justin X Massias Mathurin Massoud Alexandre.....X