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: 11 additions & 8 deletions numpy_questions.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,20 @@ def max_index(X):
Returns
-------
(i, j) : tuple(int)
The row and columnd index of the maximum.
The row and column index of the maximum.

Raises
------
ValueError
If the input is not a numpy array or
if the shape is not 2D.
"""
i = 0
j = 0
if not isinstance(X, np.ndarray) or X.ndim != 2:
raise ValueError("X must be a 2D numpy array")

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

return i, j
return int(i), int(j)


def wallis_product(n_terms):
Expand All @@ -62,6 +62,9 @@ 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 n_terms == 0:
return 1.0
n = np.arange(1, n_terms + 1, dtype=float)
pi = 2 * np.prod(4 * n ** 2/(4 * n ** 2 - 1))

return pi
68 changes: 53 additions & 15 deletions sklearn_questions.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,46 +30,84 @@


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

def __init__(self): # noqa: D107
pass

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

And describe parameters
"""Fit the classifier by using the training data.

Parameters
----------
X : ndarray of shape (n_samples, n_features)
The input array.
y : ndarray of shape (n_samples,)
The target array (class labels).

Returns
-------
self : OneNearestNeighbor
The fitted estimator.
"""
X, y = check_X_y(X, y)
X, y = validate_data(self, X, y)
check_classification_targets(y)
self.classes_ = np.unique(y)
self.n_features_in_ = X.shape[1]
self.X_ = X
self.y_ = y
return self

# XXX fix
return self

def predict(self, X):
"""Write docstring.
"""Predict the class of each sample from its nearest neighbor.

Parameters
----------
X : ndarray of shape (n_samples, n_features)
The input array.

And describe parameters
Returns
-------
y : ndarray of shape(n_samples,)
The predicted class labels
"""
check_is_fitted(self)
X = check_array(X)
if X.shape[1] != self.n_features_in_:
raise ValueError(
f"X has {X.shape[1]} features, but {self.__class__.__name__} "
f"is expecting {self.n_features_in_} features as input"
)

y_pred = np.full(
shape=len(X), fill_value=self.classes_[0],
dtype=self.classes_.dtype
)

# XXX fix
diffs = X[:, np.newaxis, :] - self.X_[np.newaxis, :, :]
distances = np.sqrt((diffs ** 2).sum(axis=2))
nearest = np.argmin(distances, axis=1)
y_pred[:] = self.y_[nearest]

return y_pred

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

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

Parameters
----------
X : ndarray of shape (n_samples, n_features)
The input array.
y : ndarray of shape (n_samples,)
The target array (class labels).

Returns
-------
score : float
The fraction of correctly classified samples.
"""
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))
Loading