sklearn classification_report,输入来自pandas dataframe prduces:"TypeError:并非在字符串格式化期间转换所有参数"

Mar*_*s W 3 python-2.7 pandas scikit-learn

我正在尝试运行sklearn.metrics.classification_report,我的数据在Pandas数据帧中.数据框df_joined看起来像这样,有100行:

Timestamp    Label       Pred
2016-10-05   29.75  30.781430
2016-10-06   30.35  31.379146
2016-10-07   31.59  31.174824
2017-02-13   29.63  29.875497
2017-02-14   29.60  29.923161
2017-02-15   30.22  30.257284
2017-02-16   30.12  30.374257
2017-02-17   30.09  30.357196
2017-02-20   31.03  30.971070
2017-02-21   31.05  30.930189
Run Code Online (Sandbox Code Playgroud)

我现在正在尝试打印classification_report

print 'Classification Report:', '\n', sklearn.metrics.classification_report(df_joined[label],df_joined['Pred'] )
Run Code Online (Sandbox Code Playgroud)

我收到错误:

文件"\ Python\WinPython-32bit-2.7.10.3\python-2.7.10\lib\site-packages\sklearn\utils\multiclass.py",第106行,在unique_labels中引发ValueError("未知标签类型:%r" %ys)

TypeError:并非在字符串格式化期间转换所有参数

我一直试图使用,sklearn.metrics.classification_report(df_joined[label].values, df_joined['Pred'].values)但它产生相同的错误.

有人知道这是从哪里来的?

joe*_*lom 5

我相信classification_report量化了您对数据点标签进行分类/预测的程度,而不是其实际值.标签不能是浮点数,sklearn文档sklearn用户指南中的所有示例都使用整数作为标签.

这些参数也暗示了这一点,因为传递1-d数组的替代方法是仅用于标签的特定数组构造.

sklearn.metrics.classification_report(y_true, y_pred, labels=None,target_names=None, sample_weight=None, digits=2)

y_true : 1d array-like, or label indicator array / sparse matrix

    Ground truth (correct) target values.

y_pred : 1d array-like, or label indicator array / sparse matrix

    Estimated targets as returned by a classifier.

...
Run Code Online (Sandbox Code Playgroud)

如果您的数据是整数标签,那么您传递的确切数据帧格式就可以正常工作:

# Does not raise an error 
classification_report(df_joined['Label'].astype(int), df_joined['Pred'].astype(int))
Run Code Online (Sandbox Code Playgroud)

您可以在模型评估中阅读更多关于sklearn的不同模型评估工具:量化预测的质量,并选择一个适合评估分类器的模型.