【问题标题】:Enormous and weird error in scikit-learn in Python Logistic Regression?Python Logistic Regression 中的 scikit-learn 出现巨大而奇怪的错误?
【发布时间】: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)

昨天我得到了符合我预期的代码结果: (不是同一个表名,而是同一个结果)

enter image description here

但是今天,我完全不知道为什么,当我想再次运行这段代码时,我遇到了一个错误:

ValueError: Shape of passed values is (300, 1), indices imply (300, 2)

怎么可能昨天有效而今天无效?我能做些什么 ? 下面是完整的错误屏幕:

enter image description here

预测样本是这样的:

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


【解决方案1】:

我认为发生错误是因为预测只有一行, 你有两个列名:

prediction=pd.DataFrame(data=predictions, 
                         columns=['Prob of Bad credit (0)','Prob of Good credit (1)'])

根据您提供的 kaggle 代码:

y_pred_prob=logreg.predict_proba(x_test)[:,:]
y_pred_prob_df=pd.DataFrame(data=y_pred_prob, columns=['Prob of no heart disease (0)','Prob of Heart Disease (1)'])
y_pred_prob_df.head()

我认为您应该将代码更改为:

prediction_df = pd.DataFrame(data=prediction,  
                         columns=['Prob of Bad credit (0)','Prob of Good credit (1)'])

小心它应该是预测,而不是预测。

【讨论】:

  • 它也不起作用,现在我有错误:ValueError: 传递值的形状是 (2, 300),索引意味着 (2, 2)
  • 您已经定义了预测,之前它是一个 2 x 2 数组。尝试设置另一个名称,它应该可以工作
  • 在 kaggle 的源代码中查看同一张表,有和我一样的代码,它在那里工作:kaggle.com/neisha/…
猜你喜欢
  • 2015-10-12
  • 2019-08-27
  • 2017-08-30
  • 2018-04-18
  • 2020-09-12
  • 2013-03-20
  • 1970-01-01
  • 2016-06-28
相关资源
最近更新 更多