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: 14 additions & 9 deletions numpy_questions.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,20 @@ 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):
raise ValueError("Input must be a numpy array.")
if X.ndim != 2:
raise ValueError("Input must be a 2D array.")

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

return i, j


def wallis_product(n_terms):
"""Implement the Wallis product to compute an approximation of pi.

See:
https://en.wikipedia.org/wiki/Wallis_product

Parameters
----------
n_terms : int
Expand All @@ -62,6 +62,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.
if n_terms == 0:
return 1.0
product = 1.0

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

return 2 * product
89 changes: 65 additions & 24 deletions sklearn_questions.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,55 +20,96 @@
`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.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

# Try importing validate_data from newer sklearn
try:
from sklearn.utils.validation import validate_data
except ImportError:
# Fallback validate_data for older sklearn versions (used on CI)
def validate_data(estimator, X, y=None, **kwargs):
"""
Fallback implementation of validate_data for older sklearn versions.

Parameters
----------
estimator : estimator instance
The estimator calling this function.
X : array-like
Input data.
y : array-like, optional
Target values.
kwargs : dict
Additional arguments (e.g., dtype, ensure_2d, reset).
"""
# Remove unsupported kwargs for check_array/check_X_y
kwargs.pop("reset", None)

if y is not None:
X, y = check_X_y(X, y, **kwargs)
estimator.n_features_in_ = X.shape[1]
return X, y
else:
X_checked = check_array(X, **kwargs)
return X_checked

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."""
# Validate input (numeric only)
X, y = check_X_y(X, y, dtype="numeric")

And describe parameters
"""
X, y = check_X_y(X, y)
check_classification_targets(y)

# Store training data
self.X_ = X
self.y_ = y

# Required sklearn attribute
self.classes_ = np.unique(y)

# Required for compatibility with predict
self.n_features_in_ = X.shape[1]

# XXX fix
return self

def predict(self, X):
"""Write docstring.

And describe parameters
"""
"""Predict class labels using 1-nearest neighbor."""
check_is_fitted(self)
X = check_array(X)
y_pred = np.full(
shape=len(X), fill_value=self.classes_[0],
dtype=self.classes_.dtype

# Validate input and check feature consistency
X = validate_data(
self, X,
dtype="numeric",
ensure_2d=True,
reset=False
)

# XXX fix
n_test = X.shape[0]
y_pred = np.empty(n_test, dtype=self.y_.dtype)

for i in range(n_test):
distances = np.sum((self.X_ - X[i]) ** 2, axis=1)
nn_index = np.argmin(distances)
y_pred[i] = self.y_[nn_index]

return y_pred

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

And describe parameters
"""
X, y = check_X_y(X, y)
"""Return mean accuracy."""
X, y = check_X_y(X, y, dtype="numeric")
y_pred = self.predict(X)

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