简体   繁体   English

(Python):使用二维数组移动平均

[英](Python): Moving average using 2D array

I have a 2D list and I want to calculate the moving average along the columns numbers.我有一个 2D 列表,我想计算沿列数的移动平均值。 I have the following code.我有以下代码。 Does anyone know a numpy method which returns a new 2D list with the moving average of all columns?有谁知道一个 numpy 方法,它返回一个新的 2D 列表,其中包含所有列的移动平均值?

v = [[1, 2, 3],
     [4, 5, 6],
     [7, 8, 9]]

print((v[0][0]+v[1][0])/2)

Returns退货

2.5 2.5

IIUC and you have no problem using pandas : IIUC 并且您使用pandas没有问题:

import pandas as pd

v = [[1, 2, 3],
     [4, 5, 6],
     [7, 8, 9]]

pd.DataFrame(v).rolling(3,axis=0, min_periods=1).mean().values

Output:输出:

array([[1. , 2. , 3. ],
       [2.5, 3.5, 4.5],
       [4. , 5. , 6. ]])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM