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
22 changes: 20 additions & 2 deletions numpy_questions.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,16 @@ def max_index(X):
If the input is not a numpy array or
if the shape is not 2D.
"""
if not isinstance(X, np.ndarray):
raise ValueError("Input must be an array")
if X.ndim != 2:
raise ValueError("Array must be 2D")

i = 0
j = 0

# TODO
i = np.argmax(np.max(X, axis=1)) # index of row with the largest value
j = np.argmax(X[i, :]) # index of max within that row

return i, j

Expand All @@ -64,4 +70,16 @@ 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

product = 1

for n in range(1, n_terms + 1):
t = (2 * n / (2 * n - 1)) * (2 * n / (2 * n + 1))
product *= t

pi = 2 * product

return pi
86 changes: 68 additions & 18 deletions sklearn_questions.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,46 +29,96 @@


class OneNearestNeighbor(BaseEstimator, ClassifierMixin):
"OneNearestNeighbor classifier."
"""One-Nearest-Neighbor classifier.

This classifier assigns to each sample the label of the closest
training point in Euclidean distance. Only a single neighbor is
considered.
"""

def __init__(self): # noqa: D107
pass

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

And describe parameters
"""Fit the OneNearestNeighbor classifier.

Parameters
----------
X : ndarray of shape (n_samples, n_features)
Training data.

y : ndarray of shape (n_samples,)
Target labels.

Returns
-------
self : object
Fitted estimator.

Raises
------
ValueError
If `X` and `y` have incompatible shapes or if `y`
is not a valid classification target.
"""
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.

And describe parameters
"""Predict class labels for given samples.

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

Returns
-------
y_pred : ndarray of shape (n_samples,)
Predicted class labels.

Raises
------
ValueError
If estimator has not been fitted.
"""
check_is_fitted(self)
X = check_array(X)
y_pred = np.full(
shape=len(X), fill_value=self.classes_[0],
dtype=self.classes_.dtype

distances = np.linalg.norm(
X[:, np.newaxis, :] - self.X_[np.newaxis, :, :],
axis=2,
)

# XXX fix
return y_pred
nearest_idx = np.argmin(distances, axis=1)

return self.y_[nearest_idx]

def score(self, X, y):
"""Write docstring.
"""Return the accuracy of the classifier.

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

And describe parameters
y : ndarray of shape (n_samples,)
True labels.

Returns
-------
score : float
Classification accuracy, i.e. the proportion of correct
predictions.
"""
X, y = check_X_y(X, y)
y_pred = self.predict(X)

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