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
18 changes: 14 additions & 4 deletions numpy_questions.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,12 @@ def max_index(X):
"""
i = 0
j = 0

# TODO

if not isinstance(X, np.ndarray):
raise ValueError("The X input must be an Array!")
if X.ndim != 2:
raise ValueError("The X input must be 2-Dimensional!")
indexes = np.argmax(X)
i, j = np.unravel_index(indexes, X.shape)
return i, j


Expand All @@ -64,4 +67,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.
wp = 1.0
if n_terms == 0:
return wp
else:
for n in range(1, n_terms + 1):
wp = wp * (4*(n**2))/(4*(n**2) - 1)
pi = wp * 2
return pi
65 changes: 50 additions & 15 deletions sklearn_questions.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,34 +23,59 @@
from sklearn.base import BaseEstimator
from sklearn.base import ClassifierMixin
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
from sklearn.utils.validation import check_array
from sklearn.metrics import pairwise_distances_argmin_min


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 OneNearestNeighbor classifier.

This function stores training data X and the labels y.

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

y : Target labels (n_samples).

And describe parameters
Returns
-------
self : returns the fitted classifier.

Raises
------
ValueError
If X and y have different numbers of samples
"""
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_ = np.asarray(X)
self.y_ = np.asarray(y)
return self

def predict(self, X):
"""Write docstring.
"""Return the predicted class for a data set in an numpy array.

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

And describe parameters
Returns
-------
y_pred : ndarray of shape (n_samples)
The predicted classes for the n_samples.
"""
check_is_fitted(self)
X = check_array(X)
Expand All @@ -59,16 +84,26 @@ def predict(self, X):
dtype=self.classes_.dtype
)

# XXX fix
indexes, _ = pairwise_distances_argmin_min(X, self.X_)
y_pred = self.y_[indexes]
return y_pred

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

And describe parameters
"""Return the score of the OneNearestNeighbor on a data set.

Parameters
----------
X : ndarray of shape (n_samples, n_features)
The input array.
y : ndarray of shape (n_samples)
The true classes of the samples.

Returns
-------
score : float
The percentage of samples accurately predicted.
"""
X, y = check_X_y(X, y)
y_pred = self.predict(X)

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