V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
songdg
V2EX  ›  Python

求解析一下这个函数的原理

  •  1
     
  •   songdg · 2018-10-16 09:30:31 +08:00 · 1771 次点击
    这是一个创建于 1990 天前的主题,其中的信息可能已经有所发展或是发生改变。
    在 stackoverflow 里找到一个 numpy 版指数移动平均的函数,但实在看不懂,还有当 window 少于等于 7 时不能得到正确的结果。
    def numpy_ewma_vectorized_v2(data, window):
    alpha = 2 /(window + 1.0)
    alpha_rev = 1-alpha
    n = data.shape[0]
    pows = alpha_rev**(np.arange(n+1))
    scale_arr = 1/pows[:-1]
    offset = data[0]*pows[1:]
    pw0 = alpha*alpha_rev**(n-1)
    mult = data*pw0*scale_arr
    cumsums = mult.cumsum()
    out = offset + cumsums*scale_arr[::-1]
    return out
    3 条回复    2018-10-16 10:48:40 +08:00
    yuankui
        1
    yuankui  
       2018-10-16 10:22:05 +08:00
    代码这么乱, 没有看下去的欲望。。
    Justin13
        2
    Justin13  
       2018-10-16 10:48:00 +08:00 via Android
    类似这种问题,应该先看数学原理。而不是上来看代码,看到懵逼。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2834 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 00:27 · PVG 08:27 · LAX 17:27 · JFK 20:27
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.