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
20 changes: 19 additions & 1 deletion numpy_questions.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,18 @@ def max_index(X):
j = 0

# TODO
if not isinstance(X, np.ndarray):
raise ValueError
if X.ndim != 2:
raise ValueError

max = X[i, j]
for index_row in range(X.shape[0]):
for index_col in range(X.shape[1]):
if X[index_row, index_col] > max:
max = X[index_row, index_col]
i = index_row
j = index_col

return i, j

Expand All @@ -64,4 +76,10 @@ 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

pi_over_2 = 1
for n in range(1, n_terms+1):
pi_over_2 *= (4*(n**2))/(4*(n**2)-1)
return 2*pi_over_2
58 changes: 48 additions & 10 deletions sklearn_questions.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,28 +29,50 @@


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

def __init__(self): # noqa: D107
pass

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

And describe parameters
"""Fit the OneNearestNeighbor classifier on training data.

Parameters
----------
self : defines the instance of the class OneNearestNeighbor we are
working on
X : ndarray of the training data,
with shape (n_observations, p_features)
y : 1-darray of the labels associated with each dimension of X,
with shape (n_observations)

Returns
-------
self : maintains the instance
"""
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 # store X and y as "learned" data,
# ensure we have trained on X and y
return self

def predict(self, X):
"""Write docstring.
"""Predict y label for input data X using the OneNearestNeighbor rule.

Parameters
-------
self : still maintain the instance of the class
X : ndarray of test data, with shape (n_observations, p_features)

And describe parameters
Returns
-------
y_pred : 1-darray of labels predicted for the test data X,
with shape (n_observations,)
"""
check_is_fitted(self)
X = check_array(X)
Expand All @@ -60,15 +82,31 @@ def predict(self, X):
)

# XXX fix
idx = 0
for x in X:
euclidean_distances = np.sqrt(np.sum((self.X_ - x) ** 2, axis=1))
NN_index = np.argmin(euclidean_distances)
NN = self.y_[NN_index]
y_pred[idx] = NN
idx += 1
return y_pred

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

And describe parameters
"""Score model performance by evaluating proportion of accurate y_pred.

Parameters
-------
X : ndarray of test data, with shape (n_observations, p_features)
y : 1d-array of the true labels associated with test samples X,
with shape (n_observations,)

Returns
-------
a score : float in [0,1]
reflecting the proportion of accurate predictions
"""
X, y = check_X_y(X, y)
y_pred = self.predict(X)

# XXX fix
return y_pred.sum()
return sum(y_pred == y)/len(y)
2 changes: 1 addition & 1 deletion students.txt
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ Liard Eléanor
Liu Guangyue X
Liu Yunxian
Lucille Maximilien X
Mahé Blanche
Mahé Blanche X
Martin Justin X
Massias Mathurin
Massoud Alexandre.....X
Expand Down