在numpy

时间:2017-09-07 15:55:48

标签: python python-3.x numpy

我有一个数组,其中这是一个小样本。它重复测量5次,我想将这些五个块整理成一个新数组,其中每行五行现在是一行,给出五个初始行的平均值,中位数和标准差

data = 
[[1, 9, 66, 74, -0.274035]
[1, 9, 66, 74, -0.269245]
[1, 9, 66, 74, -0.271161]
[1, 9, 66, 74, -0.269245]
[1, 9, 66, 74, -0.266370]
[2, 10, 65, 73, 0.085277]
[2, 10, 65, 73, 0.086235]
[2, 10, 65, 73, 0.090068]
[2, 10, 65, 73, 0.087193]
[2, 10, 65, 73, 0.085277]

我想要做的是保留前4列的块中值的值,然后找到下一列的平均值,中值和标准差,迭代地在五行块上工作。

data2 =
[[1, 9, 66, 74, mean[0:5,4], median[0:5,4], std[0:5,4]]
[2, 10, 65, 73, mean[5:10,4], median[5:10,4], std[5:10,4]]]

或用数字表示:

[[1, 9, 66, 74, -0.270011, -0.269245, 0.002528]
[2, 10, 65, 73, 0.08681, 0.086235, 0.001777]]

我试过这个,但只是得到零作为输出:

index.shape
Out[119]: (10,)

repeat = 5
a = 0
b = repeat
length = int((len(index) - repeat) / repeat)
meanVre = np.zeros(length)
for _ in range(length):
    np.append(meanVre, np.mean(data[a:b,5]))
    a = a+5
    b = b+5

(重复用作变量而不是5,因为块中的行数可能会在以后更改。)

您可以给予任何帮助。非常感谢。

1 个答案:

答案 0 :(得分:2)

def block_stats(data, blocksize = 5):
    inputs = data[::blocksize, :4]
    data_stat = data[:, 4].reshape(-1, blocksize)
    means = np.mean(data_stat, axis = 1, keepdims = 1)
    medians = np.median(data_stat, axis = 1, keepdims = 1)
    stds = np.std(data_stat, axis = 1, keepdims = 1)
    return np.vstack([inputs, means, medians, stds])