Skip to content
Open
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
Binary file added .DS_Store
Binary file not shown.
25 changes: 20 additions & 5 deletions numpy_questions.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
This will be enforced with `flake8`. You can check that there is no flake8
errors by calling `flake8` at the root of the repo.
"""

import numpy as np


Expand All @@ -37,12 +38,17 @@ def max_index(X):
If the input is not a numpy array or
if the shape is not 2D.
"""
i = 0
j = 0
i = -1
j = -1

if not isinstance(X, np.ndarray):
raise ValueError("Input should be a numpy array.")
if len(X.shape) != 2:
raise ValueError("Input should be a 2D numpy array.")

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

return i, j
return (i, j)


def wallis_product(n_terms):
Expand All @@ -64,4 +70,13 @@ 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.0

pi_half = 1

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

return 2 * pi_half
66 changes: 50 additions & 16 deletions sklearn_questions.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,55 +20,89 @@
`pydocstyle` that you can also call at the root of the repo.
"""
import numpy as np
from sklearn.base import BaseEstimator
from sklearn.base import ClassifierMixin
from sklearn.base import ClassifierMixin, BaseEstimator
from sklearn.utils.validation import check_X_y
from sklearn.utils.validation import check_array
from sklearn.utils.validation import check_is_fitted
from sklearn.utils.multiclass import check_classification_targets


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

def __init__(self): # noqa: D107
pass

def fit(self, X, y):
"""Write docstring.
"""Fit the one nearest neighbor classifier from the training dataset.

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

y : ndarray of shape (n_samples)
The target values.
"""
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.
"""Predict the class labels for new data points X_i.

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

Returns
-------
y_pred : ndarray of shape (n_samples)
The predicted class labels.
"""
check_is_fitted(self)
X = check_array(X)
X = check_array(X, reset=False)

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

# XXX fix
for i in range(len(X)):
distances = np.linalg.norm(self.X_ - X[i, :], axis=1)
y_pred[i] = self.y_[np.argmin(distances)]

return y_pred

def score(self, X, y):
"""Write docstring.
"""Return the classifier accuracy on the given test data and labels.

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

And describe parameters
y : ndarray of shape (n_samples)
The target values.

Returns
-------
score : float
The accuracy of the classifier;
number of samples corectly classified.
"""
X, y = check_X_y(X, y)
y_pred = self.predict(X)

# XXX fix
return y_pred.sum()
accurate_points = 0
for i in range(len(y)):
if y_pred[i] == y[i]:
accurate_points += 1

return accurate_points / len(y)
Loading