From 12e247238d0b4c3b36d25f9399c5a300832a2ac4 Mon Sep 17 00:00:00 2001 From: Conni2 Date: Fri, 14 Nov 2025 14:34:19 +0100 Subject: [PATCH 1/2] Completed numpy assignment --- numpy_questions.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/numpy_questions.py b/numpy_questions.py index 21fcec4b..9501b5da 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -41,6 +41,13 @@ def max_index(X): j = 0 # TODO + if not isinstance(X, np.ndarray): + raise ValueError("Input is not a numpy array") + if X.ndim != 2: + raise ValueError("Input array is not 2D") + flat_index = np.argmax(X) + i, j = np.unravel_index(flat_index, X.shape) + i, j = int(i), int(j) return i, j @@ -64,4 +71,14 @@ 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 not isinstance(n_terms, (int, np.integer)): + raise ValueError("n_terms should be an integer") + if n_terms < 0: + raise ValueError("n_terms should be non-negative") + if n_terms == 0: + return 1.0 + k = np.arange(1, n_terms + 1, dtype=float) + terms = (4.0 * k**2) / (4.0 * k**2 - 1.0) + product = np.prod(terms) + pi_approx = 2.0 * product + return float(pi_approx) From 44568221992dd9106c4203b48339a3a1f85500a1 Mon Sep 17 00:00:00 2001 From: Conni2 Date: Fri, 14 Nov 2025 15:49:11 +0100 Subject: [PATCH 2/2] Update sklearn assignment --- sklearn_questions.py | 75 +++++++++++++++++++++++++++++++------------- 1 file changed, 54 insertions(+), 21 deletions(-) diff --git a/sklearn_questions.py b/sklearn_questions.py index f65038c6..ca9529d0 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -28,47 +28,80 @@ from sklearn.utils.multiclass import check_classification_targets -class OneNearestNeighbor(BaseEstimator, ClassifierMixin): - "OneNearestNeighbor classifier." +class OneNearestNeighbor(ClassifierMixin, BaseEstimator): + """OneNearestNeighbor classifier. + + To predict the label of a sample as the label of the closest sample in + Euclidean distance in the training set. + """ def __init__(self): # noqa: D107 pass def fit(self, X, y): - """Write docstring. - - And describe parameters + """Fit the one nearest neighbor 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 estimator. """ X, y = check_X_y(X, y) check_classification_targets(y) + self.X_train_ = X + self.y_train_ = y self.classes_ = np.unique(y) self.n_features_in_ = X.shape[1] - - # XXX fix 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) + Samples to classify. - And describe parameters + Returns + ------- + y_pred : array-like 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 - ) - - # XXX fix + if X.shape[1] != self.n_features_in_: + raise ValueError( + f"X has {X.shape[1]} features, but " + f"{self.__class__.__name__} is expecting " + f"{self.n_features_in_} features as input." + ) + diff = self.X_train_[np.newaxis, :, :] - X[:, np.newaxis, :] + dist_sq = np.sum(diff ** 2, axis=2) + nearest_indices = np.argmin(dist_sq, axis=1) + y_pred = self.y_train_[nearest_indices] return y_pred def score(self, X, y): - """Write docstring. - - And describe parameters + """Return the mean accuracy on the given test data and labels. + + 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 the predictions on X. """ 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))