【发布时间】:2018-02-19 01:01:42
【问题描述】:
我建立了一个分类器,想试试pd.crosstab。但是,它似乎给了我不正确的总元素数量,这令人困惑,我不知道为什么。
实际代码:
df_confusion = pd.crosstab(pd.Series(y_pred), pd.Series(y_test),
rownames=['Predicted'], colnames= ['Actual'],
margins=True)
在 jupyter notebook 中输入:df_confusion 产生
**Actual** 0.0 1.0 **All**
**Predicted**
**0.0** 6529 1951 8480
**1.0** 718 208 926
**All** 7247 2159 9406**
而y_pred和y_test中每个类别0和1的元素总数如下:
sum(y_pred==0) equals 34264
sum(y_pred==1) equals 3514
sum(y_test==1) equals 34259
sum(y_test==0) equals 3519
但是导入confusion_matrix 会产生预期的答案
from sklearn.metrics import confusion_matrix
confusion_matrix(y_test,y_pred)
array([[34259, 0],
[ 5, 3514]], dtype=int64)
【问题讨论】:
-
请确保您直接连续运行您的代码并再次检查结果。
-
pd.isnull(y_pred).any()和pd.isnull(y_test).any()返回什么? -
pd.crosstab将(非 NaN?)值报告为0.0和1.0。如果y_pred和y_test仅包含整数值,那么这些值将保持为整数。pd.crosstab将这些值报告为浮点数表明pd.Series将这些值提升为浮点数,因为存在一些非整数值,例如np.nan。 -
谢谢! pd.isnull(y_pred).any() 的答案是 False 和 y_test
-
@unutbu pd.unique(y_pred) 产生 array([0, 1], dtype=int64) ,y_test 的答案相同
标签: python python-2.7 pandas crosstab