diff --git a/numpy_questions.py b/numpy_questions.py index 21fcec4b..5560f383 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,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. - return 0. + 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 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()