4
4
5
5
import copy
6
6
import numpy as np
7
- from sklearn .linear_model import LinearRegression , LogisticRegression
7
+ from sklearn .linear_model import LinearRegression , LogisticRegression , Ridge
8
+ from sklearn .metrics import r2_score , accuracy_score
8
9
import tensorflow as tf
9
10
10
11
from adapt .instance_based import (TrAdaBoost ,
@@ -34,6 +35,8 @@ def test_tradaboost_fit():
34
35
solver = 'lbfgs' ),
35
36
n_estimators = 20 )
36
37
model .fit (Xs , ys_classif , Xt = Xt [:10 ], yt = yt_classif [:10 ])
38
+ score = model .score (Xs , ys_classif )
39
+ assert score == accuracy_score (ys_classif , model .predict (Xs ))
37
40
assert len (model .sample_weights_src_ [0 ]) == 100
38
41
assert (model .sample_weights_src_ [0 ][:50 ].sum () ==
39
42
model .sample_weights_src_ [0 ][50 :].sum ())
@@ -58,13 +61,18 @@ def test_tradaboost_fit_keras_model():
58
61
model .fit (Xs , np .random .random ((100 , 2 )),
59
62
Xt = Xt [:10 ], yt = np .random .random ((10 , 2 )))
60
63
64
+ score = model .score (Xs , ys_classif )
65
+ assert score == accuracy_score (ys_classif , model .predict (Xs ))
66
+
61
67
62
68
def test_tradaboostr2_fit ():
63
69
np .random .seed (0 )
64
70
model = TrAdaBoostR2 (LinearRegression (fit_intercept = False ),
65
71
n_estimators = 100 ,
66
72
Xt = Xt [:10 ], yt = yt_reg [:10 ])
67
73
model .fit (Xs , ys_reg )
74
+ score = model .score (Xs , ys_reg )
75
+ assert score == r2_score (ys_reg , model .predict (Xs ))
68
76
assert np .abs (model .estimators_ [- 1 ].coef_ [0 ] - 1. ) < 1
69
77
assert np .abs (model .sample_weights_src_ [- 1 ][:50 ].sum () /
70
78
model .sample_weights_src_ [- 1 ][50 :].sum ()) > 10
@@ -80,7 +88,9 @@ def test_twostagetradaboostr2_fit():
80
88
np .random .seed (0 )
81
89
model = TwoStageTrAdaBoostR2 (LinearRegression (fit_intercept = False ),
82
90
n_estimators = 10 )
83
- model .fit (Xs , ys_reg .ravel (), Xt = Xt [:10 ], yt = yt_reg [:10 ].ravel ())
91
+ model .fit (Xs , ys_reg .ravel (), Xt = Xt [:10 ], yt = yt_reg [:10 ].ravel ())
92
+ score = model .score (Xs , ys_reg )
93
+ assert score == r2_score (ys_reg , model .predict (Xs ))
84
94
assert np .abs (model .estimators_ [- 1 ].estimators_ [- 1 ].coef_ [0 ]
85
95
- 1. ) < 1
86
96
assert np .abs (model .sample_weights_src_ [- 1 ][:50 ].sum () /
@@ -103,3 +113,75 @@ def test_tradaboost_deepcopy():
103
113
copy_model = copy .deepcopy (model )
104
114
assert np .all (model .predict (Xt ) == copy_model .predict (Xt ))
105
115
assert hex (id (model )) != hex (id (copy_model ))
116
+
117
+
118
+ def test_tradaboost_multiclass ():
119
+ np .random .seed (0 )
120
+ X = np .random .randn (10 , 3 )
121
+ y = np .random .choice (3 , 10 )
122
+ model = TrAdaBoost (LogisticRegression (penalty = 'none' ,
123
+ solver = 'lbfgs' ), Xt = X , yt = y ,
124
+ n_estimators = 20 )
125
+ model .fit (X , y )
126
+ yp = model .predict (X )
127
+ score = model .score (X , y )
128
+ assert set (np .unique (yp )) == set ([0 ,1 ,2 ])
129
+ assert score == accuracy_score (y , yp )
130
+
131
+
132
+ def test_tradaboost_multireg ():
133
+ np .random .seed (0 )
134
+ X = np .random .randn (10 , 3 )
135
+ y = np .random .randn (10 , 5 )
136
+ model = TrAdaBoostR2 (LinearRegression (),
137
+ Xt = X , yt = y ,
138
+ n_estimators = 20 )
139
+ model .fit (X , y )
140
+ yp = model .predict (X )
141
+ score = model .score (X , y )
142
+ assert np .all (yp .shape == (10 , 5 ))
143
+ assert score == r2_score (y , yp )
144
+
145
+ model = TwoStageTrAdaBoostR2 (LinearRegression (),
146
+ Xt = X , yt = y ,
147
+ n_estimators = 3 ,
148
+ n_estimators_fs = 3 )
149
+ model .fit (X , y )
150
+ yp = model .predict (X )
151
+ score = model .score (X , y )
152
+ assert np .all (yp .shape == (10 , 5 ))
153
+ assert score == r2_score (y , yp )
154
+
155
+
156
+ def test_tradaboost_above_05 ():
157
+ np .random .seed (0 )
158
+ X = np .random .randn (10 , 3 )
159
+ y = np .random .randn (10 , 5 )
160
+ model = TrAdaBoostR2 (LinearRegression (),
161
+ Xt = Xt [:10 ], yt = yt_reg [:10 ],
162
+ n_estimators = 20 )
163
+ model .fit (Xs , ys_reg )
164
+ assert np .any (np .array (model .estimator_errors_ )> 0.5 )
165
+
166
+ model = TrAdaBoostR2 (Ridge (1. ),
167
+ Xt = Xt [:20 ], yt = yt_reg [:20 ],
168
+ n_estimators = 20 )
169
+ model .fit (Xs , ys_reg )
170
+ assert np .all (np .array (model .estimator_errors_ )< 0.5 )
171
+
172
+
173
+ def test_tradaboost_lr ():
174
+ np .random .seed (0 )
175
+ model = TrAdaBoost (LogisticRegression (penalty = 'none' ),
176
+ Xt = Xt [:10 ], yt = yt_classif [:10 ],
177
+ n_estimators = 20 , lr = .1 )
178
+ model .fit (Xs , ys_classif )
179
+ err1 = model .estimator_errors_
180
+
181
+ model = TrAdaBoost (LogisticRegression (penalty = 'none' ),
182
+ Xt = Xt [:10 ], yt = yt_classif [:10 ],
183
+ n_estimators = 20 , lr = 2. )
184
+ model .fit (Xs , ys_classif )
185
+ err2 = model .estimator_errors_
186
+
187
+ assert np .sum (err1 ) > 10 * np .sum (err2 )
0 commit comments