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
23 changes: 18 additions & 5 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("Error : input must be a numpy array.")
if X.ndim != 2:
raise ValueError("Error : input must be 2D.")

return i, j
index = np.argmax(X) # index in the flattened array
i, j = np.unravel_index(index, X.shape) # convert to (row, col)

return int(i), int(j)


def wallis_product(n_terms):
Expand All @@ -62,6 +68,13 @@ 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, np.integer)) or n_terms < 0:
raise ValueError("Error : input must be a non negative integer.")

if n_terms == 0:
return 1

k = np.arange(1, n_terms + 1, dtype=float)
terms = (4.0 * k * k) / (4.0 * k * k - 1.0)
prod = np.prod(terms)
return float(2.0 * prod)
84 changes: 61 additions & 23 deletions sklearn_questions.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,47 +28,85 @@
from sklearn.utils.multiclass import check_classification_targets


class OneNearestNeighbor(BaseEstimator, ClassifierMixin):
"OneNearestNeighbor classifier."
class OneNearestNeighbor(ClassifierMixin, BaseEstimator):
"""One-nearest-neighbor classifier.

This estimator predicts the label of each input sample as the label of
the single closest training sample under the Euclidean distance.
"""

def __init__(self): # noqa: D107
pass

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

And describe parameters
"""Fit the classifier.

The fitting process for OneNearestNeighbor only means storing
the training data, as it is a lazy learning algorithm.

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.classes_ = np.unique(y)
self.n_features_in_ = X.shape[1]

# XXX fix
self.X_ = X
self.y_ = y
self.n_features_in_ = X.shape[1]
self.classes_ = np.unique(y)
return self

def predict(self, X):
"""Write docstring.
"""Predict class labels for the provided data.

Parameters
----------
X : array-like of shape (n_samples, n_features)
Input samples.

And describe parameters
Returns
-------
y_pred : ndarray 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
check_is_fitted(self, attributes=("X_", "y_"))
X = self._validate_data(X, reset=False)
X_sq = np.sum(X ** 2, axis=1, keepdims=True)
Xt_sq = np.sum(self.X_ ** 2, axis=1, keepdims=True).T
dist_2 = X_sq + Xt_sq - 2 * (X @ self.X_.T)
nn_index = np.argmin(dist_2, axis=1)
y_pred = self.y_[nn_index]

return y_pred

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

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

Returns
-------
float
Accuracy of ``self.predict(X)`` vs ``y``.
"""
X, y = check_X_y(X, y)
y = check_array(y, ensure_2d=False)
if y.shape[0] != X.shape[0]:
raise ValueError("X and y have incompatible shapes.")

y_pred = self.predict(X)

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