diff --git a/numpy_questions.py b/numpy_questions.py index 21fcec4b..908d0a9a 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -5,7 +5,7 @@ * Use automated tools to validate the code (`pytest` and `flake8`) * Submit a Pull-Request on github to practice `git`. -The two functions below are skeleton functions. The docstrings explain what +The two functions below are skeleton functions. The docstrings explain wha are the inputs, the outputs and the expected error. Fill the function to complete the assignment. The code should be able to pass the test that we wrote. To run the tests, use `pytest test_numpy_questions.py` at the root of @@ -39,8 +39,10 @@ def max_index(X): """ i = 0 j = 0 - - # TODO + if not isinstance(X, np.ndarray) or X.ndim != 2: + raise ValueError("X must be a 2D numpy.ndarray") + lin_idx = np.argmax(X) + i, j = np.unravel_index(lin_idx, X.shape) return i, j @@ -49,19 +51,28 @@ def wallis_product(n_terms): """Implement the Wallis product to compute an approximation of pi. See: - https://en.wikipedia.org/wiki/Wallis_product + https://en.wikipedia.org/wiki/Wallis_produc Parameters ---------- - n_terms : int + n_terms : in Number of steps in the Wallis product. Note that `n_terms=0` will consider the product to be `1`. Returns ------- - pi : float + pi : floa 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) or n_terms < 0: + raise ValueError("n_terms must be a non-negative integer") + + if n_terms == 0: + return 1.0 + + k = np.arange(1, n_terms + 1, dtype=float) + terms = (4 * k * k) / (4 * k * k - 1) + return 2.0 * float(np.prod(terms, dtype=float)) diff --git a/sklearn_questions.py b/sklearn_questions.py index f65038c6..2083c223 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -20,55 +20,107 @@ `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.utils.validation import check_X_y -from sklearn.utils.validation import check_array -from sklearn.utils.validation import check_is_fitted +from sklearn.base import BaseEstimator, ClassifierMixin +from sklearn.utils.validation import check_is_fitted, check_X_y, check_array from sklearn.utils.multiclass import check_classification_targets -class OneNearestNeighbor(BaseEstimator, ClassifierMixin): - "OneNearestNeighbor classifier." +class OneNearestNeighbor(ClassifierMixin, BaseEstimator): + """OneNearestNeighbor classifier. + + A simple 1-nearest-neighbor classifier using Euclidean distance. + """ def __init__(self): # noqa: D107 pass - def fit(self, X, y): - """Write docstring. + # ----------------- compatibility helpers ----------------- + def _fit_validate_compat(self, X, y): + """Validate X,y and set n_features_in_ (new/old sklearn).""" + try: + # new sklearn exposes _validate_data on estimators + X, y = self._validate_data(X, y) + except AttributeError: + # old sklearn fallback + X, y = check_X_y(X, y) + self.n_features_in_ = X.shape[1] + return X, y + + def _predict_validate_compat(self, X): + """Validate X at predict time; check n_features_in_ if needed.""" + try: + X = self._validate_data(X, reset=False) + except AttributeError: + X = check_array(X) + nfi = getattr(self, "n_features_in_", None) + if nfi is not None and X.shape[1] != nfi: + msg = ( + f"X has {X.shape[1]} features, but " + f"{self.__class__.__name__} is expecting " + f"{nfi} features as input" + ) + raise ValueError(msg) + + return X + # --------------------------------------------------------- - And describe parameters + def fit(self, X, y): + """Fit the classifier. + + Parameters + ---------- + X : array-like of shape (n_samples, n_features) + Training samples. + y : array-like of shape (n_samples,) + Target labels. + + Returns + ------- + self : OneNearestNeighbor + Fitted estimator. """ - X, y = check_X_y(X, y) + X, y = self._fit_validate_compat(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 class labels for samples in X. - And describe parameters + Parameters + ---------- + X : array-like of shape (n_samples, n_features) + + Returns + ------- + y_pred : ndarray of shape (n_samples,) """ - check_is_fitted(self) - X = check_array(X) - y_pred = np.full( - shape=len(X), fill_value=self.classes_[0], - dtype=self.classes_.dtype - ) + check_is_fitted(self, attributes=["X_", "y_"]) + X = self._predict_validate_compat(X) - # XXX fix - return y_pred + # squared distances between X (n,d) and self.X_ (m,d) + diff = X[:, None, :] - self.X_[None, :, :] + dist2 = (diff ** 2).sum(axis=2) + nn_idx = np.argmin(dist2, axis=1) + return self.y_[nn_idx] def score(self, X, y): - """Write docstring. + """Return the mean accuracy on the given test data and labels.""" + try: + X, y = self._validate_data(X, y, reset=False) + except AttributeError: + X_chk, y_chk = check_X_y(X, y) + nfi = getattr(self, "n_features_in_", None) + if nfi is not None and X_chk.shape[1] != nfi: + msg = ( + f"X has {X_chk.shape[1]} features, but " + f"{self.__class__.__name__} is expecting " + f"{nfi} features as input" + ) + raise ValueError(msg) + X, y = X_chk, y_chk - And describe parameters - """ - X, y = check_X_y(X, y) y_pred = self.predict(X) - - # XXX fix - return y_pred.sum() + return float(np.mean(y_pred == y))