From f720e77c3a9be35c96a24a70331ae20f2728c089 Mon Sep 17 00:00:00 2001 From: luc walter Date: Tue, 10 Nov 2026 15:20:10 +0100 Subject: [PATCH 1/8] answered np questions --- numpy_questions.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index 21fcec4b..c5fbf36a 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -40,8 +40,14 @@ def max_index(X): i = 0 j = 0 - # TODO - + if X.ndim !=2 or not isinstance(X,np.ndarray) : + raise ValueError("X must be a 2D numpy array") + else : + for n in range (X.shape[0]): + for m in range (X.shape[1]): + if X[n,m]>X[i,j]: + i,j=n,m + return i, j @@ -64,4 +70,7 @@ 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. + wallis = 1 + for n in range (n_terms): + wallis*=(4 * (n+1)**2)/((4 * (n+1)**2) - 1) + return wallis From cff6fbe0556c44468f8f81a32a53477f0433b4e9 Mon Sep 17 00:00:00 2001 From: luc walter Date: Tue, 10 Nov 2026 16:04:04 +0100 Subject: [PATCH 2/8] answered sklearn questions --- sklearn_questions.py | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/sklearn_questions.py b/sklearn_questions.py index f65038c6..537ea830 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -44,7 +44,9 @@ def fit(self, X, 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): @@ -54,12 +56,25 @@ def predict(self, X): """ check_is_fitted(self) X = check_array(X) + if X.shape[1] != self.n_features_in_: + raise ValueError("The given dataset does not match the number of features the model was fitted on") y_pred = np.full( shape=len(X), fill_value=self.classes_[0], dtype=self.classes_.dtype ) - # XXX fix + for i in range(len(X)) : + min_dist = None + index_min = None + x = X[i] + for j in range(len(self.X_train_)): + x_train = self.X_train_[j] + dist = np.linalg.norm(x - x_train) + if min_dist is None or dist < min_dist : + min_dist = dist + index_min = j + y_pred[i]=self.y_train_[index_min] + return y_pred def score(self, X, y): @@ -69,6 +84,6 @@ def score(self, X, y): """ X, y = check_X_y(X, y) y_pred = self.predict(X) + accuracy = sum(y_pred == y) / len(y) - # XXX fix - return y_pred.sum() + return accuracy From c118611320adc5df4ffcee2e6778601e8945ab0b Mon Sep 17 00:00:00 2001 From: luc walter Date: Tue, 10 Nov 2026 16:49:49 +0100 Subject: [PATCH 3/8] corrected via pep8 --- numpy_questions.py | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index c5fbf36a..0761299f 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -40,14 +40,13 @@ def max_index(X): i = 0 j = 0 - if X.ndim !=2 or not isinstance(X,np.ndarray) : + if X.ndim != 2 or not isinstance(X, np.ndarray): raise ValueError("X must be a 2D numpy array") - else : - for n in range (X.shape[0]): - for m in range (X.shape[1]): - if X[n,m]>X[i,j]: - i,j=n,m - + else: + for n in range(X.shape[0]): + for m in range(X.shape[1]): + if X[n, m] > X[i, j]: + i, j = n, m return i, j @@ -71,6 +70,6 @@ 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. wallis = 1 - for n in range (n_terms): - wallis*=(4 * (n+1)**2)/((4 * (n+1)**2) - 1) + for n in range(n_terms): + wallis *= (4 * (n+1)**2)/((4 * (n+1)**2) - 1) return wallis From 5e756425bf469c4650ead281c081d4e9bda68266 Mon Sep 17 00:00:00 2001 From: luc walter Date: Tue, 10 Nov 2026 16:52:49 +0100 Subject: [PATCH 4/8] corrected via pep8 --- sklearn_questions.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/sklearn_questions.py b/sklearn_questions.py index 537ea830..2ae306a4 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -46,7 +46,6 @@ def fit(self, X, y): self.X_train_ = X self.y_train_ = y - return self def predict(self, X): @@ -57,24 +56,24 @@ def predict(self, X): check_is_fitted(self) X = check_array(X) if X.shape[1] != self.n_features_in_: - raise ValueError("The given dataset does not match the number of features the model was fitted on") + raise ValueError("Dataset does not match the number" + "of features the model it was fitted on") y_pred = np.full( shape=len(X), fill_value=self.classes_[0], dtype=self.classes_.dtype ) - for i in range(len(X)) : + for i in range(len(X)): min_dist = None index_min = None x = X[i] for j in range(len(self.X_train_)): x_train = self.X_train_[j] dist = np.linalg.norm(x - x_train) - if min_dist is None or dist < min_dist : + if min_dist is None or dist < min_dist: min_dist = dist index_min = j - y_pred[i]=self.y_train_[index_min] - + y_pred[i] = self.y_train_[index_min] return y_pred def score(self, X, y): From c914d53a6bad46475a8981cc4cc7571c97a6d325 Mon Sep 17 00:00:00 2001 From: luc walter Date: Tue, 10 Nov 2026 17:28:14 +0100 Subject: [PATCH 5/8] corrected errors --- numpy_questions.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index 0761299f..ec649fba 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -40,7 +40,7 @@ def max_index(X): i = 0 j = 0 - if X.ndim != 2 or not isinstance(X, np.ndarray): + if not isinstance(X, np.ndarray) or X.ndim != 2: raise ValueError("X must be a 2D numpy array") else: for n in range(X.shape[0]): @@ -69,7 +69,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. - wallis = 1 - for n in range(n_terms): - wallis *= (4 * (n+1)**2)/((4 * (n+1)**2) - 1) + if n_terms == 0: + wallis = 1 + else : + wallis = 1 + for n in range(n_terms): + wallis *= (4 * (n+1)**2)/((4 * (n+1)**2) - 1) + wallis *= 2 return wallis From aab334ee74315d301891ac80221dc8efca54bd58 Mon Sep 17 00:00:00 2001 From: luc walter Date: Tue, 10 Nov 2026 17:28:37 +0100 Subject: [PATCH 6/8] corrected errors --- sklearn_questions.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/sklearn_questions.py b/sklearn_questions.py index 2ae306a4..3ea5f654 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -28,7 +28,7 @@ from sklearn.utils.multiclass import check_classification_targets -class OneNearestNeighbor(BaseEstimator, ClassifierMixin): +class OneNearestNeighbor(ClassifierMixin, BaseEstimator): "OneNearestNeighbor classifier." def __init__(self): # noqa: D107 @@ -56,8 +56,10 @@ def predict(self, X): check_is_fitted(self) X = check_array(X) if X.shape[1] != self.n_features_in_: - raise ValueError("Dataset does not match the number" - "of features the model it was fitted on") + raise ValueError( + f"X has {X.shape[1]} features, but {self.__class__.__name__} " + 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 From e213267846e6573ec8f1eebbe2349e3bf6d5a32b Mon Sep 17 00:00:00 2001 From: luc walter Date: Tue, 10 Nov 2026 17:35:12 +0100 Subject: [PATCH 7/8] small modif for pydocstyle --- sklearn_questions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sklearn_questions.py b/sklearn_questions.py index 3ea5f654..8859d869 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 4585f29a2e07c7e2657179c891a9bea8aa12bce1 Mon Sep 17 00:00:00 2001 From: luc walter Date: Tue, 10 Nov 2026 17:47:30 +0100 Subject: [PATCH 8/8] I hope this is the last modification --- numpy_questions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/numpy_questions.py b/numpy_questions.py index ec649fba..793a6b2e 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -71,7 +71,7 @@ def wallis_product(n_terms): # terms in the product. For example 10000. if n_terms == 0: wallis = 1 - else : + else: wallis = 1 for n in range(n_terms): wallis *= (4 * (n+1)**2)/((4 * (n+1)**2) - 1)