diff --git a/numpy_questions.py b/numpy_questions.py index 21fcec4b..1132856f 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -29,7 +29,7 @@ def max_index(X): Returns ------- (i, j) : tuple(int) - The row and columnd index of the maximum. + The row and column index of the maximum. Raises ------ @@ -37,12 +37,21 @@ def max_index(X): If the input is not a numpy array or if the shape is not 2D. """ - i = 0 - j = 0 + # Validate input type + if not isinstance(X, np.ndarray): + raise ValueError("X must be a numpy ndarray") - # TODO + # Validate shape + if X.ndim != 2: + raise ValueError("X must be a 2D array") - return i, j + # Find flat index of maximum value + flat_idx = np.argmax(X) + + # Convert flat index to (row, col) + i, j = np.unravel_index(flat_idx, X.shape) + + return int(i), int(j) def wallis_product(n_terms): @@ -62,6 +71,19 @@ 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): + raise ValueError("n_terms must be an integer") + if n_terms < 0: + raise ValueError("n_terms must be non-negative") + + # Special case as required by the tests + if n_terms == 0: + return 1.0 + + product = 1.0 + for n in range(1, n_terms + 1): + # term = (4 n^2) / (4 n^2 - 1) + product *= (4.0 * n * n) / (4.0 * n * n - 1.0) + + # Wallis product converges to π/2 + return 2.0 * product diff --git a/sklearn_questions.py b/sklearn_questions.py index f65038c6..80254d09 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -2,7 +2,6 @@ The goal of this assignment is to implement by yourself a scikit-learn estimator for the OneNearestNeighbor and check that it is working properly. - The nearest neighbor classifier predicts for a point X_i the target y_k of the training sample X_k which is the closest to X_i. We measure proximity with the Euclidean distance. The model will be evaluated with the accuracy (average @@ -10,65 +9,117 @@ `predict` and `score` methods for this class. The code you write should pass the test we implemented. You can run the tests by calling at the root of the repo `pytest test_sklearn_questions.py`. - We also ask to respect the pep8 convention: https://pep8.org. This will be enforced with `flake8`. You can check that there is no flake8 errors by calling `flake8` at the root of the repo. - Finally, you need to write docstring similar to the one in `numpy_questions` 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 -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_X_y, + check_is_fitted, + validate_data, +) from sklearn.utils.multiclass import check_classification_targets +class OneNearestNeighbor(ClassifierMixin, BaseEstimator): + """One Nearest Neighbor classifier. + + This classifier predicts the class of a sample based on the class + of its nearest neighbor in the training set, using Euclidean distance + as the proximity metric. + + Attributes + ---------- + classes_ : ndarray of shape (n_classes,) + The unique class labels in the training data. + n_features_in_ : int + The number of features seen during fit. + X_ : ndarray of shape (n_samples, n_features) + The training input samples. + y_ : ndarray of shape (n_samples,) + The training target values. -class OneNearestNeighbor(BaseEstimator, ClassifierMixin): - "OneNearestNeighbor classifier." + """ def __init__(self): # noqa: D107 pass def fit(self, X, y): - """Write docstring. + """Fit the OneNearestNeighbor classifier. + + This method stores the training data for later use during prediction. + + Parameters + ---------- + X : array-like of shape (n_samples, n_features) + The training input samples. + y : array-like of shape (n_samples,) + The target values (class labels). + + Returns + ------- + self : object + Returns self to allow method chaining. - And describe parameters """ - X, y = check_X_y(X, y) + X, y = validate_data(self, 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. + + For each sample in X, finds the nearest neighbor in the training + set and returns its class label. + + Parameters + ---------- + X : array-like of shape (n_samples, n_features) + The input samples to predict. + + Returns + ------- + y_pred : ndarray of shape (n_samples,) + The predicted class labels. - And describe parameters """ check_is_fitted(self) - X = check_array(X) + X = validate_data(self, X, reset=False) y_pred = np.full( shape=len(X), fill_value=self.classes_[0], dtype=self.classes_.dtype ) - - # XXX fix + for i, x in enumerate(X): + distances = np.sqrt(np.sum((self.X_ - x) ** 2, axis=1)) + nearest_idx = np.argmin(distances) + y_pred[i] = self.y_[nearest_idx] return y_pred def score(self, X, y): - """Write docstring. + """Calculate the accuracy score. + + Computes the mean accuracy of predictions on the given test data. + + Parameters + ---------- + X : array-like of shape (n_samples, n_features) + The input samples. + y : array-like of shape (n_samples,) + The true class labels. + + Returns + ------- + score : float + The mean accuracy of the classifier on the given data. - And describe parameters """ X, y = check_X_y(X, y) y_pred = self.predict(X) - - # XXX fix - return y_pred.sum() + return np.mean(y_pred == y) diff --git a/students.txt b/students.txt index c61f7aab..18d9a89b 100644 --- a/students.txt +++ b/students.txt @@ -78,7 +78,11 @@ Perrotton Pauline X Rouzou Julia X Sekri Juliette Seugnet Alec +<<<<<<< HEAD +Singh Ayush X +======= Singh Ayush X +>>>>>>> upstream/main Smith Oliver X Söhnchen Vincent X Soro Nangounon Mohamed X