-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
193 lines (153 loc) · 6.53 KB
/
test.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import unittest
import numpy as np
import numpy.random as rng
import pandas as pd
from numpy.typing import ArrayLike
class Test(unittest.TestCase):
@staticmethod
def add_random_missing_values(X: ArrayLike, max_rate: float = 0.7) -> ArrayLike:
rates = rng.uniform(0.0, max_rate, X.shape[1])
n_samples = np.round(X.shape[0] * rates).astype("int")
for i in range(X.shape[1]):
missing_ix = rng.choice(np.arange(X.shape[0]), n_samples[i])
X[missing_ix, i] = np.nan
return X
def test_binary_classification(self):
from sklearn.pipeline import Pipeline
from sklearn.linear_model import LogisticRegression
from sklearn.impute import SimpleImputer
from sklearn.datasets import make_classification
from crlearn.feature_selection import DropColinCV, DropByMissingRateCV
from crlearn.evaluation import crossvalidate_classification
X, y = make_classification(
n_samples=1000, n_features=100, n_redundant=25, n_classes=2
)
X = self.add_random_missing_values(X)
model = Pipeline(
[
("missing_filter", DropByMissingRateCV()),
("impiter", SimpleImputer()),
("linear_filter", DropColinCV()),
("classifier", LogisticRegression(random_state=0)),
]
)
_ = crossvalidate_classification(model, X, y, name="binary_classification_test")
def test_mutliclass_classification(self):
from sklearn.pipeline import Pipeline
from sklearn.linear_model import LogisticRegression
from sklearn.impute import SimpleImputer
from sklearn.datasets import make_classification
from crlearn.feature_selection import DropColinCV, DropByMissingRateCV
from crlearn.evaluation import crossvalidate_classification
X, y = make_classification(
n_samples=1000,
n_features=100,
n_redundant=25,
n_classes=10,
n_informative=50,
n_clusters_per_class=2,
)
X = self.add_random_missing_values(X)
model = Pipeline(
[
("missing_filter", DropByMissingRateCV()),
("impiter", SimpleImputer()),
("linear_filter", DropColinCV()),
("classifier", LogisticRegression(random_state=0)),
]
)
_ = crossvalidate_classification(
model, X, y, name="multiclass_classification_test"
)
def test_nested_mutliclass_classification(self):
from sklearn.pipeline import Pipeline
from sklearn.linear_model import LogisticRegression
from sklearn.impute import SimpleImputer
from sklearn.datasets import make_classification
from sklearn.model_selection import RandomizedSearchCV
from crlearn.feature_selection import DropColinCV, DropByMissingRateCV
from crlearn.evaluation import crossvalidate_classification
X, y = make_classification(
n_samples=1000,
n_features=100,
n_redundant=25,
n_classes=10,
n_informative=50,
n_clusters_per_class=2,
)
X = self.add_random_missing_values(X)
model = Pipeline(
[
("missing_filter", DropByMissingRateCV()),
("impiter", SimpleImputer()),
("linear_filter", DropColinCV()),
("classifier", LogisticRegression(random_state=0)),
]
)
param_grid = {"classifier__C": [0.001, 0.01, 0.1, 1]}
optimizer = RandomizedSearchCV(model, param_grid, n_iter=4)
_ = crossvalidate_classification(
optimizer, X, y, name="nested_multiclass_classification_test"
)
def test_nested_regression(self):
from sklearn.pipeline import Pipeline
from sklearn.linear_model import Ridge
from sklearn.impute import SimpleImputer
from sklearn.datasets import make_regression
from sklearn.model_selection import RandomizedSearchCV
from crlearn.feature_selection import DropColinCV, DropByMissingRateCV
from crlearn.evaluation import crossvalidate_regression
X, y = make_regression(
n_samples=1000,
n_features=100,
n_informative=50,
)
X = self.add_random_missing_values(X)
model = Pipeline(
[
("missing_filter", DropByMissingRateCV()),
("impiter", SimpleImputer()),
("linear_filter", DropColinCV()),
("regressor", Ridge(random_state=0)),
]
)
param_grid = {"regressor__alpha": [0.001, 0.01, 0.1, 1]}
optimizer = RandomizedSearchCV(model, param_grid, n_iter=4)
_ = crossvalidate_regression(optimizer, X, y, name="nested_regression_test")
def test_nested_regression_with_dataframes(self):
from sklearn.pipeline import Pipeline
from sklearn.linear_model import Ridge
from sklearn.impute import SimpleImputer
from sklearn.datasets import make_regression
from sklearn.model_selection import RandomizedSearchCV
from crlearn.feature_selection import DropColinCV, DropByMissingRateCV
from crlearn.evaluation import crossvalidate_regression
X, y = make_regression(
n_samples=1000,
n_features=100,
n_informative=50,
)
X = pd.DataFrame(self.add_random_missing_values(X))
y=pd.Series(y)
model = Pipeline(
[
("missing_filter", DropByMissingRateCV()),
("impiter", SimpleImputer()),
("linear_filter", DropColinCV()),
("regressor", Ridge(random_state=0)),
]
)
param_grid = {"regressor__alpha": [0.001, 0.01, 0.1, 1]}
optimizer = RandomizedSearchCV(model, param_grid, n_iter=4)
_ = crossvalidate_regression(optimizer, X, y, name="nested_regression_with_dataframes_test")
def test_compound_regression(self):
from sklearn.datasets import make_regression
from sklearn.linear_model import TweedieRegressor
from crlearn.evaluation import CONFIG, crossvalidation
import numpy as np
X,y=make_regression(n_samples=1000,n_features=100)
y=np.abs(np.ceil(y))+1
model=TweedieRegressor()
_,_,_=crossvalidation(CONFIG["COMPOUND_REGRESSION"],CONFIG["MAPPINGS"],model,X,y,name="compound_regression_test")
if __name__ == "__main__":
unittest.main()