簡體   English   中英

Python Logistic Regression 中 scikit-learn 中的巨大而奇怪的錯誤?

[英]Enormous and weird error in scikit-learn in Python Logistic Regression?

下面的操作涉及 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 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 0 1 1 0 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 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 0 1 0 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 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 或 0 我希望在屏幕中的示例中具有 1 或 0 的百分比概率

從下面的源代碼中查看預測末尾的同一張表,有相同的代碼並且它有效: https : //www.kaggle.com/neisha/heart-disease-prediction-using-logistic-regression

我認為發生錯誤是因為預測只有一行,而您有兩個列名:

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)'])

請注意,它應該是預測,而不是預測。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM