Python enables sliding averaging (moving Average) __python

Source: Internet
Author: User

the sliding average algorithm in Python (moving Average) scenario:

#!/usr/bin/env python #-*-coding:utf-8-*-import NumPy as NP # is equivalent to the smooth function in MATLAB, but the smooth window must be odd.
# yy = Smooth (y) smooths the data in the column vector y. # The few elements of yy are given by # yy (1) = y (1) # yy (2) = (Y (1) + y (2) + y (3))/3 # yy (3) = (Y (1) + y (2) + y (3) + Y (4) + y (5))/5 # yy (4) = (Y (2) + y (3) + y (4) + y (5) + y (6))/5 # ... def smooth (A,WSZ): # A: Raw data, numpy 1-d array con  Taining the data to be smoothed # must be 1-d, if not, please use Np.ravel () or Np.squeeze () Convert # wsz:smoothing window size needs, Which must is odd number, # as in the original MATLAB implementation out0 = Np.convolve (A,np.ones (Wsz,dtype=int), ' Valid ')/wsz R = Np.arange (1,wsz-1,2) start = Np.cumsum (A[:wsz-1]) [:: 2]/r stop = (np.cumsum (a[:-wsz:-1)) [:: 2]/ R) [:: -1] Return Np.concatenate ((Start, out0, stop)) # Another one, edge handling not good "" "Def movingaverage (data, Window_si Ze): window = np.ones (int (window_size))/float (window_size) return np.convolve (Data, window, ' same' "" "# Another one, faster # The output is not equal to the original data, assuming the original data is M, and the smoothing step is T, then the output data is M-t+1" "" Def MovingAverage "(Data, window_size): CUMSU M_vec = Np.cumsum (np.insert (data, 0, 0)) Ma_vec = (cumsum_vec[window_size:]-cumsum_vec[:-window_size])/Window_siz E return Ma_vec "" "

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.