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
19 changes: 16 additions & 3 deletions numpy_questions.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,15 @@ 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.")

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

return i.item(), j.item()


def wallis_product(n_terms):
Expand All @@ -64,4 +70,11 @@ 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
else:
product = 1
for n in range(1, n_terms+1):
product *= (4*n**2) / (4*n**2 - 1)
return product*2
61 changes: 50 additions & 11 deletions sklearn_questions.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,46 +29,85 @@


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

def __init__(self): # noqa: D107
pass

def fit(self, X, y):
"""Write docstring.
"""Fit the OneNearestNeighbot classifier.

Parameters
----------
X : ndarray of shape (n_samples, n_features)
The matrix containing training input samples.
y : ndarray of shape (n_samples)
The matrix of true labels for the input samples.

Returns
-------
self : object
The 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.
"""Return the predicted target for an input.

And describe parameters
Parameters
----------
X : ndarray of shape (n_samples, n_features)
The input array whose target is being predicted

Returns
-------
y_pred : ndarray of shape (n_samples)
The predicted target for each sample in X.
"""
check_is_fitted(self)
check_is_fitted(self), ["X_", "y_"]
X = check_array(X)
y_pred = np.full(
shape=len(X), fill_value=self.classes_[0],
dtype=self.classes_.dtype
)

# XXX fix
distances = np.sqrt(
((X[:, np.newaxis, :] - self.X_[np.newaxis, :, :]
) ** 2).sum(axis=2)
)

nearest_idx = np.argmin(distances, axis=1)

y_pred = self.y_[nearest_idx]

return y_pred

def score(self, X, y):
"""Write docstring.
"""Return the mean accuracy on the given test data and labels.

Parameters
----------
X : ndarray of shape (n_samples, n_features)
Test samples.

y : ndarray of shape (n_samples,)
True labels for X.

And describe parameters
Returns
-------
score : float
Mean accuracy of self.predict(X) with respect to 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)