diff --git a/numpy_questions.py b/numpy_questions.py index 21fcec4b..5ee62189 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -40,8 +40,15 @@ def max_index(X): i = 0 j = 0 - # TODO + # Raise ValueError + if not isinstance(X, np.ndarray) or X.ndim != 2: + raise ValueError("Input must be a 2D numpy array") + # Find max + max_val_index = np.argmax(X) # returns index of X flattened + + # Find index + i, j = np.unravel_index(max_val_index, X.shape) return i, j @@ -64,4 +71,12 @@ 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 + else: + cumprod = 1 + for n in range(1, n_terms + 1): + prod = (4 * (n ** 2)) / ((4 * (n ** 2)) - 1) + cumprod *= prod + return cumprod * 2 diff --git a/sklearn_questions.py b/sklearn_questions.py index f65038c6..c1af974c 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -22,53 +22,97 @@ import numpy as np 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_X_y +from sklearn.utils.validation import check_array -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 model based on X and y. + + Parameters + ---------- + X: array (n_samples, n_features). Training data. + + y: array (n_samples). Target data. + + Returns + ------- + self: object - And describe parameters """ 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 labels for each x based on Euclidean distance. + + Parameters + ---------- + X: array of test samples - And describe parameters + Returns + ------- + y_pred: array with shape (n_samples, ) of predicted labels """ check_is_fitted(self) X = check_array(X) + + # Required by sklearn's check_estimator + if X.shape[1] != self.n_features_in_: + raise ValueError( + f"X has {X.shape[1]} features, but OneNearestNeighbor " + f"was fitted with {self.n_features_in_} features." + ) + y_pred = np.full( shape=len(X), fill_value=self.classes_[0], dtype=self.classes_.dtype ) - # XXX fix + # Compute distances + for i, x_test in enumerate(X): + distances = np.linalg.norm(self.X_ - x_test, axis=1) + idx = np.argmin(distances) + y_pred[i] = self.y_[idx] + return y_pred def score(self, X, y): - """Write docstring. + """Return the accuracy of the classifier. + + Parameters + ---------- + X: array (n_samples, n_features). Training data. + + y: array (n_samples). Target data. - And describe parameters + y_pred: array with shape (n_samples, ) of predicted labels + + Returns + ------- + accuracy: float, fraction of correct predictions. """ X, y = check_X_y(X, y) - y_pred = self.predict(X) - # XXX fix - return y_pred.sum() + # Same feature check for consistency + if X.shape[1] != self.n_features_in_: + raise ValueError( + f"X has {X.shape[1]} features, but OneNearestNeighbor " + f"was fitted with {self.n_features_in_} features." + ) + + y_pred = self.predict(X) + return np.mean(y_pred == y)