【发布时间】:2020-06-19 16:11:42
【问题描述】:
以下操作涉及 Python scikit-learn 中的逻辑回归
我给你最重要的代码示例:
predictions = logistic_regression.predict(X_test)
prediction=logistic_regression.predict_proba(X_test)[:,:]
prediction=pd.DataFrame(data=predictions,
columns=['Prob of Bad credit (0)','Prob of Good credit (1)'])
prediction.head(10)
昨天我得到了符合我预期的代码结果: (不是同一个表名,而是同一个结果)
但是今天,我完全不知道为什么,当我想再次运行这段代码时,我遇到了一个错误:
ValueError: Shape of passed values is (300, 1), indices imply (300, 2)
怎么可能昨天有效而今天无效?我能做些什么 ? 下面是完整的错误屏幕:
预测样本是这样的:
print(predictions)
[1 1 1 1 1 1 1 1 0 0 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 0 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 0 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 0 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1]
我不想在表格中有 1 或 0 我希望在屏幕中的示例中获得 1 或 0 的百分比概率
从下面的源代码中查看预测末尾的同一张表,有相同的代码并且有效: https://www.kaggle.com/neisha/heart-disease-prediction-using-logistic-regression
【问题讨论】:
-
可能是因为第一个值被作为索引?第二列作为第一列,因此您缺少第二列的数据?
-
最好不将变量命名得如此相似。你确定
data=predictions使用的是你想要的变量吗? -
另外,你为什么在预测之后使用
[:,:]? -
predict()为您提供预测类别(单列数据),predict_proba为您提供预测类别概率的元组。您是说数据框调用中有两列。回应@user2357112supportsMonica,检查你的变量 -
但是昨天它起作用了,怎么可能以及如何编辑我的代码?
标签: python pandas regression shapes