在python中获取model.predict值
我正在运行一个随机森林模型。我使用下面的代码预测值 预测=模型预测(测试x) 我希望从下面的输出中得到列表或数据帧中的预测值 数组([1,1,1,1,0,0,0,1,0,0,0,0,1,1,0,0,1,1,1,0,0,1,1,1,0,1,1,1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,1,0,1,0,0,1,0,1,1,0],dtype=int64)在python中获取model.predict值,python,output,prediction,Python,Output,Prediction,我正在运行一个随机森林模型。我使用下面的代码预测值 预测=模型预测(测试x) 我希望从下面的输出中得到列表或数据帧中的预测值 数组([1,1,1,1,0,0,0,1,0,0,0,0,1,1,0,0,1,1,1,0,0,1,1,1,0,1,1,1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0,
如何在Python中执行此操作predict函数返回一个数组对象,以便您可以将其转换为dataframe,如下所示
import pandas as pd
prediction = model.predict(test_x)
cols = prediction[0].keys()
df = pd.DataFrame([[getattr(i,j) for j in cols] for i in prediction], columns = cols)
针对您的具体情况:
import pandas as pd
prediction = model.predict(test_x)
df = pd.DataFrame(prediction, columns = ['ouput'])
你用的是什么模块<代码>模型告诉我们的不多。从sklearn.employ导入RandomForestClassifier as rf您检查了吗?您实际尝试了什么?它给出了错误-'numpy.int64'对象没有属性'keys',乐意帮助:)