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
18 changes: 15 additions & 3 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 @@ -39,8 +40,11 @@ def max_index(X):
"""
i = 0
j = 0

# TODO
if not isinstance(X, np.ndarray):
raise ValueError("Input must be a numpy array")
if X.ndim != 2:
raise ValueError("Input array must be 2D")
i, j = np.unravel_index(np.argmax(X), X.shape)

return i, j

Expand All @@ -64,4 +68,12 @@ 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.

if n_terms == 0:
return 1.0
product = 1.0
for n in range(1, n_terms + 1):
product *= (4 * n * n) / (4 * n * n - 1)
pi = 2 * product
return pi
# return 0.
61 changes: 48 additions & 13 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,28 +30,48 @@


class OneNearestNeighbor(BaseEstimator, ClassifierMixin):
"OneNearestNeighbor classifier."
"""One Nearest Neighbor classifier."""

def __init__(self): # noqa: D107
pass

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

And describe parameters
"""Fit the OneNearestNeighbor class.

Parameters
----------
X : ndarray of shape (n_samples, n_features)
The training data.
y : ndarray of shape (n_samples, n_features)
Target for each training sample

Returns
-------
self : OneNearestNeighbor
The 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_ = X
self.y_ = y

return self

def predict(self, X):
"""Write docstring.
"""Predict the OneNearestNeighbor class.

And describe parameters
Parameters
----------
X : ndarray of shape (n_samples, n_features)
The test data.

Returns
-------
y_pred : ndarray of shape (n_samples,)
The predicted class for each sample.
"""
check_is_fitted(self)
X = check_array(X)
Expand All @@ -59,16 +80,30 @@ def predict(self, X):
dtype=self.classes_.dtype
)

# XXX fix
for i, x_test in enumerate(X):
distances = np.sqrt(np.sum((self.X_ - x_test) ** 2, axis=1))
nearest_idx = np.argmin(distances)
y_pred[i] = self.y_[nearest_idx]

return y_pred

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

And describe parameters
"""Compute the accuracy of the OneNearestNeighbor classifier.

Parameters
----------
X : ndarray of shape (n_samples, n_features)
The test data.
y : ndarray of shape (n_samples, n_features)
Train data.

Returns
-------
accuracy : float
The mean accuracy of the prediction.
"""
X, y = check_X_y(X, y)
y_pred = self.predict(X)

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