我有一些带有一些数据的csv文件。我试图从csv文件中读取numpy数组,所以这里是程序的代码:
import numpy as np
train = csv.reader(open(sys.argv[1], 'r'))
X = []
y = []
for row in train:
X.append(row[1:])
y.append(row[0])
X = np.array(X)
y = np.array(y)
我知道Python语法很不寻常。那么有没有办法在类似的东西中编写转循环?
import numpy as np
train = csv.reader(open(sys.argv[1], 'r'))
X, y = [... for row in train]
答案 0 :(得分:2)
为什么不使用np.loadtxt
读取整个csv文件?
>>> from io import StringIO
>>> txt = '''
... 1, 2, 3
... 4, 5, 6
... 7, 8, 9'''
>>>
>>> xs = np.loadtxt(StringIO(txt), delimiter=',')
>>> xs
array([[ 1., 2., 3.],
[ 4., 5., 6.],
[ 7., 8., 9.]])
>>>
>>> x, y = xs[:, 1:], xs[:, 0]
>>> x
array([[ 2., 3.],
[ 5., 6.],
[ 8., 9.]])
>>> y
array([ 1., 4., 7.])