mirror of
https://github.com/Doctorado-ML/STree.git
synced 2025-08-16 16:06:01 +00:00
Complete graphviz test
Add comments to some tests
This commit is contained in:
@@ -358,6 +358,7 @@ class Stree_test(unittest.TestCase):
|
|||||||
|
|
||||||
# Tests of score
|
# Tests of score
|
||||||
def test_score_binary(self):
|
def test_score_binary(self):
|
||||||
|
"""Check score for binary classification."""
|
||||||
X, y = load_dataset(self._random_state)
|
X, y = load_dataset(self._random_state)
|
||||||
accuracies = [
|
accuracies = [
|
||||||
0.9506666666666667,
|
0.9506666666666667,
|
||||||
@@ -380,6 +381,7 @@ class Stree_test(unittest.TestCase):
|
|||||||
self.assertAlmostEqual(accuracy_expected, accuracy_score)
|
self.assertAlmostEqual(accuracy_expected, accuracy_score)
|
||||||
|
|
||||||
def test_score_max_features(self):
|
def test_score_max_features(self):
|
||||||
|
"""Check score using max_features."""
|
||||||
X, y = load_dataset(self._random_state)
|
X, y = load_dataset(self._random_state)
|
||||||
clf = Stree(
|
clf = Stree(
|
||||||
kernel="liblinear",
|
kernel="liblinear",
|
||||||
@@ -391,6 +393,7 @@ class Stree_test(unittest.TestCase):
|
|||||||
self.assertAlmostEqual(0.9453333333333334, clf.score(X, y))
|
self.assertAlmostEqual(0.9453333333333334, clf.score(X, y))
|
||||||
|
|
||||||
def test_bogus_splitter_parameter(self):
|
def test_bogus_splitter_parameter(self):
|
||||||
|
"""Check that bogus splitter parameter raises exception."""
|
||||||
clf = Stree(splitter="duck")
|
clf = Stree(splitter="duck")
|
||||||
with self.assertRaises(ValueError):
|
with self.assertRaises(ValueError):
|
||||||
clf.fit(*load_dataset())
|
clf.fit(*load_dataset())
|
||||||
@@ -446,6 +449,7 @@ class Stree_test(unittest.TestCase):
|
|||||||
self.assertListEqual([47], resdn[1].tolist())
|
self.assertListEqual([47], resdn[1].tolist())
|
||||||
|
|
||||||
def test_score_multiclass_rbf(self):
|
def test_score_multiclass_rbf(self):
|
||||||
|
"""Test score for multiclass classification with rbf kernel."""
|
||||||
X, y = load_dataset(
|
X, y = load_dataset(
|
||||||
random_state=self._random_state,
|
random_state=self._random_state,
|
||||||
n_classes=3,
|
n_classes=3,
|
||||||
@@ -463,6 +467,7 @@ class Stree_test(unittest.TestCase):
|
|||||||
self.assertEqual(1.0, clf2.fit(X, y).score(X, y))
|
self.assertEqual(1.0, clf2.fit(X, y).score(X, y))
|
||||||
|
|
||||||
def test_score_multiclass_poly(self):
|
def test_score_multiclass_poly(self):
|
||||||
|
"""Test score for multiclass classification with poly kernel."""
|
||||||
X, y = load_dataset(
|
X, y = load_dataset(
|
||||||
random_state=self._random_state,
|
random_state=self._random_state,
|
||||||
n_classes=3,
|
n_classes=3,
|
||||||
@@ -484,6 +489,7 @@ class Stree_test(unittest.TestCase):
|
|||||||
self.assertEqual(1.0, clf2.fit(X, y).score(X, y))
|
self.assertEqual(1.0, clf2.fit(X, y).score(X, y))
|
||||||
|
|
||||||
def test_score_multiclass_liblinear(self):
|
def test_score_multiclass_liblinear(self):
|
||||||
|
"""Test score for multiclass classification with liblinear kernel."""
|
||||||
X, y = load_dataset(
|
X, y = load_dataset(
|
||||||
random_state=self._random_state,
|
random_state=self._random_state,
|
||||||
n_classes=3,
|
n_classes=3,
|
||||||
@@ -509,6 +515,7 @@ class Stree_test(unittest.TestCase):
|
|||||||
self.assertEqual(1.0, clf2.fit(X, y).score(X, y))
|
self.assertEqual(1.0, clf2.fit(X, y).score(X, y))
|
||||||
|
|
||||||
def test_score_multiclass_sigmoid(self):
|
def test_score_multiclass_sigmoid(self):
|
||||||
|
"""Test score for multiclass classification with sigmoid kernel."""
|
||||||
X, y = load_dataset(
|
X, y = load_dataset(
|
||||||
random_state=self._random_state,
|
random_state=self._random_state,
|
||||||
n_classes=3,
|
n_classes=3,
|
||||||
@@ -529,6 +536,7 @@ class Stree_test(unittest.TestCase):
|
|||||||
self.assertEqual(0.9662921348314607, clf2.fit(X, y).score(X, y))
|
self.assertEqual(0.9662921348314607, clf2.fit(X, y).score(X, y))
|
||||||
|
|
||||||
def test_score_multiclass_linear(self):
|
def test_score_multiclass_linear(self):
|
||||||
|
"""Test score for multiclass classification with linear kernel."""
|
||||||
warnings.filterwarnings("ignore", category=ConvergenceWarning)
|
warnings.filterwarnings("ignore", category=ConvergenceWarning)
|
||||||
warnings.filterwarnings("ignore", category=RuntimeWarning)
|
warnings.filterwarnings("ignore", category=RuntimeWarning)
|
||||||
X, y = load_dataset(
|
X, y = load_dataset(
|
||||||
@@ -556,11 +564,13 @@ class Stree_test(unittest.TestCase):
|
|||||||
self.assertEqual(1.0, clf2.fit(X, y).score(X, y))
|
self.assertEqual(1.0, clf2.fit(X, y).score(X, y))
|
||||||
|
|
||||||
def test_zero_all_sample_weights(self):
|
def test_zero_all_sample_weights(self):
|
||||||
|
"""Test exception raises when all sample weights are zero."""
|
||||||
X, y = load_dataset(self._random_state)
|
X, y = load_dataset(self._random_state)
|
||||||
with self.assertRaises(ValueError):
|
with self.assertRaises(ValueError):
|
||||||
Stree().fit(X, y, np.zeros(len(y)))
|
Stree().fit(X, y, np.zeros(len(y)))
|
||||||
|
|
||||||
def test_mask_samples_weighted_zero(self):
|
def test_mask_samples_weighted_zero(self):
|
||||||
|
"""Check that the weighted zero samples are masked."""
|
||||||
X = np.array(
|
X = np.array(
|
||||||
[
|
[
|
||||||
[1, 1],
|
[1, 1],
|
||||||
@@ -588,6 +598,7 @@ class Stree_test(unittest.TestCase):
|
|||||||
self.assertEqual(model2.score(X, y, w), 1)
|
self.assertEqual(model2.score(X, y, w), 1)
|
||||||
|
|
||||||
def test_depth(self):
|
def test_depth(self):
|
||||||
|
"""Check depth of the tree."""
|
||||||
X, y = load_dataset(
|
X, y = load_dataset(
|
||||||
random_state=self._random_state,
|
random_state=self._random_state,
|
||||||
n_classes=3,
|
n_classes=3,
|
||||||
@@ -603,6 +614,7 @@ class Stree_test(unittest.TestCase):
|
|||||||
self.assertEqual(4, clf.depth_)
|
self.assertEqual(4, clf.depth_)
|
||||||
|
|
||||||
def test_nodes_leaves(self):
|
def test_nodes_leaves(self):
|
||||||
|
"""Check number of nodes and leaves."""
|
||||||
X, y = load_dataset(
|
X, y = load_dataset(
|
||||||
random_state=self._random_state,
|
random_state=self._random_state,
|
||||||
n_classes=3,
|
n_classes=3,
|
||||||
@@ -622,6 +634,7 @@ class Stree_test(unittest.TestCase):
|
|||||||
self.assertEqual(6, leaves)
|
self.assertEqual(6, leaves)
|
||||||
|
|
||||||
def test_nodes_leaves_artificial(self):
|
def test_nodes_leaves_artificial(self):
|
||||||
|
"""Check leaves of artificial dataset."""
|
||||||
n1 = Snode(None, [1, 2, 3, 4], [1, 0, 1, 1], [], 0.0, "test1")
|
n1 = Snode(None, [1, 2, 3, 4], [1, 0, 1, 1], [], 0.0, "test1")
|
||||||
n2 = Snode(None, [1, 2, 3, 4], [1, 0, 1, 1], [], 0.0, "test2")
|
n2 = Snode(None, [1, 2, 3, 4], [1, 0, 1, 1], [], 0.0, "test2")
|
||||||
n3 = Snode(None, [1, 2, 3, 4], [1, 0, 1, 1], [], 0.0, "test3")
|
n3 = Snode(None, [1, 2, 3, 4], [1, 0, 1, 1], [], 0.0, "test3")
|
||||||
@@ -640,12 +653,14 @@ class Stree_test(unittest.TestCase):
|
|||||||
self.assertEqual(2, leaves)
|
self.assertEqual(2, leaves)
|
||||||
|
|
||||||
def test_bogus_multiclass_strategy(self):
|
def test_bogus_multiclass_strategy(self):
|
||||||
|
"""Check invalid multiclass strategy."""
|
||||||
clf = Stree(multiclass_strategy="other")
|
clf = Stree(multiclass_strategy="other")
|
||||||
X, y = load_wine(return_X_y=True)
|
X, y = load_wine(return_X_y=True)
|
||||||
with self.assertRaises(ValueError):
|
with self.assertRaises(ValueError):
|
||||||
clf.fit(X, y)
|
clf.fit(X, y)
|
||||||
|
|
||||||
def test_multiclass_strategy(self):
|
def test_multiclass_strategy(self):
|
||||||
|
"""Check multiclass strategy."""
|
||||||
X, y = load_wine(return_X_y=True)
|
X, y = load_wine(return_X_y=True)
|
||||||
clf_o = Stree(multiclass_strategy="ovo")
|
clf_o = Stree(multiclass_strategy="ovo")
|
||||||
clf_r = Stree(multiclass_strategy="ovr")
|
clf_r = Stree(multiclass_strategy="ovr")
|
||||||
@@ -655,6 +670,7 @@ class Stree_test(unittest.TestCase):
|
|||||||
self.assertEqual(0.9269662921348315, score_r)
|
self.assertEqual(0.9269662921348315, score_r)
|
||||||
|
|
||||||
def test_incompatible_hyperparameters(self):
|
def test_incompatible_hyperparameters(self):
|
||||||
|
"""Check incompatible hyperparameters."""
|
||||||
X, y = load_wine(return_X_y=True)
|
X, y = load_wine(return_X_y=True)
|
||||||
clf = Stree(kernel="liblinear", multiclass_strategy="ovo")
|
clf = Stree(kernel="liblinear", multiclass_strategy="ovo")
|
||||||
with self.assertRaises(ValueError):
|
with self.assertRaises(ValueError):
|
||||||
@@ -664,12 +680,15 @@ class Stree_test(unittest.TestCase):
|
|||||||
clf.fit(X, y)
|
clf.fit(X, y)
|
||||||
|
|
||||||
def test_version(self):
|
def test_version(self):
|
||||||
|
"""Check STree version."""
|
||||||
clf = Stree()
|
clf = Stree()
|
||||||
self.assertEqual(__version__, clf.version())
|
self.assertEqual(__version__, clf.version())
|
||||||
|
|
||||||
def test_graph(self):
|
def test_graph(self):
|
||||||
|
"""Check graphviz representation of the tree."""
|
||||||
X, y = load_wine(return_X_y=True)
|
X, y = load_wine(return_X_y=True)
|
||||||
clf = Stree(random_state=self._random_state)
|
clf = Stree(random_state=self._random_state)
|
||||||
|
self.assertEqual(clf.graph(), "digraph STree {\n}\n")
|
||||||
clf.fit(X, y)
|
clf.fit(X, y)
|
||||||
expected_head = "digraph STree {\n"
|
expected_head = "digraph STree {\n"
|
||||||
expected_tail = (
|
expected_tail = (
|
||||||
|
Reference in New Issue
Block a user