Skip to content
Closed
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
39 changes: 28 additions & 11 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 @@ -40,28 +41,44 @@ def max_index(X):
i = 0
j = 0

# TODO
if not isinstance(X, np.ndarray):
raise ValueError("The input must be a numpy array")

d = X.ndim

if d != 2:
raise ValueError("The input must be a 2D array")

i_flat = np.argmax(X)

i, j = np.unravel_index(i_flat, X.shape)

return i, j


def wallis_product(n_terms):
"""Implement the Wallis product to compute an approximation of pi.

See:
https://en.wikipedia.org/wiki/Wallis_product
"""
Compute an approximation of pi using the Wallis product.

Parameters
----------
n_terms : int
Number of steps in the Wallis product. Note that `n_terms=0` will
consider the product to be `1`.
Number of terms in the Wallis product. If `n_terms=0`,
the function returns 1.

Returns
-------
pi : float
The approximation of order `n_terms` of pi using the Wallis product.
Approximation of pi computed with `n_terms` terms.
"""
# 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

tab = np.arange(1, n_terms + 1)
n2 = tab * tab
n2x4 = 4 * n2
n2x4_1 = n2x4 - 1
final = n2x4 / n2x4_1
prod = np.prod(final)

return 2 * prod
25 changes: 19 additions & 6 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 @@ -29,7 +30,13 @@


class OneNearestNeighbor(BaseEstimator, ClassifierMixin):
"OneNearestNeighbor classifier."
"""
One-nearest-neighbor classifier.

This estimator stores the training data during `fit` and predicts
the label of a new sample as the label of the closest training
sample using the Euclidean distance.
"""

def __init__(self): # noqa: D107
pass
Expand All @@ -44,7 +51,9 @@ def fit(self, X, 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):
Expand All @@ -55,11 +64,15 @@ def predict(self, X):
check_is_fitted(self)
X = check_array(X)
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 in range(len(X)):
diff = self.X_ - X[i]
dist = np.linalg.norm(diff, axis=1)
idx = np.argmin(dist)
y_pred[i] = self.y_[idx]

return y_pred

def score(self, X, y):
Expand All @@ -71,4 +84,4 @@ def score(self, X, y):
y_pred = self.predict(X)

# XXX fix
return y_pred.sum()
return (y_pred == y).mean()