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
29 changes: 22 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,17 @@ 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 is not a numpy array")

if X.ndim != 2:
raise ValueError("Input must be 2D")

# TODO
idx_1d = np.argmax(X)

return i, j
(i, j) = np.unravel_index(idx_1d, X.shape)

return (i, j)


def wallis_product(n_terms):
Expand All @@ -62,6 +68,15 @@ 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.
return 0.
result = 1

if n_terms == 0:
pass

else:
for n in range(1, n_terms + 1):
result *= 4 * n**2 / (4 * n**2 - 1)

result *= 2

return result
83 changes: 65 additions & 18 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,47 +29,93 @@
from sklearn.utils.multiclass import check_classification_targets


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

Estimator that implements the 1-Nearest Neighbor algorithm to predict a new
sample's label based on the closest training sample.

Parameters
----------
None
"""

def __init__(self):
"""

def __init__(self): # noqa: D107
Init function.

Returns
-------
None.

"""
pass

def fit(self, X, y):
"""Write docstring.
"""Fit function.

Parameters
----------
X: array of shape (n_samples, n_features)
y: array of shape (n_samples,), holds the labels to predict

And describe parameters
Returns
-------
Self: itself
"""
X, y = check_X_y(X, y)
check_classification_targets(y)
self.classes_ = np.unique(y)

self.n_features_in_ = X.shape[1]
self.X_ = X
self.y_ = y
self.classes_ = np.unique(y)

# XXX fix
return self

def predict(self, X):
"""Write docstring.
"""Predict function.

Parameters
----------
X: array of shape (n_samples, n_features)

And describe parameters
Returns
-------
y_pred: the predicted labels for each sample in 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
)

# XXX fix
if X.shape[1] != self.n_features_in_:
raise ValueError(
f"X has {X.shape[1]} features, but OneNearestNeighbor "
f"is expecting {self.n_features_in_} features as input."
)

difference = X[:, None, :] - self.X_[None, :, :]
distances = np.linalg.norm(difference, axis=2)

closest_indx = np.argmin(distances, axis=1)
y_pred = self.y_[closest_indx]

return y_pred

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

Parameters
----------
X: array of shape (n_samples, n_features)
y: array of shape (n_samples,), holds the labels to predict

And describe parameters
Returns
-------
score: float,
the mean accuracy of the prediction against the true labels y
"""
X, y = check_X_y(X, y)
y_pred = self.predict(X)

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