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
24 changes: 20 additions & 4 deletions numpy_questions.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,13 @@ def max_index(X):
i = 0
j = 0

# TODO
if not isinstance(X, np.ndarray):
raise ValueError("Error: input not a numpy array")
if X.ndim != 2:
raise ValueError("Error: input array must be 2D")

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

return i, j

Expand All @@ -62,6 +68,16 @@ 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.
if not isinstance(n_terms, int) or n_terms < 0:
raise ValueError("Error: Input invalid")

if n_terms == 0:
return 1.0

product = 1.0
for k in range(1, n_terms + 1):
num = (2 * k) ** 2
denom = num - 1
product *= num / denom

return 2.0 * product
97 changes: 77 additions & 20 deletions sklearn_questions.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,47 +28,104 @@
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
This estimator implements the 1-Nearest Neighbor classification algorithm.
It predicts the label of a new sample based on the label of the single
closest training sample (nearest neighbor) using Euclidean distance.

Parameters
----------
No parameters are needed for this simple implementation.
"""

def __init__(self):
"""
Init function.

Returns
-------
None.

"""
pass

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

And describe parameters
"""
Fit the OneNearestNeighbor classifier.

Parameters
----------
X : array-like of shape (n_samples, n_features)
The training input samples.
y : array-like of shape (n_samples,)
The target values (class labels).

Returns
-------
self : object
Returns the instance 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 the class label for the provided data.

And describe parameters
Parameters
----------
X : array-like of shape (n_samples, n_features)
The input samples to predict.

Returns
-------
y_pred : ndarray of shape (n_samples,)
The predicted class labels for the input samples.
"""
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."
)

diff = X[:, None, :] - self.X_[None, :, :]
distances = np.linalg.norm(diff, 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.

And describe parameters
"""
Return the mean accuracy on the given test data and labels.

Parameters
----------
X : array-like of shape (n_samples, n_features)
Test samples.
y : array-like of shape (n_samples,)
True labels for X.

Returns
-------
score : float
Mean accuracy of self.predict(X) vs. 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)