Skip to content

Commit b0255a8

Browse files
committed
added doctests
1 parent 544a38b commit b0255a8

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
"""
2+
Doctest for RidgeRegression class
3+
4+
Tests include:
5+
- feature_scaling
6+
- fit
7+
- predict
8+
- mean_absolute_error
9+
10+
To run these tests, use the following command:
11+
python -m doctest test_ridge_regression.py -v
12+
"""
13+
14+
import numpy as np
15+
from ridge_regression import RidgeRegression
16+
17+
def test_feature_scaling():
18+
"""
19+
Tests the feature_scaling function of RidgeRegression.
20+
--------
21+
>>> model = RidgeRegression()
22+
>>> X = np.array([[1, 2], [2, 3], [3, 4]])
23+
>>> X_scaled, mean, std = model.feature_scaling(X)
24+
>>> np.round(X_scaled, 2)
25+
array([[-1.22, -1.22],
26+
[ 0. , 0. ],
27+
[ 1.22, 1.22]])
28+
>>> np.round(mean, 2)
29+
array([2., 3.])
30+
>>> np.round(std, 2)
31+
array([0.82, 0.82])
32+
"""
33+
pass
34+
35+
def test_fit():
36+
"""
37+
Tests the fit function of RidgeRegression
38+
--------
39+
>>> model = RidgeRegression(alpha=0.01, regularization_param=0.1, num_iterations=1000)
40+
>>> X = np.array([[1], [2], [3]])
41+
>>> y = np.array([2, 3, 4])
42+
43+
# Adding a bias term
44+
>>> X = np.c_[np.ones(X.shape[0]), X]
45+
46+
# Fit the model
47+
>>> model.fit(X, y)
48+
49+
# Check if the weights have been updated
50+
>>> np.round(model.theta, decimals=2)
51+
array([0. , 0.79])
52+
"""
53+
pass
54+
55+
def test_predict():
56+
"""
57+
Tests the predict function of RidgeRegression
58+
--------
59+
>>> model = RidgeRegression(alpha=0.01, regularization_param=0.1, num_iterations=1000)
60+
>>> X = np.array([[1], [2], [3]])
61+
>>> y = np.array([2, 3, 4])
62+
63+
# Adding a bias term
64+
>>> X = np.c_[np.ones(X.shape[0]), X]
65+
66+
# Fit the model
67+
>>> model.fit(X, y)
68+
69+
# Predict with the model
70+
>>> predictions = model.predict(X)
71+
>>> np.round(predictions, decimals=2)
72+
array([-0.97, 0. , 0.97])
73+
"""
74+
pass
75+
76+
def test_mean_absolute_error():
77+
"""
78+
Tests the mean_absolute_error function of RidgeRegression
79+
--------
80+
>>> model = RidgeRegression()
81+
>>> y_true = np.array([2, 3, 4])
82+
>>> y_pred = np.array([2.1, 3.0, 3.9])
83+
>>> mae = model.mean_absolute_error(y_true, y_pred)
84+
>>> float(np.round(mae, 2))
85+
0.07
86+
"""
87+
pass
88+
89+
if __name__ == "__main__":
90+
import doctest
91+
doctest.testmod()

0 commit comments

Comments
 (0)