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
23 changes: 17 additions & 6 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.
"""
i = 0
j = 0
if not isinstance(X, np.ndarray) or X.ndim != 2:
raise ValueError("Input array should be a 2-dimensional numpy array")

# TODO
i, j = 0, 0
max_val = X[0][0]
for row in range(X.shape[0]):
for col in range(X.shape[1]):
if X[row][col] > max_val:
i, j = row, col
max_val = X[row][col]

return i, j

Expand All @@ -62,6 +68,11 @@ def wallis_product(n_terms):
pi : float
The approximation of order `n_terms` of pi using the Wallis product.
"""
# XXX : The n_terms is an int that corresponds to the number of
# terms in the product. For example 10000.
return 0.
res = 1
if n_terms == 0:
return 1

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

return 2 * res
59 changes: 51 additions & 8 deletions sklearn_questions.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,46 @@ def __init__(self): # noqa: D107
pass

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

And describe parameters
"""
Train the OneNearestNeighbor predictor.

Parameters
----------
X : array-like of shape (n_samples, n_features)
Train set.
y : array-like of shape (n_samples,)
Target variable.

Returns
-------
self : object

"""
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.
"""
Predict the class of each sample of X.

And describe parameters
Parameters
----------
X : array-like of shape (n_samples, n_features)
test set.

Returns
-------
y_pred : ndarray of shape (n_samples,)
predictions
"""
check_is_fitted(self)
X = check_array(X)
Expand All @@ -60,15 +84,34 @@ def predict(self, X):
)

# XXX fix
for i, x in enumerate(X):

distances = np.sqrt(np.sum((self.X_train_ - x) ** 2, axis=1))

idx_min = np.argmin(distances)

y_pred[i] = self.y_train_[idx_min]

return y_pred

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

And describe parameters
"""
Evaluate the performance of the model

Parameters
----------
X : array-like of shape (n_samples, n_features)
test set.
y : array-like of shape (n_samples,)
Target variable.

Returns
-------
accuracy: percentage of good predictions
"""
X, y = check_X_y(X, y)
y_pred = self.predict(X)

# XXX fix
y_pred=(y_pred==y)/len(y)
return y_pred.sum()
Loading