diff --git a/numpy_questions.py b/numpy_questions.py index 21fcec4b..9a9002eb 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,10 +38,21 @@ def max_index(X): If the input is not a numpy array or if the shape is not 2D. """ + if not isinstance(X, np.ndarray): + raise ValueError("X must be a 2D array") + if X.ndim != 2: + raise ValueError("X must be a 2D array") + + n_rows, n_cols = X.shape i = 0 j = 0 - - # TODO + max_val = X[0, 0] + for row in range(n_rows): + for col in range(n_cols): + if X[row, col] > max_val: + max_val = X[row, col] + i = row + j = col return i, j @@ -62,6 +74,20 @@ def wallis_product(n_terms): pi : float The approximation of order `n_terms` of pi using the Wallis product. """ - # XXX : The n_terms is an int that corresponds to the number of - # terms in the product. For example 10000. - return 0. + if not isinstance(n_terms, (int, np.integer)): + raise ValueError("n_terms must be an integer") + if n_terms < 0: + raise ValueError("n_terms must be non-negative") + if n_terms == 0: + return 1.0 + + product = 1.0 + + # Wallis product + + for n in range(1, n_terms + 1): + numerator = 4.0 * n * n + denominator = numerator - 1 + product *= numerator / denominator # this approximates pi/2 + + return 2.0 * product diff --git a/sklearn_questions.py b/sklearn_questions.py index f65038c6..ac2be681 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -19,6 +19,7 @@ for the methods you code and for the class. The docstring will be checked using `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 @@ -29,46 +30,80 @@ class OneNearestNeighbor(BaseEstimator, ClassifierMixin): - "OneNearestNeighbor classifier." + """OneNearestNeighbor classifier.""" def __init__(self): # noqa: D107 pass def fit(self, X, y): - """Write docstring. - - And describe parameters + """Fit the 1-NN classifier. + + Parameters + ---------- + X : array-like of shape (n_samples, n_features) + Training data + y: array-like of shape (n_samples,) + Target labels. + + Returns + ---------- + self : OneNearestNeighbor + Fitted classifier. """ 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 + # Store training data + self.X_ = X + self.y_ = y return self def predict(self, X): - """Write docstring. + """Predict class labels for samples in X. + + Parameters + ---------- + X : array-like of shape (n_samples, n_features) + Input samples. - And describe parameters + Returns + ---------- + y_pred : ndarray of shape (n_samples,) + Predicted class labels. """ check_is_fitted(self) X = check_array(X) y_pred = np.full( - shape=len(X), fill_value=self.classes_[0], - dtype=self.classes_.dtype + shape=len(X), fill_value=self.classes_[0], dtype=self.classes_.dtype ) - # XXX fix + # compute distances to all training samples + diff = X[:, np.newaxis, :] - self.X_[np.newaxis, :, :] + distances = np.linalg.norm(diff, axis=2) + nearest_idx = np.argmin(distances, axis=1) + + # fill y_pred with nearest-neighbor labels + y_pred[:] = self.y_[nearest_idx] return y_pred def score(self, X, y): - """Write docstring. - - And describe parameters + """Compute mean accuracy of the classifier. + + Parameters + ---------- + X: array-like of shape (n_samples, n_features) + Test samples. + y : array-like of shape (n_samples, ) + True labels for X + + Returns + ---------- + score : float + Mean accuracy of predictions on X compared to true labels y """ X, y = check_X_y(X, y) y_pred = self.predict(X) - # XXX fix - return y_pred.sum() + return np.mean(y_pred == y)