-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update test comparision with sklearn library
- Loading branch information
Showing
3 changed files
with
276 additions
and
3 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,109 @@ | ||
#!/usr/bin/env python | ||
# Created by "Thieu" at 16:47, 23/02/2024 ----------% | ||
# Email: nguyenthieu2102@gmail.com % | ||
# Github: https://github.com/thieu1995 % | ||
# --------------------------------------------------% | ||
|
||
import numpy as np | ||
from permetrics import ClusteringMetric | ||
from sklearn.metrics import mutual_info_score, normalized_mutual_info_score, \ | ||
adjusted_rand_score, rand_score, \ | ||
completeness_score, homogeneity_score, v_measure_score, \ | ||
fowlkes_mallows_score, calinski_harabasz_score, davies_bouldin_score | ||
import pytest | ||
|
||
np.random.seed(42) | ||
|
||
|
||
def is_close_enough(x1, x2, eps=1e-5): | ||
if abs(x1 - x2) <= eps: | ||
return True | ||
return False | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def internal_data(): | ||
# generate sample data | ||
X = np.random.uniform(-1, 10, size=(300, 6)) | ||
y_pred = np.random.randint(0, 3, size=300) | ||
evaluator = ClusteringMetric(y_pred=y_pred, X=X, force_finite=True) | ||
return (X, y_pred), evaluator | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def external_data(): | ||
# generate sample data | ||
y_true = np.random.randint(0, 3, size=300) | ||
y_pred = np.random.randint(0, 3, size=300) | ||
evaluator = ClusteringMetric(y_true=y_true, y_pred=y_pred, force_finite=True) | ||
return (y_true, y_pred), evaluator | ||
|
||
|
||
def test_MIS(external_data): | ||
(y_true, y_pred), cm = external_data | ||
res1 = cm.MIS() | ||
res2 = mutual_info_score(y_true, y_pred) | ||
assert is_close_enough(res1, res2) | ||
|
||
|
||
def test_NMIS(external_data): | ||
(y_true, y_pred), cm = external_data | ||
res1 = cm.NMIS() | ||
res2 = normalized_mutual_info_score(y_true, y_pred) | ||
assert is_close_enough(res1, res2) | ||
|
||
|
||
def test_RaS(external_data): | ||
(y_true, y_pred), cm = external_data | ||
res1 = cm.RaS() | ||
res2 = rand_score(y_true, y_pred) | ||
assert is_close_enough(res1, res2) | ||
|
||
|
||
def test_ARS(external_data): | ||
(y_true, y_pred), cm = external_data | ||
res1 = cm.ARS() | ||
res2 = adjusted_rand_score(y_true, y_pred) | ||
assert is_close_enough(res1, res2) | ||
|
||
|
||
def test_CS(external_data): | ||
(y_true, y_pred), cm = external_data | ||
res1 = cm.CS() | ||
res2 = completeness_score(y_true, y_pred) | ||
assert is_close_enough(res1, res2) | ||
|
||
|
||
def test_HS(external_data): | ||
(y_true, y_pred), cm = external_data | ||
res1 = cm.HS() | ||
res2 = homogeneity_score(y_true, y_pred) | ||
assert is_close_enough(res1, res2) | ||
|
||
|
||
def test_VMS(external_data): | ||
(y_true, y_pred), cm = external_data | ||
res1 = cm.VMS() | ||
res2 = v_measure_score(y_true, y_pred) | ||
assert is_close_enough(res1, res2) | ||
|
||
|
||
def test_FMS(external_data): | ||
(y_true, y_pred), cm = external_data | ||
res1 = cm.FMS() | ||
res2 = fowlkes_mallows_score(y_true, y_pred) | ||
assert is_close_enough(res1, res2) | ||
|
||
|
||
def test_CHI(internal_data): | ||
(y_true, y_pred), cm = internal_data | ||
res1 = cm.CHI() | ||
res2 = calinski_harabasz_score(y_true, y_pred) | ||
assert is_close_enough(res1, res2) | ||
|
||
|
||
def test_DBI(internal_data): | ||
(y_true, y_pred), cm = internal_data | ||
res1 = cm.DBI() | ||
res2 = davies_bouldin_score(y_true, y_pred) | ||
assert is_close_enough(res1, res2) |
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,68 @@ | ||
#!/usr/bin/env python | ||
# Created by "Thieu" at 18:21, 22/02/2024 ----------% | ||
# Email: nguyenthieu2102@gmail.com % | ||
# Github: https://github.com/thieu1995 % | ||
# --------------------------------------------------% | ||
|
||
import numpy as np | ||
from permetrics import RegressionMetric | ||
from sklearn.metrics import explained_variance_score, max_error, mean_absolute_error, \ | ||
mean_squared_error, median_absolute_error, r2_score, mean_absolute_percentage_error | ||
import pytest | ||
|
||
|
||
@pytest.fixture(scope="module") # scope: Call only 1 time at the beginning | ||
def data(): | ||
y_true = np.array([3, -0.5, 2, 7, 5, 3, 4, -3, 10]) | ||
y_pred = np.array([2.5, 0.0, 2, 8, 5, 2, 3.5, -4, 9]) | ||
rm = RegressionMetric(y_true=y_true, y_pred=y_pred) | ||
return y_true, y_pred, rm | ||
|
||
|
||
def test_EVS(data): | ||
y_true, y_pred, rm = data | ||
res11 = rm.EVS() | ||
res12 = explained_variance_score(y_true, y_pred) | ||
assert res11 == res12 | ||
|
||
|
||
def test_ME(data): | ||
y_true, y_pred, rm = data | ||
res11 = rm.ME() | ||
res12 = max_error(y_true, y_pred) | ||
assert res11 == res12 | ||
|
||
|
||
def test_MAE(data): | ||
y_true, y_pred, rm = data | ||
res11 = rm.MAE() | ||
res12 = mean_absolute_error(y_true, y_pred) | ||
assert res11 == res12 | ||
|
||
|
||
def test_MSE(data): | ||
y_true, y_pred, rm = data | ||
res11 = rm.MSE() | ||
res12 = mean_squared_error(y_true, y_pred) | ||
assert res11 == res12 | ||
|
||
|
||
def test_MedAE(data): | ||
y_true, y_pred, rm = data | ||
res11 = rm.MedAE() | ||
res12 = median_absolute_error(y_true, y_pred) | ||
assert res11 == res12 | ||
|
||
|
||
def test_R2(data): | ||
y_true, y_pred, rm = data | ||
res11 = rm.R2() | ||
res12 = r2_score(y_true, y_pred) | ||
assert res11 == res12 | ||
|
||
|
||
def test_MAPE(data): | ||
y_true, y_pred, rm = data | ||
res11 = rm.MAPE() | ||
res12 = mean_absolute_percentage_error(y_true, y_pred) | ||
assert res11 == res12 |