diff --git a/numpy_questions.py b/numpy_questions.py index 21fcec4b..90f64811 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -42,6 +42,16 @@ def max_index(X): # 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") + + for a in range(X.shape[0]): + for b in range(X.shape[1]): + if X[a, b] > X[i, j]: + i, j = a, b + return i, j @@ -64,4 +74,13 @@ 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 + + product = 1 + for i in range(1, n_terms + 1): + four_n_squared = 4 * i * i + product *= four_n_squared / (four_n_squared - 1) + + return 2 * product diff --git a/sklearn_questions.py b/sklearn_questions.py index 948c2e54..424df9de 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -25,33 +25,56 @@ 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. + + Predict for each sample the target of the closest training sample, + using the Euclidean distance. + """ def __init__(self): # noqa: D107 pass def fit(self, X, y): - """Write docstring. + """Store the training data to fit the classifier. + + Parameters + ---------- + X = ndarray of shape (n_samples, n_features) + The training sample, input array. + + y = ndarray of shape (n_samples,) + The training target, input array. - And describe parameters + Returns + ------- + self : OneNearestNeighbor """ 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 target of each sample. + + Parameters + ---------- + X = ndarray of shape (n_samples, n_features) + The training sample, input array. + + Returns + ------- + y_pred : nd array of shape (n_samples, ) + The target of the closest training sample for each sample. - And describe parameters """ check_is_fitted(self) X = check_array(X) @@ -61,15 +84,37 @@ def predict(self, X): ) # XXX fix + + if X.shape[1] != self.n_features_in_: + raise ValueError( + f"X has {X.shape[1]} features, but " + f"{self.__class__.__name__} is expecting " + f"{self.n_features_in_} features as input" + ) + for i, x in enumerate(X): + distances = np.sum((self.X_train_ - x) ** 2, axis=1) + y_pred[i] = self.y_train_[np.argmin(distances)] + return y_pred def score(self, X, y): - """Write docstring. + """Compute the accuracy of the model. + + Parameters + ---------- + X = ndarray of shape (n_samples, n_features) + The training sample, input array. + + y = ndarray of shape (n_samples,) + The training target, input array. - And describe parameters + Returns + ------- + score : float + The fraction of the samples correctly classified """ X, y = check_X_y(X, y) y_pred = self.predict(X) # XXX fix - return y_pred.sum() + return np.mean(y_pred == y)