-
-
Notifications
You must be signed in to change notification settings - Fork 262
/
Copy pathtest_blockwise.py
284 lines (235 loc) · 9.56 KB
/
test_blockwise.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import dask
import dask.array as da
import dask.dataframe as dd
import numpy as np
import pytest
import sklearn.linear_model
import dask_ml.datasets
import dask_ml.ensemble
class TestBlockwiseVotingClassifier:
def test_hard_voting_array(self):
X, y = dask_ml.datasets.make_classification(chunks=25)
clf = dask_ml.ensemble.BlockwiseVotingClassifier(
sklearn.linear_model.LogisticRegression(solver="lbfgs"),
classes=[0, 1],
)
clf.fit(X, y)
assert len(clf.estimators_) == 4
X2, y2 = dask_ml.datasets.make_classification(chunks=20)
result = clf.predict(X2)
assert isinstance(result, da.Array)
assert result.dtype == np.dtype("int64")
assert result.shape == (len(y),)
assert result.numblocks == y2.numblocks
result_ = result.compute()
assert result_.dtype == result.dtype
assert result_.shape == result.shape
with pytest.raises(AttributeError, match="hard"):
clf.predict_proba
score = clf.score(X2, y2)
assert isinstance(score, float)
# ndarray
X3, y3 = dask.compute(X2, y2)
result2 = clf.predict(X3)
assert isinstance(result2, np.ndarray)
da.utils.assert_eq(result, result2)
score2 = clf.score(X3, y3)
assert score == score2
_, y4 = dask_ml.datasets.make_classification(chunks=20)
with pytest.raises(ValueError, match="4 != 5"):
clf.fit(X, y4)
def test_bad_chunking_raises(self):
X = da.ones((10, 5), chunks=3)
y = da.ones(10, chunks=3)
clf = dask_ml.ensemble.BlockwiseVotingClassifier(
sklearn.linear_model.LogisticRegression(solver="lbfgs"),
classes=[0, 1],
)
with pytest.raises(TypeError):
# this should *really* be a ValueError...
clf.fit(X, y)
def test_hard_voting_frame(self):
X, y = dask_ml.datasets.make_classification(chunks=25)
X = dd.from_dask_array(X)
y = dd.from_dask_array(y)
clf = dask_ml.ensemble.BlockwiseVotingClassifier(
sklearn.linear_model.LogisticRegression(solver="lbfgs"),
classes=[0, 1],
)
clf.fit(X, y)
assert len(clf.estimators_) == 4
X2, y2 = dask_ml.datasets.make_classification(chunks=20)
X2 = dd.from_dask_array(X2)
y2 = dd.from_dask_array(y2)
result = clf.predict(X2)
assert isinstance(result, da.Array) # TODO(pandas-IO)
assert result.dtype == np.dtype("int64")
assert len(result.shape) == 1 and np.isnan(result.shape[0])
assert result.numblocks == (y2.npartitions,)
result_ = result.compute()
assert result_.dtype == result.dtype
assert result_.shape == (len(y2),)
with pytest.raises(AttributeError, match="hard"):
clf.predict_proba
score = clf.score(X2, y2)
assert isinstance(score, float)
# ndarray
X3, y3 = dask.compute(X2, y2)
result2 = clf.predict(X3)
assert isinstance(result2, np.ndarray)
da.utils.assert_eq(result, result2)
# TODO: accuracy score raising for pandas.
# score2 = clf.score(X3, y3)
# assert score == score2
def test_soft_voting_array(self):
X, y = dask_ml.datasets.make_classification(chunks=25)
clf = dask_ml.ensemble.BlockwiseVotingClassifier(
sklearn.linear_model.LogisticRegression(solver="lbfgs"),
voting="soft",
classes=[0, 1],
)
clf.fit(X, y)
assert len(clf.estimators_) == 4
result = clf.predict(X)
assert isinstance(result, da.Array)
assert result.dtype == np.dtype("int64")
assert result.shape == (len(X),)
result_ = result.compute()
assert result_.dtype == result.dtype
assert result_.shape == result.shape
result = clf.predict_proba(X)
assert result.dtype == np.dtype("float64")
assert result.shape == (len(X), 2) # 2 classes
assert result.numblocks == (4, 1)
score = clf.score(X, y)
assert isinstance(score, float)
@pytest.mark.xfail(
True,
reason="AssertionError da.utils.assert_eq(result, result2)",
)
def test_soft_voting_frame(self):
X, y = dask_ml.datasets.make_classification(chunks=25)
X = dd.from_dask_array(X)
y = dd.from_dask_array(y)
clf = dask_ml.ensemble.BlockwiseVotingClassifier(
sklearn.linear_model.LogisticRegression(solver="lbfgs"),
voting="soft",
classes=[0, 1],
)
clf.fit(X, y)
assert len(clf.estimators_) == 4
X2, y2 = dask_ml.datasets.make_classification(chunks=20)
X2 = dd.from_dask_array(X2)
y2 = dd.from_dask_array(y2)
result = clf.predict(X2)
assert isinstance(result, da.Array) # TODO(pandas-IO)
assert result.dtype == np.dtype("int64")
assert len(result.shape) == 1 and np.isnan(result.shape[0])
assert result.numblocks == (y2.npartitions,)
result_ = result.compute()
assert result_.dtype == result.dtype
assert result_.shape == (len(y2),)
result = clf.predict_proba(X2)
assert result.dtype == np.dtype("float64")
assert len(result.shape) == 2
assert np.isnan(result.shape[0])
assert result.shape[1] == 2
assert result.numblocks == (5, 1)
score = clf.score(X, y)
assert isinstance(score, float)
# ndarray
X3, y3 = dask.compute(X2, y2)
result2 = clf.predict_proba(X3)
assert isinstance(result2, np.ndarray)
da.utils.assert_eq(result, result2)
# TODO: accuracy score raising for pandas.
# score2 = clf.score(X3, y3)
# assert score == score2
def test_no_classes_raises(self):
X, y = dask_ml.datasets.make_classification(chunks=25)
clf = dask_ml.ensemble.BlockwiseVotingClassifier(
sklearn.linear_model.LogisticRegression(solver="lbfgs"),
)
with pytest.raises(ValueError, match="classes"):
clf.fit(X, y)
class TestBlockwiseVotingRegressor:
def test_no_unnecessary_computation_in_fit(self, monkeypatch):
X, y = dask_ml.datasets.make_regression(n_features=20, chunks=25)
compute_called = False
original_compute = X.compute
def spy_compute(*args, **kwargs):
nonlocal compute_called
compute_called = True
return original_compute(*args, **kwargs)
monkeypatch.setattr(X, "compute", spy_compute)
est = dask_ml.ensemble.BlockwiseVotingRegressor(
sklearn.linear_model.LinearRegression(),
)
est.fit(X, y)
# Ensure that X.compute() was never invoked during fitting.
assert compute_called is False
# Verify that _n_samples was set using lazy metadata.
assert est._n_samples == X.shape[0]
def test_fit_array(self):
X, y = dask_ml.datasets.make_regression(n_features=20, chunks=25)
est = dask_ml.ensemble.BlockwiseVotingRegressor(
sklearn.linear_model.LinearRegression(),
)
est.fit(X, y)
assert len(est.estimators_) == 4
X2, y2 = dask_ml.datasets.make_regression(n_features=20, chunks=20)
result = est.predict(X2)
assert result.dtype == np.dtype("float64")
assert result.shape == y2.shape
assert result.numblocks == y2.numblocks
score = est.score(X2, y2)
assert isinstance(score, float)
# ndarray
X3, y3 = dask.compute(X2, y2)
result2 = est.predict(X3)
assert isinstance(result2, np.ndarray)
da.utils.assert_eq(result, result2)
# TODO: r2_score raising for ndarray
# score2 = est.score(X3, y3)
# assert score == score2
def test_fit_frame(self):
X, y = dask_ml.datasets.make_regression(n_features=20, chunks=25)
X = dd.from_dask_array(X)
y = dd.from_dask_array(y)
est = dask_ml.ensemble.BlockwiseVotingRegressor(
sklearn.linear_model.LinearRegression(),
)
est.fit(X, y)
assert len(est.estimators_) == 4
X2, y2 = dask_ml.datasets.make_regression(n_features=20, chunks=20)
result = est.predict(X2)
assert result.dtype == np.dtype("float64")
assert result.shape == y2.shape
assert result.numblocks == y2.numblocks
score = est.score(X2, y2)
assert isinstance(score, float)
# ndarray
X3, y3 = dask.compute(X2, y2)
result2 = est.predict(X3)
assert isinstance(result2, np.ndarray)
da.utils.assert_eq(result, result2)
# TODO: r2_score raising for ndarray
# score2 = est.score(X3, y3)
# assert score == score2
def test_predict_with_different_chunks(self):
X, y = dask_ml.datasets.make_regression(n_features=20, chunks=25)
est = dask_ml.ensemble.BlockwiseVotingRegressor(
sklearn.linear_model.LinearRegression(),
)
est.fit(X, y)
X_test, y_test = dask_ml.datasets.make_regression(n_features=20, chunks=20)
result = est.predict(X_test)
assert result.dtype == np.dtype("float64")
assert result.shape == y_test.shape
# Prediction is rechunked to have one block per estimator.
assert result.numblocks[0] == len(est.estimators_)
score = est.score(X_test, y_test)
assert isinstance(score, float)
X_test_np, y_test_np = dask.compute(X_test, y_test)
result_np = est.predict(X_test_np)
da.utils.assert_eq(result, result_np)