diff --git a/numpy_questions.py b/numpy_questions.py index 21fcec4b..d4e2dfd9 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -39,9 +39,12 @@ def max_index(X): """ i = 0 j = 0 - - # TODO - + if not isinstance(X, np.ndarray): + raise ValueError("The X input must be an Array!") + if X.ndim != 2: + raise ValueError("The X input must be 2-Dimensional!") + indexes = np.argmax(X) + i, j = np.unravel_index(indexes, X.shape) return i, j @@ -64,4 +67,11 @@ 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. + wp = 1.0 + if n_terms == 0: + return wp + else: + for n in range(1, n_terms + 1): + wp = wp * (4*(n**2))/(4*(n**2) - 1) + pi = wp * 2 + return pi diff --git a/sklearn_questions.py b/sklearn_questions.py index f65038c6..525bf14f 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -23,34 +23,59 @@ from sklearn.base import BaseEstimator from sklearn.base import ClassifierMixin 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.multiclass import check_classification_targets +from sklearn.utils.validation import check_array +from sklearn.metrics import pairwise_distances_argmin_min -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. + """Fit the OneNearestNeighbor classifier. + + This function stores training data X and the labels y. + + Parameters + ---------- + X : Training data (n_samples, n_features). + + y : Target labels (n_samples). - And describe parameters + Returns + ------- + self : returns the fitted classifier. + + Raises + ------ + ValueError + If X and y have different numbers of samples """ 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_ = np.asarray(X) + self.y_ = np.asarray(y) return self def predict(self, X): - """Write docstring. + """Return the predicted class for a data set in an numpy array. + + Parameters + ---------- + X : ndarray of shape (n_samples, n_features) + The input array. - And describe parameters + Returns + ------- + y_pred : ndarray of shape (n_samples) + The predicted classes for the n_samples. """ check_is_fitted(self) X = check_array(X) @@ -59,16 +84,26 @@ def predict(self, X): dtype=self.classes_.dtype ) - # XXX fix + indexes, _ = pairwise_distances_argmin_min(X, self.X_) + y_pred = self.y_[indexes] return y_pred def score(self, X, y): - """Write docstring. - - And describe parameters + """Return the score of the OneNearestNeighbor on a data set. + + Parameters + ---------- + X : ndarray of shape (n_samples, n_features) + The input array. + y : ndarray of shape (n_samples) + The true classes of the samples. + + Returns + ------- + score : float + The percentage of samples accurately predicted. """ X, y = check_X_y(X, y) y_pred = self.predict(X) - - # XXX fix - return y_pred.sum() + score = np.mean(y_pred == y) + return score