From 72912821cb5ef7fecf0ffa69dcb505361676f931 Mon Sep 17 00:00:00 2001 From: coasensi Date: Thu, 13 Nov 2025 16:21:31 +0100 Subject: [PATCH 1/2] questions --- numpy_questions.py | 23 ++++++++++++++++++- sklearn_questions.py | 54 ++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 74 insertions(+), 3 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index 21fcec4b..1faff71f 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -39,6 +39,15 @@ def max_index(X): """ i = 0 j = 0 + max = 0 + + for row in range(X.shape[0]): + for column in range(X.shape[1]): + if X[row,column] > max: + i = row + j = column + max = X[row,column] + # TODO @@ -64,4 +73,16 @@ 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. + + pi=2 + + + if n_terms == 0: + n_terms = 1 + + for i in range(1,n_terms): + pi *= (4*i**2)/(4*i**2-1) + + return pi + + diff --git a/sklearn_questions.py b/sklearn_questions.py index f65038c6..f20cfa5b 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -38,19 +38,47 @@ def fit(self, X, y): """Write docstring. And describe parameters + + Parameters + ----------- + self : instance of the class (OneNearestNeighbor) + + X : matrix of the features ; independent and explanatory variables + + y : matrix of the explained variable + + Returns + ------- + self : fitted estimator + """ 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 + + Parameters + ---------- + + self : instance of the class (OneNearestNeighbor) + + X : features matrix + + Returns + ------- + + y_pred : predictions for y based on inputted X """ check_is_fitted(self) X = check_array(X) @@ -60,15 +88,37 @@ def predict(self, X): ) # XXX fix + + for i, X in enumerate(X): + #compute distance to all training samples + distances = np.linalg.norm(self.X_train_ - x, axis=1) + #pick smallest distance index + index_min = np.argmin(distances) + #assign the label + y_pred[i] = self.y_train_[index_min] return y_pred def score(self, X, y): """Write docstring. And describe parameters + + Parameters + ---------- + + self : instance of the class (OneNearestNeighbor) + + X : features matrix + + y : explained variables matrix + + Returns + ------- + + """ 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 d57f18c2fe013c2e54803f9574d22ec6e2b4b9de Mon Sep 17 00:00:00 2001 From: coasensi Date: Thu, 13 Nov 2025 17:48:58 +0100 Subject: [PATCH 2/2] commit 2 --- numpy_questions.py | 41 +++++++++---------------- sklearn_questions.py | 72 ++++++++++++++++++-------------------------- 2 files changed, 45 insertions(+), 68 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index 1faff71f..e4bc7fce 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -37,22 +37,18 @@ def max_index(X): If the input is not a numpy array or if the shape is not 2D. """ - i = 0 - j = 0 - max = 0 + if X.ndim() != 2: + raise ValueError("input is not of dimension 2") + if type(X) is not np.ndarray: + raise ValueError("input is not np array") + + max = np.argmax(X) + n_columns = X.shape[1] - for row in range(X.shape[0]): - for column in range(X.shape[1]): - if X[row,column] > max: - i = row - j = column - max = X[row,column] - - - # TODO - - return i, j + row = max // n_columns + column = max % n_columns + return (row, column) def wallis_product(n_terms): """Implement the Wallis product to compute an approximation of pi. @@ -71,18 +67,11 @@ 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. - - pi=2 - - if n_terms == 0: - n_terms = 1 - - for i in range(1,n_terms): - pi *= (4*i**2)/(4*i**2-1) - - return pi + return 1 + else: + n = 4 * np.arange(1, n_terms + 1) ** 2 + pi = 2 * np.prod(n / (n - 1)) + return pi diff --git a/sklearn_questions.py b/sklearn_questions.py index f20cfa5b..b86cfad4 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -35,90 +35,78 @@ def __init__(self): # noqa: D107 pass def fit(self, X, y): - """Write docstring. - - And describe parameters + """ Parameters ----------- self : instance of the class (OneNearestNeighbor) - X : matrix of the features ; independent and explanatory variables + X : array of shape(n_samples, n_features) + matrix of the features; independent and explanatory variables - y : matrix of the explained variable + y : array of shape(n_samples,) + matrix of the explained variable Returns ------- - self : fitted estimator + self : object + fitted estimator """ 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 - + + """ Parameters ---------- - self : instance of the class (OneNearestNeighbor) - - X : features matrix + X : array of shape(n_samples, n_features) + matrix of the features; independent and explanatory variables Returns ------- - y_pred : predictions for y based on inputted X + y_pred : array of shape(n_samples,) + predictions for y """ 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 - - for i, X in enumerate(X): - #compute distance to all training samples - distances = np.linalg.norm(self.X_train_ - x, axis=1) - #pick smallest distance index - index_min = np.argmin(distances) - #assign the label - y_pred[i] = self.y_train_[index_min] - return y_pred + y_pred = [] + if X.shape[1] != self.n_features_in_: + raise ValueError(f"X has {X.shape[1]} features but expects {self.n_features_in_} features as input") - def score(self, X, y): - """Write docstring. + for value in X: + distance = np.linalg.norm(self.X_train_ - value, axis=1) + index = np.argmin(distance) + y_pred.append(self.y_train_[index]) - And describe parameters + return np.array(y_pred) - Parameters + def score(self, X, y): + """Parameters ---------- - self : instance of the class (OneNearestNeighbor) - - X : features matrix + X : array of shape(n_samples, n_features) + matrix of the features; independent and explanatory variables - y : explained variables matrix + y : array of shape(n_samples,) + explained variable Returns ------- - + accuracy score : type float """ X, y = check_X_y(X, y) y_pred = self.predict(X) - - # XXX fix return np.mean(y_pred == y)