Python实现滑动平均(Moving Average)的例子


Posted in Python onAugust 24, 2019

Python中滑动平均算法(Moving Average)方案:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import numpy as np

# 等同于MATLAB中的smooth函数,但是平滑窗口必须为奇数。

# yy = smooth(y) smooths the data in the column vector y ..
# The first 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:原始数据,NumPy 1-D array containing the data to be smoothed
  # 必须是1-D的,如果不是,请使用 np.ravel()或者np.squeeze()转化 
  # WSZ: smoothing window size needs, which must be 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,边缘处理的不好

"""
def movingaverage(data, window_size):
  window = np.ones(int(window_size))/float(window_size)
  return np.convolve(data, window, 'same')
"""

# another one,速度更快
# 输出结果 不与原始数据等长,假设原数据为m,平滑步长为t,则输出数据为m-t+1

"""
def movingaverage(data, window_size):
  cumsum_vec = np.cumsum(np.insert(data, 0, 0)) 
  ma_vec = (cumsum_vec[window_size:] - cumsum_vec[:-window_size]) / window_size
  return ma_vec
"""

以上这篇Python实现滑动平均(Moving Average)的例子就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python中SOAP项目的介绍及其在web开发中的应用
Apr 14 Python
Python中的匿名函数使用简介
Apr 27 Python
Python fileinput模块使用实例
May 28 Python
浅谈python中截取字符函数strip,lstrip,rstrip
Jul 17 Python
Python之两种模式的生产者消费者模型详解
Oct 26 Python
Python设计模式之享元模式原理与用法实例分析
Jan 11 Python
Python图片处理模块PIL操作方法(pillow)
Apr 07 Python
Python3操作YAML文件格式方法解析
Apr 10 Python
利用python控制Autocad:pyautocad方式
Jun 01 Python
Django QuerySet查询集原理及代码实例
Jun 13 Python
python的json包位置及用法总结
Jun 21 Python
有趣的二维码:使用MyQR和qrcode来制作二维码
May 10 Python
浅谈Python3 numpy.ptp()最大值与最小值的差
Aug 24 #Python
在Python3 numpy中mean和average的区别详解
Aug 24 #Python
Python绘制股票移动均线的实例
Aug 24 #Python
python+selenium 鼠标事件操作方法
Aug 24 #Python
python+selenium select下拉选择框定位处理方法
Aug 24 #Python
Python封装成可带参数的EXE安装包实例
Aug 24 #Python
python识别文字(基于tesseract)代码实例
Aug 24 #Python
You might like
用PHP 4.2书写安全的脚本
2006/10/09 PHP
PHP解析RSS的方法
2015/03/05 PHP
基于PHP实现用户注册登录功能
2016/10/14 PHP
CSS和Javascript简单复习资料
2010/06/29 Javascript
nodejs npm package.json中文文档
2014/09/04 NodeJs
JavaScript实现生成GUID(全局统一标识符)
2014/09/05 Javascript
Backbone.js 0.9.2 源码注释中文翻译版
2015/06/25 Javascript
jquery实现实时改变网页字体大小、字体背景色和颜色的方法
2015/08/05 Javascript
canvas绘制多边形
2017/02/24 Javascript
js实现鼠标拖拽多选功能示例
2017/08/01 Javascript
详解ES6之async+await 同步/异步方案
2017/09/19 Javascript
jQuery.Sumoselect插件实现下拉复选框效果
2017/11/09 jQuery
three.js中文文档学习之通过模块导入
2017/11/20 Javascript
webstorm中vue语法的支持详解
2018/05/09 Javascript
Vue 中 filter 与 computed 的区别与用法解析
2019/11/21 Javascript
基于ant design日期控件使用_仅月份的操作
2020/10/27 Javascript
centos 下面安装python2.7 +pip +mysqld
2014/11/18 Python
Python 使用with上下文实现计时功能
2018/03/09 Python
Python删除n行后的其他行方法
2019/01/28 Python
Python发展史及网络爬虫
2019/06/19 Python
Python之虚拟环境virtualenv,pipreqs生成项目依赖第三方包的方法
2019/07/23 Python
Python PO设计模式的具体使用
2019/08/16 Python
python3 动态模块导入与全局变量使用实例
2019/12/22 Python
Django 解决由save方法引发的错误
2020/05/21 Python
Python如何获取文件指定行的内容
2020/05/27 Python
解决pyinstaller打包运行程序时出现缺少plotly库问题
2020/06/02 Python
HTML5超文本标记语言的实现方法
2020/09/24 HTML / CSS
运动服饰每月订阅盒:Ellie
2018/04/29 全球购物
Bailey帽子官方商店:Bailey Hats
2018/09/25 全球购物
大门门卫岗位职责
2013/11/30 职场文书
八一慰问活动方案
2014/02/07 职场文书
路政管理毕业自荐书范文
2014/02/10 职场文书
交通文明倡议书
2014/05/16 职场文书
预备党员个人总结
2015/02/14 职场文书
优秀党员先进事迹材料2016
2016/02/29 职场文书
Oracle 触发器trigger使用案例
2022/02/24 Oracle