-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* style: fix linter * test: add tests * feat: add LinearRegression .fit and .predict method * chore: bump version to 0.2 * refactor: put fixtures parameters into constants * fix: add a bias column to the LinearRegression model * style: apply linter fixes
- Loading branch information
Showing
6 changed files
with
143 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[mypy-sklearn.*] | ||
ignore_missing_imports = True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[tool.poetry] | ||
name = "cmnemoi-learn" | ||
version = "0.1.0" | ||
version = "0.2.0" | ||
description = "Machine Learning from scratch by Charles-Meldhine Madi Mnemoi" | ||
authors = ["Charles-Meldhine Madi Mnemoi <[email protected]>"] | ||
license = "MIT" | ||
|
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
""" | ||
Fixtures for unit tests | ||
""" | ||
|
||
import numpy as np | ||
from sklearn.datasets import make_regression, make_circles | ||
import pytest | ||
|
||
BIAS = 5 | ||
NOISE = 2 | ||
NUMBER_OF_FEATURES = 2 | ||
NUMBER_OF_SAMPLES = 50 | ||
RANDOM_STATE = 42 | ||
|
||
|
||
@pytest.fixture | ||
def regression_circle_dataset() -> np.ndarray: | ||
"""Regression dataset which follows circles pattern | ||
`X, y = regression_circle_dataset` to use | ||
Returns: | ||
np.ndarray: The dataset | ||
""" | ||
return make_circles( | ||
n_samples=NUMBER_OF_SAMPLES, shuffle=False, random_state=RANDOM_STATE | ||
) | ||
|
||
|
||
@pytest.fixture | ||
def regression_linear_dataset() -> np.ndarray: | ||
"""Regression dataset which follows a linear pattern | ||
`X, y = regression_circle_dataset` to use | ||
Returns: | ||
np.ndarray: The dataset | ||
""" | ||
return make_regression( | ||
n_samples=NUMBER_OF_SAMPLES, | ||
n_features=NUMBER_OF_FEATURES, | ||
n_informative=NUMBER_OF_FEATURES, | ||
bias=BIAS, | ||
shuffle=False, | ||
random_state=RANDOM_STATE, | ||
) | ||
|
||
|
||
@pytest.fixture | ||
def regression_linear_dataset_with_noise() -> np.ndarray: | ||
"""Regression dataset which follows a linear pattern | ||
`X, y = regression_circle_dataset` to use | ||
Returns: | ||
np.ndarray: The dataset | ||
""" | ||
return make_regression( | ||
n_samples=NUMBER_OF_SAMPLES, | ||
n_features=NUMBER_OF_FEATURES, | ||
n_informative=NUMBER_OF_FEATURES, | ||
bias=BIAS, | ||
noise=NOISE, | ||
shuffle=False, | ||
random_state=RANDOM_STATE, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,61 @@ | ||
""" | ||
Unit tests for Linear Regression model | ||
""" | ||
import numpy as np | ||
|
||
from sklearn.linear_model import LinearRegression as SklearnLinearRegression | ||
|
||
from cmnemoi_learn.linear_regression import LinearRegression | ||
|
||
np.random.seed(42) | ||
|
||
|
||
def test_linear_predict(regression_linear_dataset) -> None: | ||
""" | ||
Test `predict` against sklearn implementation. | ||
""" | ||
X, y = regression_linear_dataset | ||
cmnemoi_model = LinearRegression() | ||
cmnemoi_model = cmnemoi_model.fit(X, y) | ||
|
||
sklearn_model = SklearnLinearRegression() | ||
sklearn_model = sklearn_model.fit(X, y) | ||
|
||
cmnemoi_prediction = cmnemoi_model.predict(X) | ||
sklearn_prediction = sklearn_model.predict(X) | ||
|
||
assert np.allclose(cmnemoi_prediction, sklearn_prediction) | ||
|
||
def test_predict() -> None: | ||
|
||
def test_linear_with_noise_predict(regression_linear_dataset_with_noise) -> None: | ||
""" | ||
Test `predict` againt sklearn implementation. | ||
Test `predict` against sklearn implementation. | ||
""" | ||
LinearRegression() | ||
X, y = regression_linear_dataset_with_noise | ||
cmnemoi_model = LinearRegression() | ||
cmnemoi_model = cmnemoi_model.fit(X, y) | ||
|
||
sklearn_model = SklearnLinearRegression() | ||
sklearn_model = sklearn_model.fit(X, y) | ||
|
||
cmnemoi_prediction = cmnemoi_model.predict(X) | ||
sklearn_prediction = sklearn_model.predict(X) | ||
|
||
assert np.allclose(cmnemoi_prediction, sklearn_prediction) | ||
|
||
|
||
def test_circle_predict(regression_circle_dataset) -> None: | ||
""" | ||
Test `predict` against sklearn implementation. | ||
""" | ||
X, y = regression_circle_dataset | ||
cmnemoi_model = LinearRegression() | ||
cmnemoi_model = cmnemoi_model.fit(X, y) | ||
|
||
sklearn_model = SklearnLinearRegression() | ||
sklearn_model = sklearn_model.fit(X, y) | ||
|
||
cmnemoi_prediction = cmnemoi_model.predict(X) | ||
sklearn_prediction = sklearn_model.predict(X) | ||
|
||
assert np.allclose(cmnemoi_prediction, sklearn_prediction) |