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
21 changes: 18 additions & 3 deletions numpy_questions.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,30 @@ 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("X must be a numpy array")
if X.ndim != 2:
raise ValueError("X must be a 2D array")
i = 0
j = 0

# TODO

n_samples, n_features = np.shape(X)
maxi = 0
for k in range(n_samples):
for l in range(n_features):
if X[k][l] > maxi:
maxi = X[k][l]
i = k
j = l
return i, j


def wallis_product(n_terms):
if n_terms == 0:
return 1
pi = 1
for k in range(1, n_terms+1):
pi *= (4*(k**2))/((4*(k**2))-1)
return 2*pi
"""Implement the Wallis product to compute an approximation of pi.

See:
Expand Down
58 changes: 44 additions & 14 deletions sklearn_questions.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,40 @@ def __init__(self): # noqa: D107
pass

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

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

Parameters
----------
X : ndarray of shape (n_samples, n_features)
The training input samples.
y : ndarray of shape (n_samples,)
The target labels.

Returns
-------
self : object
The fitted estimator.
"""
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 labels for the given samples.

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

And describe parameters
Returns
-------
y_pred : ndarray of shape (n_samples,)
The predicted class for each sample.
"""
check_is_fitted(self)
X = check_array(X)
Expand All @@ -59,16 +77,28 @@ def predict(self, X):
dtype=self.classes_.dtype
)

# XXX fix
for i, x in enumerate(X):
distances = np.linalg.norm(self.X_train_ - x, axis=1)
nearest = np.argmin(distances)
y_pred[i] = self.y_train_[nearest]

return y_pred

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

And describe parameters
"""Return the accuracy of the classifier on the given data.

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

Returns
-------
score : float
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)
Loading