From b4e770f01173602a7b2d24fe1d0eb03c31eb803a Mon Sep 17 00:00:00 2001 From: jankovicnina Date: Thu, 13 Nov 2025 18:24:17 +0100 Subject: [PATCH 1/7] Fix max_index dimension check and wallis_product formula - all tests pass --- numpy_questions.py | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index 21fcec4b..6bb5a99b 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -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 - # TODO + if not isinstance(X, np.ndarray): + raise ValueError('The input is not an array.') - return i, j + if X.ndim != 2: + raise ValueError('The input is not 2D array') + + overall_max = np.max(X) + + for i in range(np.shape(X)[0]): + for j in range(np.shape(X)[1]): + if X[i][j] == overall_max: + return i, j + + return 0, 0 def wallis_product(n_terms): @@ -62,6 +71,12 @@ 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 + + else: + prod = 1 + for i in range(1, n_terms + 1): + prod *= (4 * i ** 2) / (4 * i**2 - 1) + + return prod * 2 From 0ffcdaabbe8ffe45d55b51f7ceb1d460c2c73582 Mon Sep 17 00:00:00 2001 From: jankovicnina Date: Sat, 15 Nov 2025 18:13:05 +0100 Subject: [PATCH 2/7] Zadnji commit za nalogo --- sklearn_questions.py | 78 ++++++++++++++++++++++++++++++++++++-------- 1 file changed, 64 insertions(+), 14 deletions(-) diff --git a/sklearn_questions.py b/sklearn_questions.py index f65038c6..a597993c 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): +class OneNearestNeighbor(ClassifierMixin, BaseEstimator): "OneNearestNeighbor classifier." def __init__(self): # noqa: D107 pass def fit(self, X, y): - """Write docstring. - - And describe parameters """ + Fit the classifier with training data. + + Parameters: + ---------- + X : array-like of shape (n_samples, n_features) + Training data. + y : array-like of shape (n_samples,) + Target labels. + + Returns + ------- + self : object + Returns the instance itself. + """ + 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. + """ + Predict labels for new samples. + + For each input point, finds the closest training sample (using + Euclidean distance) and returns its label. - And describe parameters + Parameters + ---------- + X : array-like of shape (n_samples, n_features) + Test data. + + Returns + ------- + y_pred : ndarray of shape (n_samples,) + Predicted class labels for each input sample. """ check_is_fitted(self) - X = check_array(X) + 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 ) - # XXX fix + for i, x_test in enumerate(X): + distances = np.sqrt(np.sum((self.X_train_ - x_test) ** 2, axis=1)) + nearest_idx = np.argmin(distances) + y_pred[i] = self.y_train_[nearest_idx] + return y_pred def score(self, X, y): - """Write docstring. - - And describe parameters + """ + Compute the accuracy of the classifier. + + Accuracy is the fraction of correctly predicted samples + compared to the true labels. + + Parameters + ---------- + X : array-like of shape (n_samples, n_features) + Test data. + y : array-like of shape (n_samples,) + True labels for the test data. + + Returns + ------- + score : float + Mean accuracy of predictions. """ 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 faccb610a9e6a55f20a0a128eafd59192756ada0 Mon Sep 17 00:00:00 2001 From: jankovicnina Date: Sat, 15 Nov 2025 18:19:20 +0100 Subject: [PATCH 3/7] Res zadnji commit za nalogo --- numpy_questions.py | 2 -- sklearn_questions.py | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index 6bb5a99b..4e504aff 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -43,7 +43,6 @@ def max_index(X): if X.ndim != 2: raise ValueError('The input is not 2D array') - overall_max = np.max(X) for i in range(np.shape(X)[0]): @@ -73,7 +72,6 @@ def wallis_product(n_terms): """ if n_terms == 0: return 1 - else: prod = 1 for i in range(1, n_terms + 1): diff --git a/sklearn_questions.py b/sklearn_questions.py index a597993c..1f91e804 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -79,7 +79,7 @@ def predict(self, X): Predicted class labels for each input sample. """ check_is_fitted(self) - X = check_array(X) + X = check_array(X) if X.shape[1] != self.n_features_in_: raise ValueError( From 1c3a7d27cb55bbdb133dc828bf043699d6fc061a Mon Sep 17 00:00:00 2001 From: jankovicnina Date: Sat, 15 Nov 2025 18:24:41 +0100 Subject: [PATCH 4/7] Docstring mistakes --- numpy_questions.py | 1 - sklearn_questions.py | 6 +++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index 4e504aff..4eb24614 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -37,7 +37,6 @@ 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('The input is not an array.') diff --git a/sklearn_questions.py b/sklearn_questions.py index 1f91e804..66954d54 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -29,8 +29,9 @@ class OneNearestNeighbor(ClassifierMixin, BaseEstimator): - "OneNearestNeighbor classifier." - + """ + OneNearestNeighbor classifier. + """ def __init__(self): # noqa: D107 pass @@ -50,7 +51,6 @@ def fit(self, X, y): self : object Returns the instance itself. """ - X, y = check_X_y(X, y) check_classification_targets(y) self.classes_ = np.unique(y) From ca44d1227fc6d3c8d1c7daed1f7ab54073503129 Mon Sep 17 00:00:00 2001 From: jankovicnina Date: Sat, 15 Nov 2025 18:26:07 +0100 Subject: [PATCH 5/7] Docstring mistakes in sklearn --- sklearn_questions.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/sklearn_questions.py b/sklearn_questions.py index 66954d54..6efc881f 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -29,9 +29,8 @@ class OneNearestNeighbor(ClassifierMixin, BaseEstimator): - """ - OneNearestNeighbor classifier. - """ + """ OneNearestNeighbor classifier. """ + def __init__(self): # noqa: D107 pass From a0f1dd50fa5ea0b7f14fee6d84837450e2d3ab57 Mon Sep 17 00:00:00 2001 From: jankovicnina Date: Sat, 15 Nov 2025 18:28:15 +0100 Subject: [PATCH 6/7] Docstring mistakes in sklearn --- sklearn_questions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sklearn_questions.py b/sklearn_questions.py index 6efc881f..eb3483f2 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -29,7 +29,7 @@ class OneNearestNeighbor(ClassifierMixin, BaseEstimator): - """ OneNearestNeighbor classifier. """ + """OneNearestNeighbor classifier.""" def __init__(self): # noqa: D107 pass From 478b14efad538deea1df3b8993965f1cf2a78abe Mon Sep 17 00:00:00 2001 From: jankovicnina Date: Sat, 15 Nov 2025 18:29:33 +0100 Subject: [PATCH 7/7] whitespace error --- sklearn_questions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sklearn_questions.py b/sklearn_questions.py index eb3483f2..e3dbec9d 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -30,7 +30,7 @@ class OneNearestNeighbor(ClassifierMixin, BaseEstimator): """OneNearestNeighbor classifier.""" - + def __init__(self): # noqa: D107 pass