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
26 changes: 23 additions & 3 deletions numpy_questions.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,25 @@ 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 a numpy array")

if X.ndim != 2:
raise ValueError("Input must be a 2D array, but got shape: {}"
.format(X.shape))

i = 0
j = 0
num_rows, num_cols = X.shape

# TODO
max = 0
for i in range(num_cols):
for j in range(num_rows):
if X[i][j] > max:
max = X[i][j]
res = (i, j)

return i, j
return res


def wallis_product(n_terms):
Expand All @@ -64,4 +77,11 @@ 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
res = 1
for i in range(1, n_terms+1):
res *= (4 * i**2) / (4 * i**2 - 1)

return res * 2
81 changes: 66 additions & 15 deletions sklearn_questions.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,47 +28,98 @@
from sklearn.utils.multiclass import check_classification_targets


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

This classifier implements the 1-nearest neighbor algorithm, which
classifies a new data point based on the class of its closest neighbor
in the training set. Proximity is measured using Euclidean distance.
"""

def __init__(self): # noqa: D107
pass

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

And describe parameters
"""Train the 1-nearest neighbor classifier.

Parameters
----------
X : array-like of shape (n_samples, n_features)
Training data, where n_samples is the number of samples and
n_features is the number of features.
y : array-like of shape (n_samples,)
Target values.

Returns
-------
self : object
Returns self.
"""
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_train_ = X
self.y_train_ = y
return self

def predict(self, X):
"""Write docstring.

And describe parameters
"""Predict the class labels for the provided data.

Parameters
----------
X : array-like of shape (n_samples, n_features)
Test samples, where n_samples is the number of samples and
n_features is the number of features.

Returns
-------
y_pred : ndarray of shape (n_samples,)
Class labels for each data sample.
"""
check_is_fitted(self)
check_is_fitted(self, ['X_train_', 'y_train_'])
X = check_array(X)
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."
)
y_pred = np.full(
shape=len(X), fill_value=self.classes_[0],
dtype=self.classes_.dtype
)
for i, x in enumerate(X):
# Calculate distances to all training points
distances = np.sqrt(np.sum((self.X_train_ - x)**2, axis=1))

# Find the index of the minimum distance
nearest_idx = np.argmin(distances)

# Assign the class of the nearest neighbor
y_pred[i] = self.y_train_[nearest_idx]

# XXX fix
return y_pred

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

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

Parameters
----------
X : array-like of shape (n_samples, n_features)
Test samples, where n_samples is the number of samples and
n_features is the number of features.
y : array-like of shape (n_samples,)
True labels for X.

Returns
-------
score : float
Accuracy of the classifier, which is 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 np.mean(y_pred == y)