Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在numpy内5行的重复块上重复计算_Python_Python 3.x_Numpy - Fatal编程技术网

Python 在numpy内5行的重复块上重复计算

Python 在numpy内5行的重复块上重复计算,python,python-3.x,numpy,Python,Python 3.x,Numpy,我有一个数组,这是一个小样本。它重复测量5次,我想把这些5行的数据块整理成一个新的数组,每个5行的数据块现在是一行,给出了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, 1

我有一个数组,这是一个小样本。它重复测量5次,我想把这些5行的数据块整理成一个新的数组,每个5行的数据块现在是一行,给出了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列的值,然后找到下一列的平均值、中值和标准偏差,在5行的块上迭代工作

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
(repeat用作变量,而不是5,因为块中的行数可能在以后更改)


如果您能提供任何帮助,我们将不胜感激。

因此,每个度量都有相同数量的条目?是的,没错,每个度量都有相同数量的条目,在这个文件中是5。未来的文件可能会有所不同,但这个文件它们都是五个。因此,每个度量都有完全相同的条目数?是的,没错,每个度量都有相同的条目数,在这个文件中是5。未来的文件可能会有所不同,但此文件共有五个
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])