推荐学习书目
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 · Oct 16, 2018 · 2254 views
    This topic created in 2777 days ago, the information mentioned may be changed or developed.
    在 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 replies    2018-10-16 10:48:40 +08:00
    yuankui
        1
    yuankui  
       Oct 16, 2018
    代码这么乱, 没有看下去的欲望。。
    Justin13
        2
    Justin13  
       Oct 16, 2018 via Android
    类似这种问题,应该先看数学原理。而不是上来看代码,看到懵逼。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   968 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 47ms · UTC 18:45 · PVG 02:45 · LAX 11:45 · JFK 14:45
    ♥ Do have faith in what you're doing.