|
4 | 4 | from sklearn.utils.testing import
A3E2
assert_less
|
5 | 5 | from sklearn.utils.testing import assert_greater
|
6 | 6 | from sklearn.utils.testing import assert_array_almost_equal, assert_array_equal
|
| 7 | +from sklearn.utils.testing import assert_almost_equal |
7 | 8 | from sklearn.utils.testing import assert_raises
|
8 | 9 |
|
9 | 10 | from sklearn.base import ClassifierMixin
|
@@ -125,6 +126,77 @@ def test_classifier_undefined_methods():
|
125 | 126 | assert_raises(AttributeError, lambda x: getattr(clf, x), meth)
|
126 | 127 |
|
127 | 128 |
|
| 129 | +def test_class_weights(): |
| 130 | + # Test class weights. |
| 131 | + X2 = np.array([[-1.0, -1.0], [-1.0, 0], [-.8, -1.0], |
| 132 | + [1.0, 1.0], [1.0, 0.0]]) |
| 133 | + y2 = [1, 1, 1, -1, -1] |
| 134 | + |
| 135 | + clf = PassiveAggressiveClassifier(C=0.1, n_iter=100, class_weight=None, |
| 136 | + random_state=100) |
| 137 | + clf.fit(X2, y2) |
| 138 | + assert_array_equal(clf.predict([[0.2, -1.0]]), np.array([1])) |
| 139 | + |
| 140 | + # we give a small weights to class 1 |
| 141 | + clf = PassiveAggressiveClassifier(C=0.1, n_iter=100, |
| 142 | + class_weight={1: 0.001}, |
| 143 | + random_state=100) |
| 144 | + clf.fit(X2, y2) |
| 145 | + |
| 146 | + # now the hyperplane should rotate clock-wise and |
| 147 | + # the prediction on this point should shift |
| 148 | + assert_array_equal(clf.predict([[0.2, -1.0]]), np.array([-1])) |
| 149 | + |
| 150 | + |
| 151 | +def test_partial_fit_weight_class_balanced(): |
| 152 | + # partial_fit with class_weight='balanced' not supported |
| 153 | + clf = PassiveAggressiveClassifier(class_weight="balanced") |
| 154 | + assert_raises(ValueError, clf.partial_fit, X, y, classes=np.unique(y)) |
| 155 | + |
| 156 | + |
| 157 | +def test_equal_class_weight(): |
| 158 | + X2 = [[1, 0], [1, 0], [0, 1], [0, 1]] |
| 159 | + y2 = [0, 0, 1, 1] |
| 160 | + clf = PassiveAggressiveClassifier(C=0.1, n_iter=1000, class_weight=None) |
| 161 | + clf.fit(X2, y2) |
| 162 | + |
| 163 | + # Already balanced, so "balanced" weights should have no effect |
| 164 | + clf_balanced = PassiveAggressiveClassifier(C=0.1, n_iter=1000, |
| 165 | + class_weight="balanced") |
| 166 | + clf_balanced.fit(X2, y2) |
| 167 | + |
| 168 | + clf_weighted = PassiveAggressiveClassifier(C=0.1, n_iter=1000, |
| 169 | + class_weight={0: 0.5, 1: 0.5}) |
| 170 | + clf_weighted.fit(X2, y2) |
| 171 | + |
| 172 | + # should be similar up to some epsilon due to learning rate schedule |
| 173 | + assert_almost_equal(clf.coef_, clf_weighted.coef_, decimal=2) |
| 174 | + assert_almost_equal(clf.coef_, clf_balanced.coef_, decimal=2) |
| 175 | + |
| 176 | + |
| 177 | +def test_wrong_class_weight_label(): |
| 178 | + # ValueError due to wrong class_weight label. |
| 179 | + X2 = np.array([[-1.0, -1.0], [-1.0, 0], [-.8, -1.0], |
| 180 | + [1.0, 1.0], [1.0, 0.0]]) |
| 181 | + y2 = [1, 1, 1, -1, -1] |
| 182 | + |
| 183 | + clf = PassiveAggressiveClassifier(class_weight={0: 0.5}) |
| 184 | + assert_raises(ValueError, clf.fit, X2, y2) |
| 185 | + |
| 186 | + |
| 187 | +def test_wrong_class_weight_format(): |
| 188 | + # ValueError due to wrong class_weight argument type. |
| 189 | + X2 = np.array([[-1.0, -1.0], [-1.0, 0], [-.8, -1.0], |
| 190 | + [1.0, 1.0], [1.0, 0.0]]) |
| 191 | + y2 = [1, 1, 1, -1, -1] |
| 192 | + |
| 193 | + clf = PassiveAggressiveClassifier(class_weight=[0.5]) |
| 194 | + assert_raises(ValueError, clf.fit, X2, y2) |
| 195 | + |
| 196 | + clf = PassiveAggressiveClassifier(class_weight="the larch") |
| 197 | + assert_raises(ValueError, clf.fit, X2, y2) |
| 198 | + |
| 199 | + |
128 | 200 | def test_regressor_mse():
|
129 | 201 | y_bin = y.copy()
|
130 | 202 | y_bin[y != 1] = -1
|
|
0 commit comments