diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 00000000..8850d6ca Binary files /dev/null and b/.DS_Store differ diff --git a/numpy_questions.py b/numpy_questions.py index 21fcec4b..6d175db8 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -15,6 +15,7 @@ This will be enforced with `flake8`. You can check that there is no flake8 errors by calling `flake8` at the root of the repo. """ + import numpy as np @@ -37,12 +38,17 @@ def max_index(X): If the input is not a numpy array or if the shape is not 2D. """ - i = 0 - j = 0 + i = -1 + j = -1 + + if not isinstance(X, np.ndarray): + raise ValueError("Input should be a numpy array.") + if len(X.shape) != 2: + raise ValueError("Input should be a 2D numpy array.") - # TODO + (i, j) = np.unravel_index(X.argmax(), X.shape) - return i, j + return (i, j) def wallis_product(n_terms): @@ -64,4 +70,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.0 + + pi_half = 1 + + for n in range(1, n_terms + 1): + pi_half = pi_half * (4 * n ** 2) / (4 * n ** 2 - 1) + + return 2 * pi_half diff --git a/sklearn_questions.py b/sklearn_questions.py index f65038c6..7d5fa622 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -20,55 +20,89 @@ `pydocstyle` that you can also call at the root of the repo. """ import numpy as np -from sklearn.base import BaseEstimator -from sklearn.base import ClassifierMixin +from sklearn.base import ClassifierMixin, BaseEstimator 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 -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 one nearest neighbor classifier from the training dataset. - And describe parameters + Parameters + ---------- + X : ndarray of shape (n_samples, n_features) + The input array. + + y : ndarray of shape (n_samples) + The target values. """ 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 class labels for new data points X_i. - And describe parameters + Parameters + ---------- + X : ndarray of shape (n_samples, n_features) + The input array. + + Returns + ------- + y_pred : ndarray of shape (n_samples) + The predicted class labels. """ check_is_fitted(self) - X = check_array(X) + X = check_array(X, reset=False) + y_pred = np.full( shape=len(X), fill_value=self.classes_[0], dtype=self.classes_.dtype ) - # XXX fix + for i in range(len(X)): + distances = np.linalg.norm(self.X_ - X[i, :], axis=1) + y_pred[i] = self.y_[np.argmin(distances)] + return y_pred def score(self, X, y): - """Write docstring. + """Return the classifier accuracy on the given test data and labels. + + Parameters + ---------- + X : ndarray of shape (n_samples, n_features) + The input array. - And describe parameters + y : ndarray of shape (n_samples) + The target values. + + Returns + ------- + score : float + The accuracy of the classifier; + number of samples corectly classified. """ X, y = check_X_y(X, y) y_pred = self.predict(X) - # XXX fix - return y_pred.sum() + accurate_points = 0 + for i in range(len(y)): + if y_pred[i] == y[i]: + accurate_points += 1 + + return accurate_points / len(y)