繁体   English   中英

f1_score:ValueError所有输入数组的维数必须相同

[英]f1_score : ValueError all the input arrays must have same number of dimensions

我想计算f1_score

代码如下所示:

if __name__ == '__main__':
    y_pred_df = pd.read_csv('file1.csv', skipinitialspace=True, sep='\t', header=None, dtype= str)
    y_pred = y_pred_df.values

    y_true_df = pd.read_csv('file2.csv', header=None, dtype= str)
    y_true = y_true_df.values

    test_score = accuracy_score(y_true[:,0], y_pred[:,0])
    print("\n Accuracy score (Random Forest with 100 estimators) : {}%".format(round(test_score*100,2)))

    print (y_true[:,0])
    print (y_pred[:,0])

    score_test = f1_score(y_true[:,0], y_pred[:,0],pos_label=list(set(y_true[:,0])),average = 'weighted')


    print (score_test)

执行上述代码时,在计算f1_score时出现以下错误:

Accuracy score (Random Forest with 100 estimators) : 61.62%
['4' '4' '4' '4' '4' '12' '12' '12' '12' '12' '12' '12' '12' '4' '4' '4'
'4' '4' '4' '4' '4' '4' '12' '12' '12' '12' '12' '12' '12' '12' '12' '12'
'12' '12' '12' '12' '12' '12' '12' '12' '12' '12' '12' '12' '12' '12'     '12'
'12' '12' '12' '12' '4' '4' '4' '4' '4' '4' '4' '4' '4' '4' '4' '4' '4'
'4' '4' '4' '4' '4' '12' '12' '4' '4' '4' '12' '12' '12' '12' '12' '12'
'12' '12' '12' '12' '12' '12' '12' '12' '12' '12' '12' '12' '12' '4' '4'
'4' '4' '4' '4']

['4' '12' '12' '12' '12' '12' '12' '12' '12' '12' '12' '12' '12' '12' '12'
'12' '12' '12' '12' '12' '12' '12' '12' '12' '12' '12' '12' '12' '12' '12'
'12' '12' '12' '12' '12' '12' '12' '12' '12' '12' '12' '12' '12' '12' '12'
'12' '12' '12' '12' '12' '12' '12' '4' '12' '4' '12' '12' '12' '12' '12'
'12' '12' '12' '12' '12' '12' '12' '12' '12' '12' '12' '12' '12' '12' '12'
'12' '12' '12' '12' '12' '12' '12' '12' '12' '12' '12' '12' '12' '12' '12'
'12' '12' '12' '12' '12' '12' '12' '12' '12']
Traceback (most recent call last):
  File "<ipython-input-25-f80f0ca3aea2>", line 1, in <module>
runfile('C:/Anaconda3/envs/python27/Scripts/spade/examples/project/Fmeasure.py', wdir='C:/Anaconda3/envs/python27/Scripts/spade/examples/project')

  File "C:\Anaconda3\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 714, in runfile
    execfile(filename, namespace)

  File "C:\Anaconda3\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 89, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/Anaconda3/envs/python27/Scripts/spade/examples/project/Fmeasure.py", line 47, in <module>
    score_test = f1_score(y_true[:,0], y_pred[:,0],pos_label=list(set(y_true[:,0])),average = 'binary')

  File "C:\Anaconda3\lib\site-packages\sklearn\metrics\classification.py", line 639, in f1_score
    sample_weight=sample_weight)

  File "C:\Anaconda3\lib\site-packages\sklearn\metrics\classification.py", line 756, in fbeta_score
    sample_weight=sample_weight)

  File "C:\Anaconda3\lib\site-packages\sklearn\metrics\classification.py", line 992, in precision_recall_fscore_support
    assume_unique=True)])

  File "C:\Anaconda3\lib\site-packages\numpy\core\shape_base.py", line 280, in hstack
    return _nx.concatenate(arrs, 1)

ValueError: all the input arrays must have same number of dimensions

你能告诉我问题来源吗?

pos_label必须仅包含一个元素,您正在传递标签列表。

pos_label旨在一次计算一个标签的f1得分,当您传递列表时它崩溃。 如果要计算每个标签的f1,则应进行循环,在其中循环遍历标签集,如下所示:

for label in set(yt)
    score_test = f1_score(yt_, yp_, pos_label=[label])
    print( 'f1', label, score_test )

如果您想要的是f1分数的加权平均值,那么您不应该使用pos_label,

score_test = f1_score(yt_, yp_, average = 'weighted')

但是,在sklearn 0.20上,以下工作有效,但它会向您发出警告

from sklearn.metrics import f1_score

if __name__ == '__main__':
    yt_ =  ['4', '4', '4', '4', '4', '12', '12', '12', '12', '12', '12', '12', '12', '4', '4', '4', '4', '4', '4', '4', '4', '4', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '12', '12', '4', '4', '4', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '4', '4', '4', '4', '4', '4']

    yp_ = ['4', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '4', '12', '4', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12', '12'] 

    score_test = f1_score(yt_, yp_, pos_label=list(set(yt_)),average = 'weighted')

    print (score_test)  

警告:

用户警告:请注意,当平均值!='binary'(得到'weighted')时,pos_label(设置为['12','4'])将被忽略。 您可以使用labels = [pos_label]来指定单个肯定类。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM