我如何用我的输出打印出正确的Id

2024-04-26 13:28:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在与sklearn和pandas一起工作,我的预测结果是一个数组,没有正确的id,它被设置为索引。在

我的代码:

train = train.set_index('activity_id')
test = test.set_index('activity_id')

y_train = train['outcome']
x_train = train.drop('people_id', axis=1)
x_test = test

model = DecisionTreeClassifier(min_samples_leaf=100)

model.fit(x_train,y_train)


scores = cross_val_score(model, x_train,y_train, cv=10)
print('mean: {:.3f} (std: {:.3f})'.format(scores.mean(), scores.std()), end='\n\n')
print(model.score(x_train,y_train))
#make predictions
y_pred = model.predict(x_test)

有什么想法让他们打印出正确的活动清单?谢谢!在


Tags: testidpandasindexmodeltrain数组sklearn
1条回答
网友
1楼 · 发布于 2024-04-26 13:28:52

从你写的东西来看,我相信你是想在x测试生成的y_pred值旁边显示你的x_test索引。在

这可以通过将numpy数组输出从model.predict(x_test)转换为一个数据帧来完成。然后我们可以将新数据帧的索引设置为与x_test的索引相同。在

举个例子

df_pred = pd.DataFrame(y_pred, index=x_test.index, columns=['y_pred'])

相关问题 更多 >