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

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

return i, j

Expand All @@ -64,4 +71,14 @@ 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 not isinstance(n_terms, (int, np.integer)):
raise ValueError("n_terms should be an integer")
if n_terms < 0:
raise ValueError("n_terms should be non-negative")
if n_terms == 0:
return 1.0
k = np.arange(1, n_terms + 1, dtype=float)
terms = (4.0 * k**2) / (4.0 * k**2 - 1.0)
product = np.prod(terms)
pi_approx = 2.0 * product
return float(pi_approx)
75 changes: 54 additions & 21 deletions sklearn_questions.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,47 +28,80 @@
from sklearn.utils.multiclass import check_classification_targets


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

To predict the label of a sample as the label of the closest sample in
Euclidean distance in the training set.
"""

def __init__(self): # noqa: D107
pass

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

And describe parameters
"""Fit the one nearest neighbor classifier.

Parameters
----------
X : array-like of shape (n_samples, n_features)
Training data.
y : array-like of shape (n_samples,)
Target labels.

Returns
-------
self : OneNearestNeighbor
Fitted estimator.
"""
X, y = check_X_y(X, y)
check_classification_targets(y)
self.X_train_ = X
self.y_train_ = y
self.classes_ = np.unique(y)
self.n_features_in_ = X.shape[1]

# XXX fix
return self

def predict(self, X):
"""Write docstring.
"""Predict class labels for samples in X.

Parameters
----------
X: array-like of shape (n_samples, n_features)
Samples to classify.

And describe parameters
Returns
-------
y_pred : array-like of shape (n_samples,)
Predicted class labels.
"""
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 "
f"{self.__class__.__name__} is expecting "
f"{self.n_features_in_} features as input."
)
diff = self.X_train_[np.newaxis, :, :] - X[:, np.newaxis, :]
dist_sq = np.sum(diff ** 2, axis=2)
nearest_indices = np.argmin(dist_sq, axis=1)
y_pred = self.y_train_[nearest_indices]
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 the predictions on X.
"""
X, y = check_X_y(X, y)
y_pred = self.predict(X)

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