【发布时间】:2014-09-12 13:10:33
【问题描述】:
我正在尝试使用以下命令将 .csv 文件导入我的 IPython 笔记本:
import csv
datafile_dC = open('/Users/iMacHome/dual-Core.csv', 'r')
datareader_dC = csv.reader(datafile_dC)
data_dC = []
for row in datareader_dC:
data_dC.append(row)
data_dC = np.array(data_dC)
print data_dC
x = data_dC[:, 6]
y = data_dC[:, 7]
打印数据产量(速记):
[['1.29940' '-0.06908' '0.85295' ..., '-0.83824' '-0.11374' '0.11374']
['0.93471' '0.10030' '1.25981' ..., '-1.47697' '0.02932' '-0.02932']
['0.57901' '0.25031' '1.77954' ..., '-1.28751' '0.23731' '-0.23731']
...,
['1.26506' '-0.21818' '0.60509' ..., '-1.15941' '-0.10211' '0.10211']
['1.22592' '-0.39114' '0.40631' ..., '-1.71412' '-0.08846' '0.08846']
['2.13359' '-0.51551' '0.30513' ..., '-2.07729' '-0.32911' '0.32911']]
但是,使用这种方法*似乎会产生以下错误:
TypeError: unsupported operand type(s) for *: 'float' and 'numpy.ndarray'
调用以下内容时:
def myBoundary(x, y0=-2.163, slope=-1.7):
return (slope * x) + y0
y_boundary = myBoundary(x, y0=-2.163, slope=-1.7)
我知道这很简单,但如果有人能提出解决这个问题的方法,我将不胜感激。
注意 *手动将数据作为列表输入不会产生相同的错误。
【问题讨论】:
-
您应该将数据转换为浮点数,并将列表列表实际转换为 numpy 列表。
-
我该怎么做?
-
您知道
loadtxt(docs.scipy.org/doc/numpy/reference/generated/numpy.loadtxt.html) 和genfromtxt(docs.scipy.org/doc/numpy/reference/generated/…) 吗?
标签: python csv numpy matplotlib ipython-notebook