Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中获取model.predict值_Python_Output_Prediction - Fatal编程技术网

在python中获取model.predict值

在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,

我正在运行一个随机森林模型。我使用下面的代码预测值

预测=模型预测(测试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中执行此操作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',乐意帮助:)