mirror of
https://github.com/Doctorado-ML/mufs.git
synced 2025-08-17 08:35:52 +00:00
Rename Project and first working version
This commit is contained in:
49
mfs/tests/MFS_test.py
Executable file
49
mfs/tests/MFS_test.py
Executable file
@@ -0,0 +1,49 @@
|
||||
import unittest
|
||||
from mdlp import MDLP
|
||||
from sklearn.datasets import load_wine
|
||||
|
||||
from ..Selection import MFS
|
||||
|
||||
|
||||
class MFS_test(unittest.TestCase):
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
mdlp = MDLP(random_state=1)
|
||||
X, self.y = load_wine(return_X_y=True)
|
||||
self.X = mdlp.fit_transform(X, self.y).astype("int64")
|
||||
self.m, self.n = self.X.shape
|
||||
|
||||
# @classmethod
|
||||
# def setup(cls):
|
||||
# pass
|
||||
|
||||
def test_initialize(self):
|
||||
mfs = MFS()
|
||||
mfs.fcbs(self.X, self.y, 0.05)
|
||||
mfs._initialize()
|
||||
self.assertIsNone(mfs.get_results())
|
||||
self.assertListEqual([], mfs.get_scores())
|
||||
self.assertDictEqual({}, mfs._su_features)
|
||||
self.assertIsNone(mfs._su_labels)
|
||||
|
||||
def test_csf(self):
|
||||
mfs = MFS()
|
||||
expected = [6, 4]
|
||||
self.assertListEqual(expected, mfs.cfs(self.X, self.y).get_results())
|
||||
expected = [0.5218299405215557, 2.4168234005280964]
|
||||
self.assertListEqual(expected, mfs.get_scores())
|
||||
|
||||
def test_fcbs(self):
|
||||
mfs = MFS()
|
||||
computed = mfs.fcbs(self.X, self.y, threshold=0.05).get_results()
|
||||
expected = [6, 9, 12, 0, 11, 4]
|
||||
self.assertListEqual(expected, computed)
|
||||
expected = [
|
||||
0.5218299405215557,
|
||||
0.46224298637417455,
|
||||
0.44518278979085646,
|
||||
0.38942355544213786,
|
||||
0.3790082191220976,
|
||||
0.24972405134844652,
|
||||
]
|
||||
self.assertListEqual(expected, mfs.get_scores())
|
89
mfs/tests/Metrics_test.py
Executable file
89
mfs/tests/Metrics_test.py
Executable file
@@ -0,0 +1,89 @@
|
||||
import unittest
|
||||
from sklearn.datasets import load_iris
|
||||
from mdlp import MDLP
|
||||
from ..Selection import Metrics
|
||||
|
||||
|
||||
class Metrics_test(unittest.TestCase):
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
mdlp = MDLP(random_state=1)
|
||||
X, self.y = load_iris(return_X_y=True)
|
||||
self.X = mdlp.fit_transform(X, self.y).astype("int64")
|
||||
self.m, self.n = self.X.shape
|
||||
|
||||
# @classmethod
|
||||
# def setup(cls):
|
||||
|
||||
def test_entropy(self):
|
||||
metric = Metrics()
|
||||
datasets = [
|
||||
([0, 0, 0, 0, 1, 1, 1, 1], 2, 1.0),
|
||||
([0, 1, 0, 2, 1, 2], 3, 1.0),
|
||||
([0, 0, 0, 0, 0, 0, 0, 2, 2, 2], 2, 0.8812908992306927),
|
||||
([1, 1, 1, 5, 2, 2, 3, 3, 3], 4, 0.9455305560363263),
|
||||
([1, 1, 1, 2, 2, 3, 3, 3, 5], 4, 0.9455305560363263),
|
||||
([1, 1, 5], 2, 0.9182958340544896),
|
||||
(self.y, 3, 0.999999999),
|
||||
]
|
||||
for dataset, base, entropy in datasets:
|
||||
computed = metric.entropy(dataset, base)
|
||||
self.assertAlmostEqual(entropy, computed)
|
||||
|
||||
def test_conditional_entropy(self):
|
||||
metric = Metrics()
|
||||
results_expected = [
|
||||
0.490953458537736,
|
||||
0.7110077966379169,
|
||||
0.15663362014829718,
|
||||
0.13032469395094992,
|
||||
]
|
||||
for expected, col in zip(results_expected, range(self.n)):
|
||||
computed = metric.conditional_entropy(self.X[:, col], self.y, 3)
|
||||
self.assertAlmostEqual(expected, computed)
|
||||
self.assertAlmostEqual(
|
||||
0.6309297535714573,
|
||||
metric.conditional_entropy(
|
||||
[1, 3, 2, 3, 2, 1], [1, 2, 0, 1, 1, 2], 3
|
||||
),
|
||||
)
|
||||
# https://planetcalc.com/8414/?joint=0.4%200%0A0.2%200.4&showDetails=1
|
||||
self.assertAlmostEqual(
|
||||
0.5509775004326938,
|
||||
metric.conditional_entropy([1, 1, 2, 2, 2], [0, 0, 0, 2, 2], 2),
|
||||
)
|
||||
|
||||
def test_information_gain(self):
|
||||
metric = Metrics()
|
||||
results_expected = [
|
||||
0.5090465414622638,
|
||||
0.28899220336208287,
|
||||
0.8433663798517026,
|
||||
0.8696753060490499,
|
||||
]
|
||||
for expected, col in zip(results_expected, range(self.n)):
|
||||
computed = metric.information_gain(self.X[:, col], self.y, 3)
|
||||
self.assertAlmostEqual(expected, computed)
|
||||
# https://planetcalc.com/8419/
|
||||
# ?_d=FrDfFN2COAhqh9Pb5ycqy5CeKgIOxlfSjKgyyIR.Q5L0np-g-hw6yv8M1Q8_
|
||||
results_expected = [
|
||||
0.806819679,
|
||||
0.458041805,
|
||||
1.336704086,
|
||||
1.378402748,
|
||||
]
|
||||
for expected, col in zip(results_expected, range(self.n)):
|
||||
computed = metric.information_gain(self.X[:, col], self.y, 2)
|
||||
self.assertAlmostEqual(expected, computed)
|
||||
|
||||
def test_symmetrical_uncertainty(self):
|
||||
metric = Metrics()
|
||||
results_expected = [
|
||||
0.33296547388990266,
|
||||
0.19068147573570668,
|
||||
0.810724587460511,
|
||||
0.870521418179061,
|
||||
]
|
||||
for expected, col in zip(results_expected, range(self.n)):
|
||||
computed = metric.symmetrical_uncertainty(self.X[:, col], self.y)
|
||||
self.assertAlmostEqual(expected, computed)
|
4
mfs/tests/__init__.py
Normal file
4
mfs/tests/__init__.py
Normal file
@@ -0,0 +1,4 @@
|
||||
from .MFS_test import MFS_test
|
||||
from .Metrics_test import Metrics_test
|
||||
|
||||
__all__ = ["MFS_test", "Metrics_test"]
|
Reference in New Issue
Block a user