将预测结果保存为 CSV [英] Saving prediction results to CSV
本文介绍了将预测结果保存为 CSV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在将 sklearn 回归模型的结果存储到变量预测中.
I am storing the results from a sklearn regression model to the varibla prediction.
prediction = regressor.predict(data[['X']])
print(prediction)
预测输出的值看起来像这样
The values of the prediction output looks like this
[ 266.77832991 201.06347505 446.00066136 499.76736079 295.15519906
214.50514991 422.1043505 531.13126879 287.68760191 201.06347505
402.68859792 478.85808879 286.19408248 192.10235848]
然后我尝试使用 to_csv 函数将结果保存到本地 CSV 文件:
I am then trying to use the to_csv function to save the results to a local CSV file:
prediction.to_csv('C:/localpath/test.csv')
但我返回的错误是:
AttributeError: 'numpy.ndarray' object has no attribute 'to_csv'
我正在使用 Pandas/Numpy/SKlearn.关于基本修复的任何想法?
I am using Pandas/Numpy/SKlearn. Any idea on the basic fix?
推荐答案
您可以使用 Pandas.如前所述,numpy 数组没有 to_csv 函数.
You can use pandas. As it's said, numpy arrays don't have a to_csv function.
import numpy as np
import pandas as pd
prediction = pd.DataFrame(predictions, columns=['predictions']).to_csv('prediction.csv')
如果您希望您的值成行或列状,请添加.T".
add ".T" if you want either your values in line or column-like.
这篇关于将预测结果保存为 CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文