Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为AUC分数提供nan值的Gridsearch_Python_Scikit Learn_Random Forest_Auc_Gridsearchcv - Fatal编程技术网

Python 为AUC分数提供nan值的Gridsearch

Python 为AUC分数提供nan值的Gridsearch,python,scikit-learn,random-forest,auc,gridsearchcv,Python,Scikit Learn,Random Forest,Auc,Gridsearchcv,我尝试在具有AUC分数的随机森林分类器上运行网格搜索 这是我的密码: from sklearn.ensemble import RandomForestClassifier from sklearn.model_selection import GridSearchCV from sklearn.model_selection import RepeatedStratifiedKFold from sklearn.metrics import make_scorer, roc_auc_score

我尝试在具有AUC分数的随机森林分类器上运行网格搜索

这是我的密码:

from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import GridSearchCV
from sklearn.model_selection import RepeatedStratifiedKFold
from sklearn.metrics import make_scorer, roc_auc_score

estimator = RandomForestClassifier()
scoring = {'auc': make_scorer(roc_auc_score, multi_class="ovr")}
kfold = RepeatedStratifiedKFold(n_splits=3, n_repeats=10, random_state=42)

grid_search = GridSearchCV(estimator=estimator, param_grid=param_grid,
                           cv=kfold, n_jobs=-1, scoring=scoring)
grid_search.fit(X, y)
但是,当我运行此操作时,我会得到AUC分数的nan值和以下警告:

UserWarning,
/opt/conda/lib/python3.7/site-packages/sklearn/model_selection/_validation.py:687: UserWarning: Scoring failed. The score on this train-test partition for these parameters will be set to nan. Details: 
Traceback (most recent call last):
  File "/opt/conda/lib/python3.7/site-packages/sklearn/model_selection/_validation.py", line 674, in _score
    scores = scorer(estimator, X_test, y_test)
  File "/opt/conda/lib/python3.7/site-packages/sklearn/metrics/_scorer.py", line 88, in __call__
    *args, **kwargs)
  File "/opt/conda/lib/python3.7/site-packages/sklearn/metrics/_scorer.py", line 243, in _score
    **self._kwargs)
  File "/opt/conda/lib/python3.7/site-packages/sklearn/utils/validation.py", line 63, in inner_f
    return f(*args, **kwargs)
  File "/opt/conda/lib/python3.7/site-packages/sklearn/metrics/_ranking.py", line 538, in roc_auc_score
    multi_class, average, sample_weight)
  File "/opt/conda/lib/python3.7/site-packages/sklearn/metrics/_ranking.py", line 595, in _multiclass_roc_auc_score
    if not np.allclose(1, y_score.sum(axis=1)):
  File "/opt/conda/lib/python3.7/site-packages/numpy/core/_methods.py", line 47, in _sum
    return umr_sum(a, axis, dtype, out, keepdims, initial, where)
numpy.AxisError: axis 1 is out of bounds for array of dimension 1

  UserWarning,
/opt/conda/lib/python3.7/site-packages/sklearn/model_selection/_search.py:921: UserWarning: One or more of the test scores are non-finite: [nan nan nan ... nan nan nan]
  category=UserWarning
/opt/conda/lib/python3.7/site-packages/sklearn/model_selection/_search.py:921: UserWarning: One or more of the train scores are non-finite: [nan nan nan ... nan nan nan]
  category=UserWarning

事实上我已经弄明白了。我需要在make_scorer函数中将needs_proba设置为True,这样gridsearch就不会直接从我的估计器的(分类)预测中计算auc分数

scoring = {'auc': make_scorer(roc_auc_score, needs_proba=True, multi_class="ovr")}