@@ -346,7 +346,7 @@ def test_LinearSVC():
346346 assert clf .fit_intercept
347347
348348 assert_array_equal (clf .predict (T ), true_result )
349- assert_array_almost_equal (clf .intercept_ , [0 ], decimal = 5 )
349+ assert_array_almost_equal (clf .intercept_ , [0 ], decimal = 3 )
350350
351351 # the same with l1 penalty
352352 clf = svm .LinearSVC (penalty = 'l1' , dual = False ).fit (X , Y )
@@ -368,6 +368,40 @@ def test_LinearSVC_iris():
368368 clf = svm .LinearSVC ().fit (iris .data , iris .target )
369369 assert np .mean (clf .predict (iris .data ) == iris .target ) > 0.95
370370
371+ def test_dense_liblinear_intercept_handling (classifier = svm .LinearSVC ):
372+ """
373+ Test that dense liblinear honours intercept_scaling param
374+ """
375+ X = [[2 , 1 ],
376+ [3 , 1 ],
377+ [1 , 3 ],
378+ [2 , 3 ]]
379+ y = [0 , 0 , 1 , 1 ]
380+ clf = classifier (fit_intercept = True , penalty = 'l1' , loss = 'l2' ,
381+ dual = False , C = 1 , eps = 1e-7 )
382+ assert clf .intercept_scaling == 1 , clf .intercept_scaling
383+ assert clf .fit_intercept
384+
385+ # when intercept_scaling is low the intercept value is highly "penalized"
386+ # by regularization
387+ clf .intercept_scaling = 1
388+ clf .fit (X , y )
389+ assert_almost_equal (clf .intercept_ , 0 , decimal = 5 )
390+
391+ # when intercept_scaling is sufficiently high, the intercept value
392+ # is not affected by regularization
393+ clf .intercept_scaling = 100
394+ clf .fit (X , y )
395+ intercept1 = clf .intercept_
396+ assert intercept1 > 1
397+
398+ # when intercept_scaling is sufficiently high, the intercept value
399+ # doesn't depend on intercept_scaling value
400+ clf .intercept_scaling = 1000
401+ clf .fit (X , y )
402+ intercept2 = clf .intercept_
403+ assert_array_almost_equal (intercept1 , intercept2 , decimal = 2 )
404+
371405
372406if __name__ == '__main__' :
373407 import nose
0 commit comments