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
13 changes: 12 additions & 1 deletion numpy_questions.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ def max_index(X):
j = 0

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

if X.ndim != 2:
raise ValueError("The shape is not 2D")

id = np.argmax(X)
i, j = np.unravel_index(id, X.shape)

return i, j

Expand All @@ -64,4 +72,7 @@ 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
res = np.prod(np.array([4*n**2/(4*n**2 - 1) for n in range(1, n_terms+1)]))
return 2 * res
73 changes: 64 additions & 9 deletions sklearn_questions.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,29 +28,53 @@
from sklearn.utils.multiclass import check_classification_targets


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

def __init__(self): # noqa: D107
pass

def fit(self, X, y):
"""Write docstring.
"""Implement the fitting of a nearest neighbor classifier.

Parameters
----------
X : numpy.darray of shape (number of samples, number of features)
The input array we want to fit on.

y : numpy.darray of shape (number of samples)
The target of the samples present in X.

Returns
-------
self : OneNearestNeighbor
Fitted estimator.

And describe parameters
"""
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.

And describe parameters
"""Implement the prediction of a target y according to the entry X.

Parameters
----------
X : numpy.darray of shape (number of samples, number of features)
The input array we want to predict.

Returns
-------
y : numpy.darray of shape (number of samples)
The prediction of the samples in X according to the
nearest neighbor classifier.
"""
check_is_fitted(self)
X = check_array(X)
Expand All @@ -60,15 +84,46 @@ def predict(self, X):
)

# XXX fix

if X.shape[1] != self.n_features_in_:
raise ValueError(
"X has {} features, but {} is expecting {} features as input"
.format(
X.shape[1],
self.__class__.__name__,
self.n_features_in_,
)
)

diffs = X[:, np.newaxis, :] - self.X_[np.newaxis, :, :]
dist_sq = np.sum(diffs ** 2, axis=2)

nearest_indices = np.argmin(dist_sq, axis=1)

y_pred = self.y_[nearest_indices]

return y_pred

def score(self, X, y):
"""Write docstring.
"""Give the score of a prediction.

Parameters
----------
X : numpy.darray of shape (number of samples, number of features)
The samples we test.

y : numpy.darray of shape (number of samples)
The true labels of the X we try to predict with the classifier.
Returns
-------
score : float
The mean accuracy of the predictor on the given data and target.

And describe parameters
"""
X, y = check_X_y(X, y)
y_pred = self.predict(X)

# XXX fix
y_pred = (y_pred == y).astype(float) / len(y)

return y_pred.sum()