From cda261320e615b64d925ed637e99180bc0f870fe Mon Sep 17 00:00:00 2001 From: kariny-0806 Date: Thu, 1 Jan 2026 01:26:08 +0900 Subject: [PATCH 1/5] Completed numpy questions KYamakawa --- numpy_questions.py | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index 21fcec4b..b225d4bc 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -16,6 +16,8 @@ errors by calling `flake8` at the root of the repo. """ import numpy as np +from numpy import unravel_index +import infinity as inf def max_index(X): @@ -37,12 +39,17 @@ def max_index(X): If the input is not a numpy array or if the shape is not 2D. """ - i = 0 - j = 0 + i = -inf + j = -inf - # TODO + if not isinstance(X, np.ndarray): + raise ValueError("Input should be a numpy array.") + if len(X.shape) != 2: + raise ValueError("Input should be a 2D numpy array.") + + (i, j) = np.unravel_index(X.argmax(), X.shape) - return i, j + return (i, j) def wallis_product(n_terms): @@ -64,4 +71,10 @@ 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_half = 1 + + for n in range(1, n_terms + 1): + pi_half = pi_half * (4 * n ** 2) / (4 * n ** 2 - 1) + + return 2 * pi_half From fd55895ff7d78a279a01e973a29b59e6f69d4307 Mon Sep 17 00:00:00 2001 From: kariny-0806 Date: Thu, 1 Jan 2026 01:44:42 +0900 Subject: [PATCH 2/5] git commit -m "Complete numpy questions" --- .DS_Store | Bin 0 -> 6148 bytes numpy_questions.py | 4 ++-- 2 files changed, 2 insertions(+), 2 deletions(-) create mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..8850d6ca3e26d80eea512e8453d6e6ffd207fdf3 GIT binary patch literal 6148 zcmeHKJ5Iwu5S>jN;fN+B<(>d3H!zVofhj>k#*%{+%PB~g!grvCOHgnG#636&Z+3>@ z#e@;%K714B3h027#lV9sX=&hd(wVkZa>#7QbpQk@<#oc@YANrykv_)bvL z$%V>TM=Lu$p}0^Tb|1pYWr8*u1I9qiz*sJ4-2cz_pa0_|yD|ohfj`B7OY$OL;Fh$v x4sMQnt%qJiSval}+@)X=S}|g|6`w$Z!0veh%oIC8SRnQz5NNQ$82D8N-T@JQNk#ww literal 0 HcmV?d00001 diff --git a/numpy_questions.py b/numpy_questions.py index b225d4bc..ee6247b1 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -15,8 +15,8 @@ This will be enforced with `flake8`. You can check that there is no flake8 errors by calling `flake8` at the root of the repo. """ + import numpy as np -from numpy import unravel_index import infinity as inf @@ -46,7 +46,7 @@ def max_index(X): raise ValueError("Input should be a numpy array.") if len(X.shape) != 2: raise ValueError("Input should be a 2D numpy array.") - + (i, j) = np.unravel_index(X.argmax(), X.shape) return (i, j) From 2f973a9733fc954db5eb06a5a33454e1a74c1609 Mon Sep 17 00:00:00 2001 From: kariny-0806 Date: Thu, 1 Jan 2026 01:48:41 +0900 Subject: [PATCH 3/5] Fix flake8 and pydocstyle issues --- numpy_questions.py | 6 ++---- sklearn_questions.py | 2 +- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index ee6247b1..f3e35408 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -17,8 +17,6 @@ """ import numpy as np -import infinity as inf - def max_index(X): """Return the index of the maximum in a numpy array. @@ -39,8 +37,8 @@ def max_index(X): If the input is not a numpy array or if the shape is not 2D. """ - i = -inf - j = -inf + i = -1 + j = -1 if not isinstance(X, np.ndarray): raise ValueError("Input should be a numpy array.") diff --git a/sklearn_questions.py b/sklearn_questions.py index f65038c6..ba753b5f 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -29,7 +29,7 @@ class OneNearestNeighbor(BaseEstimator, ClassifierMixin): - "OneNearestNeighbor classifier." + """OneNearestNeighbor classifier.""" def __init__(self): # noqa: D107 pass From fb4b8b48eb40df7dd52e268ac2edd6727009f72f Mon Sep 17 00:00:00 2001 From: kariny-0806 Date: Thu, 1 Jan 2026 03:31:04 +0900 Subject: [PATCH 4/5] Debugged sklearn question errors --- numpy_questions.py | 4 +++ sklearn_questions.py | 68 +++++++++++++++++++++++++++++++++----------- 2 files changed, 55 insertions(+), 17 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index f3e35408..6d175db8 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -18,6 +18,7 @@ import numpy as np + def max_index(X): """Return the index of the maximum in a numpy array. @@ -70,6 +71,9 @@ 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. + if n_terms == 0: + return 1.0 + pi_half = 1 for n in range(1, n_terms + 1): diff --git a/sklearn_questions.py b/sklearn_questions.py index ba753b5f..78f45a4c 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -20,55 +20,89 @@ `pydocstyle` that you can also call at the root of the repo. """ import numpy as np -from sklearn.base import BaseEstimator -from sklearn.base import ClassifierMixin +from sklearn.base import ClassifierMixin, BaseEstimator from sklearn.utils.validation import check_X_y -from sklearn.utils.validation import check_array +from sklearn.utils.validation import validate_data from sklearn.utils.validation import check_is_fitted 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. + """Fit the one nearest neighbor classifier from the training dataset. - And describe parameters + Parameters + ---------- + X : ndarray of shape (n_samples, n_features) + The input array. + + y : ndarray of shape (n_samples) + The target values. """ - X, y = check_X_y(X, y) + X, y = validate_data(self, X, y) check_classification_targets(y) self.classes_ = np.unique(y) - self.n_features_in_ = X.shape[1] - # XXX fix + self.X_ = X + self.y_ = y + return self def predict(self, X): - """Write docstring. + """Predict the class labels for new data points X_i. - And describe parameters + Parameters + ---------- + X : ndarray of shape (n_samples, n_features) + The input array. + + Returns + ------- + y_pred : ndarray of shape (n_samples) + The predicted class labels. """ check_is_fitted(self) - X = check_array(X) + X = validate_data(self, X, reset=False) + y_pred = np.full( shape=len(X), fill_value=self.classes_[0], dtype=self.classes_.dtype ) - # XXX fix + for i in range(len(X)): + distances = np.linalg.norm(self.X_ - X[i, :], axis=1) + y_pred[i] = self.y_[np.argmin(distances)] + return y_pred def score(self, X, y): - """Write docstring. + """Return the classifier accuracy on the given test data and labels. + + Parameters + ---------- + X : ndarray of shape (n_samples, n_features) + The input array. - And describe parameters + y : ndarray of shape (n_samples) + The target values. + + Returns + ------- + score : float + The accuracy of the classifier; + number of samples corectly classified. """ X, y = check_X_y(X, y) y_pred = self.predict(X) - # XXX fix - return y_pred.sum() + accurate_points = 0 + for i in range(len(y)): + if y_pred[i] == y[i]: + accurate_points += 1 + + return accurate_points / len(y) From 23385c11d51e13d02220e1ad1ac101ed4494cce7 Mon Sep 17 00:00:00 2001 From: kariny-0806 Date: Thu, 1 Jan 2026 03:38:26 +0900 Subject: [PATCH 5/5] Debugged sklearn validate_data version inconguency --- sklearn_questions.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/sklearn_questions.py b/sklearn_questions.py index 78f45a4c..7d5fa622 100644 --- a/sklearn_questions.py +++ b/sklearn_questions.py @@ -22,7 +22,7 @@ import numpy as np from sklearn.base import ClassifierMixin, BaseEstimator from sklearn.utils.validation import check_X_y -from sklearn.utils.validation import validate_data +from sklearn.utils.validation import check_array from sklearn.utils.validation import check_is_fitted from sklearn.utils.multiclass import check_classification_targets @@ -44,7 +44,7 @@ def fit(self, X, y): y : ndarray of shape (n_samples) The target values. """ - X, y = validate_data(self, X, y) + X, y = check_X_y(X, y) check_classification_targets(y) self.classes_ = np.unique(y) @@ -67,7 +67,7 @@ def predict(self, X): The predicted class labels. """ check_is_fitted(self) - X = validate_data(self, X, reset=False) + X = check_array(X, reset=False) y_pred = np.full( shape=len(X), fill_value=self.classes_[0],