Skip to content

Commit ddb4638

Browse files
committed
machine_learning: accept numpy.matrix inputs and add test
1 parent a0db9d6 commit ddb4638

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

machine_learning/ridge_regression.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
from __future__ import annotations
2727

2828
from dataclasses import dataclass
29-
from typing import Optional
29+
3030

3131
import httpx
3232
import numpy as np
@@ -62,10 +62,11 @@ def __post_init__(self) -> None:
6262

6363
@staticmethod
6464
def _add_intercept(features: np.ndarray) -> np.ndarray:
65-
if features.ndim != 2:
65+
arr = np.asarray(features, dtype=float)
66+
if arr.ndim != 2:
6667
raise ValueError("features must be a 2D array")
67-
n_samples = features.shape[0]
68-
return np.c_[np.ones(n_samples), features]
68+
n_samples = arr.shape[0]
69+
return np.c_[np.ones(n_samples), arr]
6970

7071
def fit(
7172
self, features: np.ndarray, target: np.ndarray, add_intercept: bool = True
@@ -81,6 +82,9 @@ def fit(
8182
add_intercept: bool
8283
If True the model will add a bias column of ones to `features`.
8384
"""
85+
features = np.asarray(features, dtype=float)
86+
target = np.asarray(target, dtype=float)
87+
8488
if features.ndim != 2:
8589
raise ValueError("features must be a 2D array")
8690
if target.ndim != 1:
@@ -119,6 +123,8 @@ def predict(self, features: np.ndarray, add_intercept: bool = True) -> np.ndarra
119123
"""
120124
if self.weights is None:
121125
raise ValueError("Model is not trained")
126+
127+
features = np.asarray(features, dtype=float)
122128
x = features if not add_intercept else self._add_intercept(features)
123129
return x @ self.weights
124130

machine_learning/tests/test_ridge_regression.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,17 @@ def test_input_validation():
4949
model = RidgeRegression()
5050
with pytest.raises(ValueError):
5151
model.fit(np.array([1, 2, 3]), np.array([1, 2, 3]))
52+
53+
54+
def test_accepts_numpy_matrix():
55+
from machine_learning.ridge_regression import collect_dataset
56+
57+
data = collect_dataset()
58+
X = np.c_[data[:, 0].astype(float)] # numpy.matrix
59+
y = np.ravel(data[:, 1].astype(float))
60+
61+
model = RidgeRegression(learning_rate=0.0002, lambda_=0.01, epochs=500)
62+
model.fit(X, y)
63+
preds = model.predict(X)
64+
assert preds.shape == (y.shape[0],)
65+
assert mean_absolute_error(preds, y) >= 0.0

0 commit comments

Comments
 (0)