-
-
Notifications
You must be signed in to change notification settings - Fork 554
/
test_dispersion.py
149 lines (122 loc) · 4.25 KB
/
test_dispersion.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# tests.test_contrib.test_missing.test_dispersion
# Tests for the alpha selection visualizations.
#
# Author: Nathan Danielsen <nathan.danielsen@gmail.com>
# Created: Thu Mar 29 12:13:04 2018 -0500
#
# Copyright (C) 2018 The scikit-yb developers
# For license information, see LICENSE.txt
#
# ID: test_dispersion.py [1443e16] ndanielsen@users.noreply.github.com $
"""
Tests for the MissingValuesDispersion visualizations.
"""
##########################################################################
## Imports
##########################################################################
import os
import pytest
from sklearn.datasets import make_classification
from tests.base import VisualTestCase
from yellowbrick.contrib.missing.dispersion import *
try:
import pandas as pd
except ImportError:
pd = None
@pytest.fixture(scope="class")
def missing_dispersion_tolerance(request):
request.cls.tol = 0.5 if os.name == "nt" else 0.01
##########################################################################
## Feature Importances Tests
##########################################################################
@pytest.mark.usefixtures("missing_dispersion_tolerance")
class TestMissingValuesDispersion(VisualTestCase):
"""
MissingValuesDispersion visualizer
"""
@pytest.mark.skipif(pd is None, reason="pandas is required")
def test_missingvaluesdispersion_with_pandas(self):
"""
Integration test of visualizer with pandas
"""
X, y = make_classification(
n_samples=400,
n_features=20,
n_informative=8,
n_redundant=8,
n_classes=2,
n_clusters_per_class=4,
random_state=854,
)
# add nan values to a range of values in the matrix
X[X > 1.5] = np.nan
X_ = pd.DataFrame(X)
features = [str(n) for n in range(20)]
viz = MissingValuesDispersion(features=features)
viz.fit(X_)
viz.finalize()
self.assert_images_similar(viz, tol=self.tol)
@pytest.mark.skipif(pd is None, reason="pandas is required")
def test_missingvaluesdispersion_with_pandas_with_y_targets(self):
"""
Integration test of visualizer with pandas with y targets
"""
X, y = make_classification(
n_samples=400,
n_features=20,
n_informative=8,
n_redundant=8,
n_classes=2,
n_clusters_per_class=4,
random_state=854,
)
# add nan values to a range of values in the matrix
X[X > 1.5] = np.nan
X_ = pd.DataFrame(X)
features = [str(n) for n in range(20)]
classes = ["Class A", "Class B"]
viz = MissingValuesDispersion(features=features, classes=classes)
viz.fit(X_, y=y)
viz.finalize()
self.assert_images_similar(viz, tol=self.tol)
def test_missingvaluesdispersion_with_numpy(self):
"""
Integration test of visualizer with numpy
"""
X, y = make_classification(
n_samples=400,
n_features=20,
n_informative=8,
n_redundant=8,
n_classes=2,
n_clusters_per_class=4,
random_state=852,
)
# add nan values to a range of values in the matrix
X[X > 1.5] = np.nan
features = [str(n) for n in range(20)]
viz = MissingValuesDispersion(features=features)
viz.fit(X)
viz.finalize()
self.assert_images_similar(viz, tol=self.tol)
def test_missingvaluesdispersion_with_numpy_with_y_targets(self):
"""
Integration test of visualizer with numpy with y targets
"""
X, y = make_classification(
n_samples=400,
n_features=20,
n_informative=8,
n_redundant=8,
n_classes=2,
n_clusters_per_class=4,
random_state=852,
)
# add nan values to a range of values in the matrix
X[X > 1.5] = np.nan
features = [str(n) for n in range(20)]
classes = ["Class A", "Class B"]
viz = MissingValuesDispersion(features=features, classes=classes)
viz.fit(X, y=y)
viz.finalize()
self.assert_images_similar(viz, tol=self.tol)