Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"python.testing.pytestArgs": [
"."
],
"python.testing.unittestEnabled": false,
"python.testing.pytestEnabled": true
}
33 changes: 26 additions & 7 deletions numpy_questions.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
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


Expand All @@ -37,12 +38,24 @@ def max_index(X):
If the input is not a numpy array or
if the shape is not 2D.
"""
i = 0
j = 0
if not isinstance(X, np.ndarray):
raise ValueError("Input must be a numpy array.")
if X.ndim != 2:
raise ValueError("Input must be a 2D array.")

# TODO
i_max = 0
j_max = 0
max_value = -np.inf
n_samples = len(X)
n_features = len(X[0])
for i in range(n_samples):
for j in range(n_features):
if X[i, j] > max_value:
max_value = X[i, j]
i_max = i
j_max = j

return i, j
return i_max, j_max


def wallis_product(n_terms):
Expand All @@ -57,11 +70,17 @@ def wallis_product(n_terms):
Number of steps in the Wallis product. Note that `n_terms=0` will
consider the product to be `1`.



Returns
-------
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
product = 1
for n in range(n_terms):
quatre_n_2 = 4 * ((n + 1) ** 2)
product *= quatre_n_2 / (quatre_n_2 - 1)
return product * 2
38 changes: 29 additions & 9 deletions sklearn_questions.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
for the methods you code and for the class. The docstring will be checked using
`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
Expand All @@ -28,7 +29,7 @@
from sklearn.utils.multiclass import check_classification_targets


class OneNearestNeighbor(BaseEstimator, ClassifierMixin):
class OneNearestNeighbor(ClassifierMixin, BaseEstimator):
"OneNearestNeighbor classifier."

def __init__(self): # noqa: D107
Expand All @@ -43,32 +44,51 @@ def fit(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.


And describe parameters
"""
check_is_fitted(self)

X = check_array(X)

if X.shape[1] != self.n_features_in_:
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
shape=len(X), fill_value=self.classes_[0], dtype=self.classes_.dtype
)

# XXX fix
for i, x_i in enumerate(X):
min_d = np.inf
j_min = np.inf
for j, x_j in enumerate(self.X_):
d = np.linalg.norm(x_i - x_j)
if d < min_d:
min_d = d
j_min = j
y_pred[i] = self.y_[j_min]
return y_pred

def score(self, X, y):
"""Write docstring.

And describe parameters
"""
n = len(y)
accuracy = 0
X, y = check_X_y(X, y)
y_pred = self.predict(X)
for i in range(n):
if y_pred[i] == y[i]:
accuracy += 1

# XXX fix
return y_pred.sum()
return accuracy / n
Loading