From 906f92dca449dccef4a5a36b0a12bc8e1beadb1f Mon Sep 17 00:00:00 2001 From: oli Date: Thu, 13 Nov 2025 15:13:57 +0100 Subject: [PATCH 1/2] both exercised solved --- numpy_questions.py | 26 ++++++++++++-- sklearn_questions.py | 80 +++++++++++++++++++++++++++++++++++--------- 2 files changed, 88 insertions(+), 18 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index 21fcec4b..0294318c 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -37,12 +37,25 @@ 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("Input must be a numpy array") + + if X.ndim != 2: + raise ValueError("Input must be a 2D array, but got shape: {}" + .format(X.shape)) + i = 0 j = 0 + num_rows, num_cols = X.shape - # TODO + max = 0 + for i in range(num_cols): + for j in range(num_rows): + if X[i][j] > max: + max = X[i][j] + res = (i, j) - return i, j + return res def wallis_product(n_terms): @@ -64,4 +77,11 @@ 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 + res = 1 + for i in range(1, n_terms+1): + res *= (4 * i**2) / (4 * i**2 - 1) + + return res * 2 diff --git a/sklearn_questions.py b/sklearn_questions.py index f65038c6..7db7d5e6 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -28,47 +28,97 @@ from sklearn.utils.multiclass import check_classification_targets -class OneNearestNeighbor(BaseEstimator, ClassifierMixin): - "OneNearestNeighbor classifier." +class OneNearestNeighbor(ClassifierMixin, BaseEstimator): + """OneNearestNeighbor classifier. + This classifier implements the 1-nearest neighbor algorithm, which + classifies a new data point based on the class of its closest neighbor + in the training set. Proximity is measured using Euclidean distance. + """ def __init__(self): # noqa: D107 pass def fit(self, X, y): - """Write docstring. - - And describe parameters + """Train the 1-nearest neighbor classifier. + + Parameters + ---------- + X : array-like of shape (n_samples, n_features) + Training data, where n_samples is the number of samples and + n_features is the number of features. + y : array-like of shape (n_samples,) + Target values. + + Returns + ------- + self : object + Returns self. """ 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_train_ = X + self.y_train_ = y return self def predict(self, X): - """Write docstring. - - And describe parameters + """Predict the class labels for the provided data. + + Parameters + ---------- + X : array-like of shape (n_samples, n_features) + Test samples, where n_samples is the number of samples and + n_features is the number of features. + + Returns + ------- + y_pred : ndarray of shape (n_samples,) + Class labels for each data sample. """ - check_is_fitted(self) + check_is_fitted(self, ['X_train_', 'y_train_']) X = check_array(X) + if X.shape[1] != self.n_features_in_: + raise ValueError( + f"X has {X.shape[1]} features, but OneNearestNeighbor " + f"is expecting {self.n_features_in_} features as input." + ) y_pred = np.full( shape=len(X), fill_value=self.classes_[0], dtype=self.classes_.dtype ) + for i, x in enumerate(X): + # Calculate distances to all training points + distances = np.sqrt(np.sum((self.X_train_ - x)**2, axis=1)) + + # Find the index of the minimum distance + nearest_idx = np.argmin(distances) + + # Assign the class of the nearest neighbor + y_pred[i] = self.y_train_[nearest_idx] - # XXX fix return y_pred def score(self, X, y): - """Write docstring. - - And describe parameters + """Return the accuracy on the given test data and labels. + + Parameters + ---------- + X : array-like of shape (n_samples, n_features) + Test samples, where n_samples is the number of samples and + n_features is the number of features. + y : array-like of shape (n_samples,) + True labels for X. + + Returns + ------- + score : float + Accuracy of the classifier, which is the fraction of correctly + classified samples. """ X, y = check_X_y(X, y) y_pred = self.predict(X) # XXX fix - return y_pred.sum() + return np.mean(y_pred == y) From b9fa6cb485c94395e5e3307ad62407133e151c0e Mon Sep 17 00:00:00 2001 From: oli Date: Thu, 13 Nov 2025 15:18:03 +0100 Subject: [PATCH 2/2] fixed format to pass test --- sklearn_questions.py | 1 + 1 file changed, 1 insertion(+) diff --git a/sklearn_questions.py b/sklearn_questions.py index 7db7d5e6..9d20e243 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -30,6 +30,7 @@ class OneNearestNeighbor(ClassifierMixin, BaseEstimator): """OneNearestNeighbor classifier. + This classifier implements the 1-nearest neighbor algorithm, which classifies a new data point based on the class of its closest neighbor in the training set. Proximity is measured using Euclidean distance.