77
88import numpy as np
99import pytest
10- from sklearn import datasets , svm
10+ from sklearn import datasets
11+ from sklearn .linear_model import LogisticRegression
1112from sklearn .metrics import (
1213 accuracy_score ,
1314 average_precision_score ,
4344
4445
4546def make_prediction (dataset = None , binary = False ):
46- """Make some classification predictions on a toy dataset using a SVC
47+ """Make some classification predictions on a toy dataset using a classifier.
4748 If binary is True restrict to a binary classification problem instead of a
4849 multiclass classification problem
4950 """
@@ -72,7 +73,7 @@ def make_prediction(dataset=None, binary=False):
7273 X = np .c_ [X , rng .randn (n_samples , 200 * n_features )]
7374
7475 # run classifier, get class probabilities and label predictions
75- clf = svm . SVC ( kernel = "linear" , probability = True , random_state = 0 )
76+ clf = LogisticRegression ( )
7677 probas_pred = clf .fit (X [:half ], y [:half ]).predict_proba (X [half :])
7778
7879 if binary :
@@ -275,10 +276,10 @@ def test_geometric_mean_sample_weight(
275276@pytest .mark .parametrize (
276277 "average, expected_gmean" ,
277278 [
278- ("multiclass" , 0.41 ),
279- (None , [0.85 , 0.29 , 0.7 ]),
280- ("macro" , 0.68 ),
281- ("weighted" , 0.65 ),
279+ ("multiclass" , 0.36 ),
280+ (None , [0.82 , 0.24 , 0.72 ]),
281+ ("macro" , 0.67 ),
282+ ("weighted" , 0.64 ),
282283 ],
283284)
284285def test_geometric_mean_score_prediction (average , expected_gmean ):
@@ -309,10 +310,10 @@ def test_classification_report_imbalanced_multiclass():
309310
310311 # print classification report with class names
311312 expected_report = (
312- "pre rec spe f1 geo iba sup setosa 0.83 0.79 0.92 "
313- "0.81 0.85 0.72 24 versicolor 0.33 0.10 0.86 0.15 "
314- "0.29 0.08 31 virginica 0.42 0.90 0.55 0.57 0.70 "
315- "0.51 20 avg / total 0.51 0.53 0.80 0.47 0.58 0.40 75"
313+ "pre rec spe f1 geo iba sup setosa 0.70 0.79 0.84 "
314+ "0.75 0.82 0.66 24 versicolor 0.29 0.06 0.89 0.11 "
315+ "0.24 0.05 31 virginica 0.44 0.90 0.58 0.59 0.72 "
316+ "0.54 20 avg / total 0.46 0.52 0.79 0.44 0.55 0.38 75"
316317 )
317318
318319 report = classification_report_imbalanced (
@@ -324,10 +325,10 @@ def test_classification_report_imbalanced_multiclass():
324325 assert _format_report (report ) == expected_report
325326 # print classification report with label detection
326327 expected_report = (
327- "pre rec spe f1 geo iba sup 0 0.83 0.79 0.92 0.81 "
328- "0.85 0.72 24 1 0.33 0.10 0.86 0.15 0.29 0.08 31 "
329- "2 0.42 0.90 0.55 0.57 0.70 0.51 20 avg / total "
330- "0.51 0.53 0.80 0.47 0.58 0.40 75"
328+ "pre rec spe f1 geo iba sup 0 0.70 0.79 0.84 0.75 "
329+ "0.82 0.66 24 1 0.29 0.06 0.89 0.11 0.24 0.05 31 "
330+ "2 0.44 0.90 0.58 0.59 0.72 0.54 20 avg / total "
331+ "0.46 0.52 0.79 0.44 0.55 0.38 75"
331332 )
332333
333334 report = classification_report_imbalanced (y_true , y_pred )
@@ -340,12 +341,12 @@ def test_classification_report_imbalanced_multiclass_with_digits():
340341
341342 # print classification report with class names
342343 expected_report = (
343- "pre rec spe f1 geo iba sup setosa 0.82609 0.79167 "
344- "0.92157 0.80851 0.85415 0.72010 24 versicolor "
345- "0.33333 0.09677 0.86364 0.15000 0.28910 0.07717 "
346- "31 virginica 0.41860 0.90000 0.54545 0.57143 0.70065 "
347- "0.50831 20 avg / total 0.51375 0.53333 0.79733 "
348- "0.47310 0.57966 0.39788 75"
344+ "pre rec spe f1 geo iba sup setosa 0.70370 0.79167 "
345+ "0.84314 0.74510 0.81700 0.66405 24 versicolor "
346+ "0.28571 0.06452 0.88636 0.10526 0.23913 0.05249 "
347+ "31 virginica 0.43902 0.90000 0.58182 0.59016 0.72363 "
348+ "0.54030 20 avg / total 0.46035 0.52000 0.79132 "
349+ "0.43932 0.55325 0.37827 75"
349350 )
350351 report = classification_report_imbalanced (
351352 y_true ,
@@ -357,10 +358,10 @@ def test_classification_report_imbalanced_multiclass_with_digits():
357358 assert _format_report (report ) == expected_report
358359 # print classification report with label detection
359360 expected_report = (
360- "pre rec spe f1 geo iba sup 0 0.83 0.79 0.92 0.81 "
361- "0.85 0.72 24 1 0.33 0.10 0.86 0.15 0.29 0.08 31 "
362- "2 0.42 0.90 0.55 0.57 0.70 0.51 20 avg / total 0.51 "
363- "0.53 0.80 0.47 0.58 0.40 75"
361+ "pre rec spe f1 geo iba sup 0 0.70 0.79 0.84 0.75 "
362+ "0.82 0.66 24 1 0.29 0.06 0.89 0.11 0.24 0.05 31 "
363+ "2 0.44 0.90 0.58 0.59 0.72 0.54 20 avg / total 0.46 "
364+ "0.52 0.79 0.44 0.55 0.38 75"
364365 )
365366 report = classification_report_imbalanced (y_true , y_pred )
366367 assert _format_report (report ) == expected_report
@@ -373,19 +374,19 @@ def test_classification_report_imbalanced_multiclass_with_string_label():
373374 y_pred = np .array (["blue" , "green" , "red" ])[y_pred ]
374375
375376 expected_report = (
376- "pre rec spe f1 geo iba sup blue 0.83 0.79 0.92 0.81 "
377- "0.85 0.72 24 green 0.33 0.10 0.86 0.15 0.29 0.08 31 "
378- "red 0.42 0.90 0.55 0.57 0.70 0.51 20 avg / total "
379- "0.51 0.53 0.80 0.47 0.58 0.40 75"
377+ "pre rec spe f1 geo iba sup blue 0.70 0.79 0.84 0.75 "
378+ "0.82 0.66 24 green 0.29 0.06 0.89 0.11 0.24 0.05 31 "
379+ "red 0.44 0.90 0.58 0.59 0.72 0.54 20 avg / total "
380+ "0.46 0.52 0.79 0.44 0.55 0.38 75"
380381 )
381382 report = classification_report_imbalanced (y_true , y_pred )
382383 assert _format_report (report ) == expected_report
383384
384385 expected_report = (
385- "pre rec spe f1 geo iba sup a 0.83 0.79 0.92 0.81 0.85 "
386- "0.72 24 b 0.33 0.10 0.86 0.15 0.29 0.08 31 c 0.42 "
387- "0.90 0.55 0.57 0.70 0.51 20 avg / total 0.51 0.53 "
388- "0.80 0.47 0.58 0.40 75"
386+ "pre rec spe f1 geo iba sup a 0.70 0.79 0.84 0.75 0.82 "
387+ "0.66 24 b 0.29 0.06 0.89 0.11 0.24 0.05 31 c 0.44 "
388+ "0.90 0.58 0.59 0.72 0.54 20 avg / total 0.46 0.52 "
389+ "0.79 0.44 0.55 0.38 75"
389390 )
390391 report = classification_report_imbalanced (
391392 y_true , y_pred , target_names = ["a" , "b" , "c" ]
@@ -401,10 +402,10 @@ def test_classification_report_imbalanced_multiclass_with_unicode_label():
401402 y_pred = labels [y_pred ]
402403
403404 expected_report = (
404- "pre rec spe f1 geo iba sup blue¢ 0.83 0.79 0.92 0.81 "
405- "0.85 0.72 24 green¢ 0.33 0.10 0.86 0.15 0.29 0.08 31 "
406- "red¢ 0.42 0.90 0.55 0.57 0.70 0.51 20 avg / total "
407- "0.51 0.53 0.80 0.47 0.58 0.40 75"
405+ "pre rec spe f1 geo iba sup blue¢ 0.70 0.79 0.84 0.75 "
406+ "0.82 0.66 24 green¢ 0.29 0.06 0.89 0.11 0.24 0.05 31 "
407+ "red¢ 0.44 0.90 0.58 0.59 0.72 0.54 20 avg / total "
408+ "0.46 0.52 0.79 0.44 0.55 0.38 75"
408409 )
409410 report = classification_report_imbalanced (y_true , y_pred )
410411 assert _format_report (report ) == expected_report
@@ -418,10 +419,10 @@ def test_classification_report_imbalanced_multiclass_with_long_string_label():
418419 y_pred = labels [y_pred ]
419420
420421 expected_report = (
421- "pre rec spe f1 geo iba sup blue 0.83 0.79 0.92 0.81 "
422- "0.85 0.72 24 greengreengreengreengreen 0.33 0.10 "
423- "0.86 0.15 0.29 0.08 31 red 0.42 0.90 0.55 0.57 0.70 "
424- "0.51 20 avg / total 0.51 0.53 0.80 0.47 0.58 0.40 75"
422+ "pre rec spe f1 geo iba sup blue 0.70 0.79 0.84 0.75 "
423+ "0.82 0.66 24 greengreengreengreengreen 0.29 0.06 "
424+ "0.89 0.11 0.24 0.05 31 red 0.44 0.90 0.58 0.59 0.72 "
425+ "0.54 20 avg / total 0.46 0.52 0.79 0.44 0.55 0.38 75"
425426 )
426427
427428 report = classification_report_imbalanced (y_true , y_pred )
0 commit comments