@@ -39,13 +39,8 @@ def test_distribution():
39
39
labels = [0 , 1 , - 1 ]
40
40
for estimator , parameters in ESTIMATORS :
41
41
clf = estimator (** parameters ).fit (samples , labels )
42
- if parameters ['kernel' ] == 'knn' :
43
- continue # unstable test; changes in k-NN ordering break it
44
- assert_array_almost_equal (clf .predict_proba ([[1. , 0.0 ]]),
45
- np .array ([[1. , 0. ]]), 2 )
46
- else :
47
- assert_array_almost_equal (np .asarray (clf .label_distributions_ [2 ]),
48
- np .array ([.5 , .5 ]), 2 )
42
+ assert_array_almost_equal (np .asarray (clf .label_distributions_ [2 ]),
43
+ np .array ([.5 , .5 ]), decimal = 3 )
49
44
50
45
51
46
def test_predict ():
@@ -62,20 +57,23 @@ def test_predict_proba():
62
57
for estimator , parameters in ESTIMATORS :
63
58
clf = estimator (** parameters ).fit (samples , labels )
64
59
assert_array_almost_equal (clf .predict_proba ([[1. , 1. ]]),
65
- np .array ([[0.5 , 0.5 ]]))
60
+ np .array ([[0.5 , 0.5 ]]), decimal = 3 )
66
61
67
62
68
63
def test_alpha_deprecation ():
69
64
X , y = make_classification (n_samples = 100 )
70
65
y [::3 ] = - 1
71
66
72
- lp_default = label_propagation .LabelPropagation (kernel = 'rbf' , gamma = 0.1 )
73
- lp_default_y = assert_no_warnings (lp_default .fit , X , y ).transduction_
67
+ for kernel in ['rbf' , 'knn' ]:
68
+ lp_default = label_propagation .LabelPropagation (kernel = kernel ,
69
+ gamma = 0.1 )
70
+ lp_default_y = assert_no_warnings (lp_default .fit , X , y ).transduction_
74
71
75
- lp_0 = label_propagation .LabelPropagation (alpha = 0 , kernel = 'rbf' , gamma = 0.1 )
76
- lp_0_y = assert_warns (DeprecationWarning , lp_0 .fit , X , y ).transduction_
72
+ lp_0 = label_propagation .LabelPropagation (alpha = 0 , kernel = kernel ,
73
+ gamma = 0.1 )
74
+ lp_0_y = assert_warns (DeprecationWarning , lp_0 .fit , X , y ).transduction_
77
75
78
- assert_array_equal (lp_default_y , lp_0_y )
76
+ assert_array_equal (lp_default_y , lp_0_y )
79
77
80
78
81
79
def test_label_spreading_closed_form ():
@@ -94,7 +92,8 @@ def test_label_spreading_closed_form():
94
92
expected /= expected .sum (axis = 1 )[:, np .newaxis ]
95
93
clf = label_propagation .LabelSpreading (max_iter = 10000 , alpha = alpha )
96
94
clf .fit (X , y )
97
- assert_array_almost_equal (expected , clf .label_distributions_ , 4 )
95
+ assert_array_almost_equal (expected , clf .label_distributions_ ,
96
+ decimal = 4 )
98
97
99
98
100
99
def test_label_propagation_closed_form ():
@@ -139,9 +138,12 @@ def test_convergence_speed():
139
138
# This is a non-regression test for #5774
140
139
X = np .array ([[1. , 0. ], [0. , 1. ], [1. , 2.5 ]])
141
140
y = np .array ([0 , 1 , - 1 ])
142
- mdl = label_propagation .LabelSpreading (kernel = 'rbf' , max_iter = 5000 )
143
- mdl .fit (X , y )
144
141
145
- # this should converge quickly:
146
- assert mdl .n_iter_ < 10
147
- assert_array_equal (mdl .predict (X ), [0 , 1 , 1 ])
142
+ for kernel in ['rbf' , 'knn' ]:
143
+ mdl = label_propagation .LabelSpreading (kernel = kernel , max_iter = 5000 ,
144
+ n_neighbors = 2 )
145
+ mdl .fit (X , y )
146
+
147
+ # this should converge quickly:
148
+ assert mdl .n_iter_ < 10
149
+ assert_array_almost_equal (mdl .predict_proba ([[0.5 , 0.5 ]]), [[0.5 , 0.5 ]], decimal = 3 )
0 commit comments