0

我正在使用 cumsum 函数来计算 csv 列(行)的累积总和。输入如下所示:

输入(平均_):

1   0.000053    0.004531    0.010447    0.051962
2   0.000589    0.004518    0.009801    0.052226
3   0.000083    0.004581    0.010362    0.052288
4   -0.000192   0.003726    0.011258    0.051094
5   0.000281    0.004078    0.01008     0.052156
6   0.000091    0.004028    0.009853    0.052301
7   0.000478    0.004298    0.009966    0.054803
8   0.00028     0.004267    0.010051    0.050296
9   0.000198    0.004766    0.010245    0.051754

但是这段代码:

acc_column = []
for row in average_:
    f_column = numpy.cumsum(row[1], dtype = float)
    acc_column.append(f_column)

不起作用并给出错误:

"TypeError: cannot perform accumulate with flexible type"

有什么想法我做错了什么吗?

4

2 回答 2

2

我敢打赌,您所拥有的(即使您没有向我们展示)是一个结构化数组,如下所示:

array([(1, 5.3e-05, 0.004531, 0.010447, 0.051962),
       (2, 0.000589, 0.004518, 0.009801, 0.052226),
       (3, 8.3e-05, 0.004581, 0.010362, 0.052288),
       (4, -0.000192, 0.003726, 0.011258, 0.051094),
       (5, 0.000281, 0.004078, 0.01008, 0.052156),
       (6, 9.1e-05, 0.004028, 0.009853, 0.052301),
       (7, 0.000478, 0.004298, 0.009966, 0.054803),
       (8, 0.00028, 0.004267, 0.010051, 0.050296),
       (9, 0.000198, 0.004766, 0.010245, 0.051754)], 
      dtype=[('f0', '<i8'), ('f1', '<f8'), ('f2', '<f8'), ('f3', '<f8'), ('f4', '<f8')])

如果没有,那么您可以从文件中加载它:

a = np.genfromtxt('tmp.txt', dtype=None)

一旦它全部在一个数组中,就不需要遍历每一行:
如果你想累积第 1 列,那么这实际上'f1'是结构化数组中的字段,所以使用:

>>> np.cumsum(a['f1'])
array([ 5.300e-05, 6.420e-04, 7.250e-04,
        5.330e-04, 8.140e-04, 9.050e-04,
        1.383e-03, 1.663e-03, 1.861e-03])

如果它是一个普通的浮点数组:

array([[  1.000e+00,   5.300e-05,   4.531e-03,   1.0447e-02,   5.1962e-02],
       [  2.000e+00,   5.890e-04,   4.518e-03,   9.8010e-03,   5.2226e-02],
       [  3.000e+00,   8.300e-05,   4.581e-03,   1.0362e-02,   5.2288e-02],
       [  4.000e+00,  -1.920e-04,   3.726e-03,   1.1258e-02,   5.1094e-02],
       [  5.000e+00,   2.810e-04,   4.078e-03,   1.0080e-02,   5.2156e-02],
       [  6.000e+00,   9.100e-05,   4.028e-03,   9.8530e-03,   5.2301e-02],
       [  7.000e+00,   4.780e-04,   4.298e-03,   9.9660e-03,   5.4803e-02],
       [  8.000e+00,   2.800e-04,   4.267e-03,   1.0051e-02,   5.0296e-02],
       [  9.000e+00,   1.980e-04,   4.766e-03,   1.0245e-02,   5.1754e-02]])

然后你所要做的就是cumsum在列上调用:

>>> np.cumsum(a[:,1])
array([ 5.300e-05,  6.420e-04,  7.250e-04,
        5.330e-04,  8.140e-04,  9.050e-04,
        1.383e-03,  1.663e-03,  1.861e-03])
于 2013-11-28T02:20:29.647 回答
1

看起来您的某些数据不正确dtype,试试这个(将其转换为float),它是否有效或提供另一个TypeError

f_column = numpy.cumsum(numpy.asarray(row[1], dtype=float))
于 2013-11-28T01:51:31.397 回答