forked from ibis-project/ibis-ml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_handle_outliers.py
109 lines (103 loc) · 2.9 KB
/
test_handle_outliers.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
import ibis
import numpy as np
import pandas as pd
import pandas.testing as tm
import pytest
import ibis_ml as ml
@pytest.mark.parametrize(
("deviation_factor", "method", "treatment", "cols", "test_table", "expected"),
[
(
2,
"z-score",
"capping",
"int_col",
{"int_col": [None, 0, -1, 1]},
{"int_col": [None, 0, 0, 0]},
),
(
2,
"IQR",
"capping",
"int_col",
{"int_col": [None, 0, -1, 1]},
{"int_col": [None, 0, 0, 0]},
),
(
3.0,
"z-score",
"trimming",
"int_col",
{"int_col": [None, 0, -1, 1]},
{"int_col": [None, 0]},
),
(
3.0,
"IQR",
"trimming",
"int_col",
{"int_col": [None, 0, -1, 1]},
{"int_col": [None, 0]},
),
(
2,
"z-score",
"capping",
"floating_col",
{"floating_col": [None, 0, -1, 1, np.nan]},
{"floating_col": [None, 0.0, 0.0, 0.0, np.nan]},
),
(
2,
"z-score",
"trimming",
"floating_col",
{"floating_col": [None, 0, -1, 1, np.nan]},
{"floating_col": [None, np.nan, 0.0]},
),
(
2,
"z-score",
"trimming",
["floating_col", "int_col"],
{
"floating_col": [None, 0, -1, 1, np.nan],
"int_col": [None, 0, 0, None, None],
},
{"floating_col": [None, np.nan, 0.0], "int_col": [None, None, 0]},
),
(
2,
"z-score",
"capping",
["floating_col", "int_col"],
{
"floating_col": [None, 0, -1, 1, np.nan],
"int_col": [None, 0, 0, None, None],
},
{
"floating_col": [None, 0, 0, 0, np.nan],
"int_col": [None, 0, 0, None, None],
},
),
],
)
def test_handle_univariate_outliers(
deviation_factor, method, treatment, cols, test_table, expected
):
train_table = ibis.memtable(
{
# use same value for easier calculation statistics
"int_col": [0] * 10, # mean = 0, std = 0 Q1 = 0, Q3 = 0
"floating_col": [0.0] * 10, # mean = 0, std = 0, Q1 = 0, Q3 = 0
}
)
test_table = ibis.memtable(test_table)
step = ml.HandleUnivariateOutliers(
cols, method=method, deviation_factor=deviation_factor, treatment=treatment
)
step.fit_table(train_table, ml.core.Metadata())
assert step.is_fitted()
result = step.transform_table(test_table)
expected = pd.DataFrame(expected)
tm.assert_frame_equal(result.execute(), expected, check_dtype=False)