forked from AxeldeRomblay/MLBox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_drift_threshold.py
128 lines (110 loc) · 4.92 KB
/
test_drift_threshold.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
# !/usr/bin/env python
# coding: utf-8
# Author: Axel ARONIO DE ROMBLAY <[email protected]>
# Author: Henri GERARD <[email protected]>
# License: BSD 3 clause
"""Test mlbox.preprocessing.drift module."""
import pytest
import pandas as pd
from mlbox.preprocessing.drift import DriftThreshold
from mlbox.preprocessing.drift import sync_fit
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
def test_init_drift_threshold():
"""Test init method of DriftThreshold class."""
drift_threshold = DriftThreshold()
assert drift_threshold.threshold == 0.6
assert drift_threshold.subsample == 1.
assert isinstance(drift_threshold.estimator,
type(DecisionTreeClassifier()))
assert drift_threshold.n_folds == 2
assert drift_threshold.stratify
assert drift_threshold.random_state == 1
assert drift_threshold.n_jobs == -1
assert not drift_threshold._DriftThreshold__fitOK
def test_get_params_drift_threshold():
"""Test get_params method of DriftThreshold class."""
drift_threshold = DriftThreshold()
dict = {'threshold': 0.6,
'subsample': 1.,
'n_folds': 2,
'stratify': True,
'random_state': 1,
'n_jobs': -1}
dict_get_params = drift_threshold.get_params()
assert dict_get_params["threshold"] == dict["threshold"]
assert dict_get_params["subsample"] == dict["subsample"]
assert dict_get_params["n_folds"] == dict["n_folds"]
assert dict_get_params["stratify"] == dict["stratify"]
assert dict_get_params["random_state"] == dict["random_state"]
assert dict_get_params["n_jobs"] == dict["n_jobs"]
def test_set_params_drift_threshold():
"""Test set_params method of DriftThreshold class."""
drift_threshold = DriftThreshold()
dict = {'threshold': 0.6,
'subsample': 1.,
'estimator': DecisionTreeClassifier(max_depth=6),
'n_folds': 2,
'stratify': True,
'random_state': 1,
'n_jobs': -1}
drift_threshold.set_params(**dict)
dict_get_params = drift_threshold.get_params()
assert dict_get_params["threshold"] == dict["threshold"]
assert dict_get_params["subsample"] == dict["subsample"]
assert dict_get_params["n_folds"] == dict["n_folds"]
assert dict_get_params["stratify"] == dict["stratify"]
assert dict_get_params["random_state"] == dict["random_state"]
assert dict_get_params["n_jobs"] == dict["n_jobs"]
def test_fit_drift_threshold():
"""Test fit method of DriftThreshold class."""
df_train = pd.read_csv("data_for_tests/clean_train.csv")
df_test = pd.read_csv("data_for_tests/clean_test.csv")
drift_threshold = DriftThreshold()
drift_threshold.fit(df_train, df_test)
assert drift_threshold._DriftThreshold__fitOK
def test_transform_drift_threshold():
"""Test transform method of DriftThreshold class."""
df_train = pd.read_csv("data_for_tests/clean_train.csv")
df_test = pd.read_csv("data_for_tests/clean_test.csv")
drift_threshold = DriftThreshold()
with pytest.raises(ValueError):
drift_threshold.transform(df_train)
drift_threshold.fit(df_train, df_test)
df_transformed = drift_threshold.transform(df_train)
assert (df_train.columns == df_transformed.columns).all()
def test_get_support_drift_threshold():
"""Test get_support method of DriftThreshold class."""
df_train = pd.read_csv("data_for_tests/clean_train.csv")
df_test = pd.read_csv("data_for_tests/clean_test.csv")
drift_threshold = DriftThreshold()
with pytest.raises(ValueError):
drift_threshold.get_support()
drift_threshold.fit(df_train, df_test)
keep_list = drift_threshold.get_support()
drop_list = drift_threshold.get_support(complement=True)
for name in ['Age', 'Fare', 'Parch', 'Pclass', 'SibSp']:
assert (name in keep_list)
assert not drop_list
def test_drifts_drift_threshold():
"""Test drifts method of DriftThreshold class."""
df_train = pd.read_csv("data_for_tests/clean_train.csv")
df_test = pd.read_csv("data_for_tests/clean_test.csv")
drift_threshold = DriftThreshold()
with pytest.raises(ValueError):
drift_threshold.drifts()
drift_threshold.fit(df_train, df_test)
drifts = drift_threshold.drifts()
for name in ['Age', 'Fare', 'Parch', 'Pclass', 'SibSp']:
assert (name in list(drifts.keys()))
def test_sync_fit_drift_threshold():
"""Test method sync_fit of drift_threshold module."""
df_train = pd.read_csv("data_for_tests/clean_train.csv")
df_test = pd.read_csv("data_for_tests/clean_test.csv")
estimator = RandomForestClassifier(n_estimators=50,
n_jobs=-1,
max_features=1.,
min_samples_leaf=5,
max_depth=5)
score = sync_fit(df_train, df_test, estimator)
assert 0 <= score