将预测结果保存为CSV [英] Saving prediction results to CSV

查看:530
本文介绍了将预测结果保存为CSV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将sklearn回归模型的结果存储到varibla预测中.

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?

推荐答案

您可以使用熊猫. 据说,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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆