使用Matplotlib和csv2rec设置数据类型 [英] Setting Data Types with Matplotlib and csv2rec
问题描述
我正在学习Matplotlib,并尝试手工实现简单的线性回归.但是,我在使用 csv2rec 后导入和处理数据时遇到了问题.
I'm learning Matplotlib, and trying to implement a simple linear regression by hand. However, I've run into a problem when importing and then working with my data after using csv2rec.
data= matplotlib.mlab.csv2rec('KC_Filtered01.csv',delimiter=',')
x = data['list_price']
y = data['square_feet']
sumx = x.sum()
sumy = y.sum()
sumxSQ = sum([sq**2 for sq in x])
sumySQ = sum([sq**2 for sq in y])
我正在阅读房价清单,并试图得出平方和.但是,当 csv2rec 从文件中读取价格时,它会将这些值存储为 int32.由于房价的平方和大于32位整数,所以溢出.但是,我没有看到更改 csv2rec 读取文件时分配的数据类型的方法.读入或分配数组时如何更改数据类型?
I'm reading in a list of housing prices, and trying to get the sum of the squares. However, when csv2rec reads in the prices from the file, it stores the values as an int32. Since the sum of the squares of the housing prices is greater than a 32 bit integer, it overflows. However I don't see a method of changing the data type that is assigned when csv2rec reads the file. How can I change the data type when the array is read in or assigned?
推荐答案
x = data['list_price'].astype('int64')
,与 y
相同.
And: csv2rec
has a converterd
argument: http://matplotlib.sourceforge.net/api/mlab_api.html#matplotlib.mlab.csv2rec
这篇关于使用Matplotlib和csv2rec设置数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!