numpy - 邻居平均值数组

标签 numpy average

功能是什么func计算 n-1 -大小数组的后续元素的平均值数组 n (即窗口宽度为 2 的移动平均线)?

func(numpy.array([1,2,3,4,5]))
# return numpy.array([1.5, 2.5, 3.5, 4.5])

最佳答案

这里不需要函数:

import numpy as np

x = np.array([1,2,3,4,5])
x_f2 = 0.5*(x[1:] + x[:-1])

如果你想把它作为一个函数:
def window(x, n):
    return (x[(n-1):] + x[:-(n-1)])/float(n)

关于numpy - 邻居平均值数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10849054/

相关文章:

python - Numpy 数组 View 语法

python - 为什么图像值不会根据阈值改变

python - Pandas 处理 numpy timedelta64[ms]

mysql - SQL (MySQL) 统计每个类(class)一个人的平均数

google-sheets - 获取 Google Sheets 中最后 X 行的范围

python - Spyder、Python IDE 启动代码导致 GUI 崩溃

python - 为什么维度顺序会随着 boolean 索引而改变?

csv - 如何从 JMeter 生成的 csv 文件计算平均响应时间?

python - 列表中的平均时间增量

mysql - 将 MySQL 结果转换为 int (C++)