前提・実現したいこと
機械学習初心者です。
SVMで画像分類を行うことを目標として、GridSearchCVでハイパラメータのチューニングを行っています。
GridSearchCVをfitさせるとbest_score_などの結果は得られるのですが、UndefinedMetricWarningが大量に出てしまいます。
この警告がなぜ出るのかという理由と、消す方法を教えていただけますでしょうか。
発生している問題・エラーメッセージ
FutureWarning: You are accessing a training score ('std_train_score'), which will not be available by default any more in 0.21. If you need training scores, please set return_train_score=True warnings.warn(*warn_args, **warn_kwargs)
該当のソースコード
Python
1#グリッドサーチ 2X_train, X_test, y_train, y_test = train_test_split(Images,Labels, test_size=0.2, random_state=0) 3tuned_parameters = [ 4 {'C': [1, 10, 100, 1000], 'kernel': ['linear']}, 5 {'C': [1, 10, 100, 1000], 'kernel': ['rbf'], 'gamma': [10,1,0.01,0.1,0.001,0.0001]}, 6 {'C': [1, 10, 100, 1000], 'kernel': ['poly'], 'degree': [2, 3, 4], 'gamma': [10,1,0.1,0.01,0.001, 0.0001]}, 7 {'C': [1, 10, 100, 1000], 'kernel': ['sigmoid'], 'gamma': [10,1,0.1,0.01,0.001,0.0001]} 8 ] 9clf = GridSearchCV( 10 SVC(), # 識別器 11 tuned_parameters, # 最適化したいパラメータセット 12 cv=StratifiedKFold(), # 交差検定の回数 13 scoring = 'f1' 14 )# モデルの評価関数の指定 15clf.fit(X_train,y_train)
###コード内変数の説明
train_test_splitに渡しているImagesは訓練画像で、85枚+84枚の計169枚のデータが入っています。
またLabelsには0と1のラベルが入っています。
試したこと
・GridSearchCVのcvを以前は数字でそのまま指定していたのですが、StratifiedKFold()に変えてみました。
・scoreingを'f1'から'recall'に変えてみると警告が消えました。
・scikit-learnのバージョンの問題で起こることがあるという情報がありましたので、アップグレードしました(現在0.19.1)
・サンプル数が少ないとこの警告が出ることがあるという情報がありましたので、変数Imagesを435+435の計870枚のデータに変えてみましたが、変化は見られませんでした。
・警告内容から予測結果がすべてネガティブだと、Precisionやrecallが0になることでF値が0/0になってしまうことが原因とのことでしたので、GridSearchCV内で行われる交差検定の結果についてconfusion_matrixができないかと考えたのですが、やり方がわかりませんでした。
###問題かと思われるところ
GridSearchCVのcv_results_を実行すると、 mean_test_scoreやmean_train_score,split0_train_scoreなどが0.000000になっているところがありました。
0.000000になっているところは何度実行しても値がそのままです。
cv_resultsの実行結果も載せさせていただきます。
Python
1 mean_fit_time mean_score_time mean_test_score mean_train_score \ 20 0.000667 0.000334 0.549208 0.494650 31 0.000000 0.000667 0.618010 0.635955 42 0.000334 0.000334 0.606922 0.640143 53 0.000667 0.000334 0.606922 0.640143 64 0.000667 0.000334 0.648052 0.647610 75 0.000334 0.000334 0.635159 0.638346 86 0.000667 0.000334 0.000000 0.000000 97 0.000000 0.001001 0.000000 0.000000 108 0.000000 0.001001 0.000000 0.000000 119 0.000667 0.000667 0.000000 0.000000 12(中略) 13 param_C param_degree param_gamma param_kernel \ 140 1 NaN NaN linear 151 10 NaN NaN linear 162 100 NaN NaN linear 173 1000 NaN NaN linear 184 1 NaN 10 rbf 195 1 NaN 1 rbf 206 1 NaN 0.01 rbf 217 1 NaN 0.1 rbf 228 1 NaN 0.001 rbf 239 1 NaN 0.0001 rbf 24(中略) 25 params rank_test_score \ 260 {'C': 1, 'kernel': 'linear'} 25 271 {'C': 10, 'kernel': 'linear'} 11 282 {'C': 100, 'kernel': 'linear'} 19 293 {'C': 1000, 'kernel': 'linear'} 19 304 {'C': 1, 'gamma': 10, 'kernel': 'rbf'} 2 315 {'C': 1, 'gamma': 1, 'kernel': 'rbf'} 7 326 {'C': 1, 'gamma': 0.01, 'kernel': 'rbf'} 47 337 {'C': 1, 'gamma': 0.1, 'kernel': 'rbf'} 47 348 {'C': 1, 'gamma': 0.001, 'kernel': 'rbf'} 47 359 {'C': 1, 'gamma': 0.0001, 'kernel': 'rbf'} 47 36 37(中略) 38 split0_test_score split0_train_score split1_test_score \ 390 0.545455 0.500000 0.473684 401 0.590909 0.623656 0.571429 412 0.558140 0.637363 0.571429 423 0.558140 0.637363 0.571429 434 0.590909 0.623656 0.590909 445 0.666667 0.631579 0.571429 456 0.000000 0.000000 0.000000 467 0.000000 0.000000 0.000000 478 0.000000 0.000000 0.000000 489 0.000000 0.000000 0.000000 49(中略) 50 split1_train_score split2_test_score split2_train_score std_fit_time \ 510 0.617284 0.628571 0.366667 4.717075e-04 521 0.631579 0.692308 0.652632 0.000000e+00 532 0.630435 0.692308 0.652632 4.718199e-04 543 0.630435 0.692308 0.652632 4.717075e-04 554 0.639175 0.763636 0.680000 4.717637e-04 565 0.638298 0.666667 0.645161 4.718199e-04 576 0.000000 0.000000 0.000000 4.718199e-04 587 0.000000 0.000000 0.000000 0.000000e+00 598 0.000000 0.000000 0.000000 0.000000e+00 609 0.000000 0.000000 0.000000 4.717075e-04 61(中略) 62 std_score_time std_test_score std_train_score 630 4.718199e-04 0.063051 0.102384 641 4.718200e-04 0.052845 0.012227 652 4.717075e-04 0.060282 0.009273 663 4.717075e-04 0.060282 0.009273 674 4.718199e-04 0.081270 0.023763 685 4.718199e-04 0.044811 0.005545 696 4.718199e-04 0.000000 0.000000 707 1.123916e-07 0.000000 0.000000 718 2.247832e-07 0.000000 0.000000 729 4.716513e-04 0.000000 0.000000
なぜ0.000000になってしまうのかわからないです。
GridSearchCV内の引数がおかしいのでしょうか。それともデータの問題なのでしょうか。
何かお気づきの点などございましたら、ご教示頂けますと幸いです。
よろしくお願いいたします。
補足情報(FW/ツールのバージョンなど)
開発環境
Windows10
WinPython-64bit-3.6.3(Python3.6)
###追記1
Python
1 2 fit_time score_time test_f1 test_precision test_recall train_f1 \ 30 0.001001 0.002001 0.0 0.0 0.0 0.0 41 0.001001 0.002001 0.0 0.0 0.0 0.0 52 0.000000 0.002002 0.0 0.0 0.0 0.0 6 7 train_precision train_recall 80 0.0 0.0 91 0.0 0.0 102 0.0 0.0
###追記2
Python
1UndefinedMetricWarning: F-score is ill-defined and being set to 0.0 due to no predicted samples. 2 'precision', 'predicted', average, warn_for
###追記3
Python
1 fit_time score_time test_accuracy test_f1_macro train_accuracy \ 20 0.0005 0.001501 0.533333 0.347826 0.522727 31 0.0005 0.001001 0.522727 0.343284 0.528090 42 0.0005 0.000500 0.522727 0.343284 0.528090 5 6 train_f1_macro 70 0.343284 81 0.345588 92 0.345588 10 11 12 13Z:\Downloads\WinPython-64bit-3.6.3.0Qt5\python-3.6.3.amd64\lib\site-packages\sklearn\metrics\classification.py:1135: UndefinedMetricWarning: F-score is ill-defined and being set to 0.0 in labels with no predicted samples. 14 'precision', 'predicted', average, warn_for) 15Z:\Downloads\WinPython-64bit-3.6.3.0Qt5\python-3.6.3.amd64\lib\site-packages\sklearn\metrics\classification.py:1135: UndefinedMetricWarning: F-score is ill-defined and being set to 0.0 in labels with no predicted samples. 16 'precision', 'predicted', average, warn_for) 17Z:\Downloads\WinPython-64bit-3.6.3.0Qt5\python-3.6.3.amd64\lib\site-packages\sklearn\metrics\classification.py:1135: UndefinedMetricWarning: F-score is ill-defined and being set to 0.0 in labels with no predicted samples. 18 'precision', 'predicted', average, warn_for)
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/07/13 03:23
2018/07/13 03:57
2018/07/13 05:20
2018/07/13 06:21
2018/07/13 06:29 編集
2018/07/13 16:11
2018/07/13 19:47
2018/07/18 05:42