【发布时间】:2021-10-30 13:43:39
【问题描述】:
我在训练集和测试集上拟合了LogisticRegression,准确率约为 80%
然后我想对测试集进行预测,根据answered_correctly 是否为每个student_id 给出分数[1 表示是,0 表示否]。
我这样做了:
features_X = X.columns # getting columns names of X
# X_test is an array created from a previous train_test_split step.
test_df = pd.DataFrame(columns=features_X, data=X_test)
predictions = grid_logit.predict(test_df[features_X])
#Create a DataFrame with predictions
submission = pd.DataFrame({'Id':test_df['student_id'],'Answered_correctly':predictions})
#Visualize the first 5 rows
submission.head()
Id Answered_correctly
12992348 0
7268428 0
9497321 1
588792 1
5045118 1
如您所见,它将每个用户分类在 0 和 1 之间。
我想要的是这样的:
Id Answered_correctly
12992348 0.32
7268428 0.52
9497321 0.65
answered_correctly_values 对应于属于第 1 类的概率。
注意:使用predict_probafunction 返回错误:
Exception: Data must be 1-dimensional
编辑:
我用predict_proba(test_df[[features_X]]) 替换了predict
但它返回一个错误:None of [[ features_X cols]] are in the [columns]
【问题讨论】:
-
predict_proba为您提供所需的内容。您没有显示您尝试调用它的代码 -
@krisograbek 我没有显示它,因为我只是在上面的代码中替换了它。
grid_logit.predict_proba
标签: python machine-learning scikit-learn classification logistic-regression