diff --git a/numpy_questions.py b/numpy_questions.py index 21fcec4b..0c2b4506 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -37,10 +37,13 @@ def max_index(X): If the input is not a numpy array or if the shape is not 2D. """ - i = 0 - j = 0 + if not isinstance(X, np.ndarray): + raise ValueError("Input must be a numpy array.") + if X.ndim != 2: + raise ValueError("Input must be a 2D array.") - # TODO + flat_index = np.argmax(X) + i, j = np.unravel_index(flat_index, X.shape) return i, j @@ -48,9 +51,6 @@ def max_index(X): def wallis_product(n_terms): """Implement the Wallis product to compute an approximation of pi. - See: - https://en.wikipedia.org/wiki/Wallis_product - Parameters ---------- n_terms : int @@ -62,6 +62,11 @@ 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 n_terms == 0: + return 1.0 + product = 1.0 + + for k in range(1, n_terms + 1): + product *= (4 * k * k) / (4 * k * k - 1) + + return 2 * product diff --git a/sklearn_questions.py b/sklearn_questions.py index f65038c6..9df31309 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -20,55 +20,96 @@ `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.utils.multiclass import check_classification_targets +# Try importing validate_data from newer sklearn +try: + from sklearn.utils.validation import validate_data +except ImportError: + # Fallback validate_data for older sklearn versions (used on CI) + def validate_data(estimator, X, y=None, **kwargs): + """ + Fallback implementation of validate_data for older sklearn versions. + + Parameters + ---------- + estimator : estimator instance + The estimator calling this function. + X : array-like + Input data. + y : array-like, optional + Target values. + kwargs : dict + Additional arguments (e.g., dtype, ensure_2d, reset). + """ + # Remove unsupported kwargs for check_array/check_X_y + kwargs.pop("reset", None) + + if y is not None: + X, y = check_X_y(X, y, **kwargs) + estimator.n_features_in_ = X.shape[1] + return X, y + else: + X_checked = check_array(X, **kwargs) + return X_checked -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.""" + # Validate input (numeric only) + X, y = check_X_y(X, y, dtype="numeric") - And describe parameters - """ - X, y = check_X_y(X, y) check_classification_targets(y) + + # Store training data + self.X_ = X + self.y_ = y + + # Required sklearn attribute self.classes_ = np.unique(y) + + # Required for compatibility with predict self.n_features_in_ = X.shape[1] - # XXX fix return self def predict(self, X): - """Write docstring. - - And describe parameters - """ + """Predict class labels using 1-nearest neighbor.""" check_is_fitted(self) - X = check_array(X) - y_pred = np.full( - shape=len(X), fill_value=self.classes_[0], - dtype=self.classes_.dtype + + # Validate input and check feature consistency + X = validate_data( + self, X, + dtype="numeric", + ensure_2d=True, + reset=False ) - # XXX fix + n_test = X.shape[0] + y_pred = np.empty(n_test, dtype=self.y_.dtype) + + for i in range(n_test): + distances = np.sum((self.X_ - X[i]) ** 2, axis=1) + nn_index = np.argmin(distances) + y_pred[i] = self.y_[nn_index] + return y_pred def score(self, X, y): - """Write docstring. - - And describe parameters - """ - X, y = check_X_y(X, y) + """Return mean accuracy.""" + X, y = check_X_y(X, y, dtype="numeric") y_pred = self.predict(X) - - # XXX fix - return y_pred.sum() + return np.mean(y_pred == y)