Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
func计算n-1大小数组的后续元素的平均大小数组n(即窗口宽度为 2 的移动平均值)的函数是什么?
func
n-1
n
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)
>>> x = np.array([1,2,3,4,5]) >>> np.vstack([x[1:], x[:-1]]).mean(axis=0)