python - numpy.genfromtxt 导入元组而不是数组

标签 python arrays csv numpy matrix

我正在尝试学习 Python 和 Numpy,所以请多多包涵。我正在使用 numpy.genfromtxt 将 CSV 文件导入矩阵。 CSV 如下所示:

Time(min),Nm,Speed,Power,Distance,Rpm,Bpm,interval,Altitude,Rate,Incline,Temp,PowerBalance,LeftTorqueEffectiveness,RightTorqueEffectiveness,getLeftPedalSmoothness,getRightPedalSmoothness,getCombinedPedalSmoothness,THb,SmO2,km
0.016666668,,4.3555064,0,0.002,0,118,1,684.3,0.0,0.0,14.71,50,-1.0,-1.0,-1.0,-1.0,-1.0,311.72,311.72
0.033333335,,4.3555064,20,0.002,0,119,1,684.3,0.0,0.0,14.71,50,-1.0,-1.0,-1.0,-1.0,-1.0,311.72,311.72
0.05,,4.444291,13,0.004,0,119,1,684.3,0.0,0.0,14.71,50,-1.0,-1.0,-1.0,-1.0,-1.0,311.72,311.72

现在我跑:

matrixCsv = np.genfromtxt(open(csvFile, "rb"), delimiter=',', \
                          missing_values=0,skip_header=1,dtype=float,\
                          usecols=(0,2,3,4,5,6,7,8,9,10,11,17),names=True)

我得到:

[ (0.033333335, 4.3555064, 20.0, 0.002, 0.0, 119.0, 1.0, 684.3, 0.0, 0.0, 14.71, -1.0)
(0.05, 4.444291, 13.0, 0.004, 0.0, 119.0, 1.0, 684.3, 0.0, 0.0, 14.71, -1.0)
(0.06666667, 4.4781966, 16.0, 0.006, 0.0, 120.0, 1.0, 684.3, 0.0, 0.0, 14.71, -1.0)
...,

在我看来,这就像封装在数组中的元组。但为什么是元组?我知道 numpy 数组/矩阵需要是同质的,并且 numpy 从非同质数据中生成元组。但为什么我的数据不均匀?我不明白...

最佳答案

您对如何使用 skip_headernames 感到困惑。读取数据并使用第一行作为变量名的正确方法是:

In [185]:

np.genfromtxt('temp.csv', delimiter=',', \
                          missing_values=0,skip_header=0,dtype=float,\
                          usecols=(0,2,3,4,5,6,7,8,9,10,11,17),names=True)
Out[185]:
array([ (0.016666668, 4.3555064, 0.0, 0.002, 0.0, 118.0, 1.0, 684.3, 0.0, 0.0, 14.71, -1.0),
       (0.033333335, 4.3555064, 20.0, 0.002, 0.0, 119.0, 1.0, 684.3, 0.0, 0.0, 14.71, -1.0),
       (0.05, 4.444291, 13.0, 0.004, 0.0, 119.0, 1.0, 684.3, 0.0, 0.0, 14.71, -1.0)], 
      dtype=[('Timemin', '<f8'), ('Speed', '<f8'), ('Power', '<f8'), ('Distance', '<f8'), ('Rpm', '<f8'), ('Bpm', '<f8'), ('interval', '<f8'), ('Altitude', '<f8'), ('Rate', '<f8'), ('Incline', '<f8'), ('Temp', '<f8'), ('getCombinedPedalSmoothness', '<f8')])

不是tuple数组,而是结构化数组skip_header=1 将导致使用第一行数据作为名称,这可能不是您想要的(看看您是如何丢失第一行数据的?)。

您也可以去掉名称并将数据读入普通的 numpy array

In [186]:

np.genfromtxt('temp.csv', delimiter=',', \
                          missing_values=0,skip_header=1,dtype=float,\
                          usecols=(0,2,3,4,5,6,7,8,9,10,11,17))
Out[186]:
array([[  1.66666680e-02,   4.35550640e+00,   0.00000000e+00,
          2.00000000e-03,   0.00000000e+00,   1.18000000e+02,
          1.00000000e+00,   6.84300000e+02,   0.00000000e+00,
          0.00000000e+00,   1.47100000e+01,  -1.00000000e+00],
       [  3.33333350e-02,   4.35550640e+00,   2.00000000e+01,
          2.00000000e-03,   0.00000000e+00,   1.19000000e+02,
          1.00000000e+00,   6.84300000e+02,   0.00000000e+00,
          0.00000000e+00,   1.47100000e+01,  -1.00000000e+00],
       [  5.00000000e-02,   4.44429100e+00,   1.30000000e+01,
          4.00000000e-03,   0.00000000e+00,   1.19000000e+02,
          1.00000000e+00,   6.84300000e+02,   0.00000000e+00,
          0.00000000e+00,   1.47100000e+01,  -1.00000000e+00]])

关于python - numpy.genfromtxt 导入元组而不是数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24109403/

相关文章:

java - 使用 bufferedreader 和 split 方法读取文件时出现问题

python - 可以一次 append 多个列表吗? (Python)

python - 在 python 中,如何确定局部范围内的变量值

python - 模糊匹配一列中的字符串并使用 fuzzywuzzy 创建新数据框

Ruby,在遍历数组时跳过项目?

java - 如何在 Java 中存储对数组?

python - 通过比较两个数组计算百分比误差

c++ - 使用 stringstream 和 ofstream 写长日志文件

python - 以简单且 Python 的方式将简单的字典写入 csv 文件?

php - National Instruments USB 6008 和 Linux