Python实现滑动平均(Moving Average)的例子
作者:Luke__Zhang 时间:2023-02-10 22:36:58
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
"""
来源:https://blog.csdn.net/Maverick_7/article/details/79102130
标签:Python,滑动平均,Moving,Average
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python高阶函数map() 简介和使用详解
2021-04-03 04:34:11
![](https://img.aspxhome.com/file/2023/1/128771_0s.png)
php 静态页面中显示动态内容
2023-11-18 22:09:22
python模块中pip命令的基本使用
2023-09-28 22:44:20
![](https://img.aspxhome.com/file/2023/0/100260_0s.png)
分享vim python缩进等一些配置
2022-09-28 00:12:55
Mysql json类型字段Java+Mybatis数据字典功能的实践方式
2024-01-22 00:24:34
![](https://img.aspxhome.com/file/2023/3/71243_0s.jpg)
浅析SpringBoot微服务中异步调用数据提交数据库的问题
2024-01-16 06:07:06
![](https://img.aspxhome.com/file/2023/6/76526_0s.png)
Python绘制地理图表可视化神器pyecharts
2021-01-22 18:08:58
![](https://img.aspxhome.com/file/2023/2/115752_0s.png)
Python中的类对象示例详解
2022-03-20 12:40:50
最具创意的广告牌全集
2007-09-21 19:54:00
![](https://img.aspxhome.com/file/UploadPic/20079/21/200792120756728s.jpg)
python实现五子棋小程序
2023-12-04 20:49:37
pytest配置文件pytest.ini的详细使用
2021-10-25 07:48:04
![](https://img.aspxhome.com/file/2023/3/133743_0s.png)
用函数模板,写一个简单高效的 JSON 查询器的方法介绍
2024-04-18 10:53:00
![](https://img.aspxhome.com/file/2023/4/136414_0s.png)
Gradio机器学习模型快速部署工具quickstart前篇
2023-07-01 15:07:51
![](https://img.aspxhome.com/file/2023/4/62844_0s.png)
wxPython使用系统剪切板的方法
2023-04-01 14:38:29
![](https://img.aspxhome.com/file/2023/7/109497_0s.jpg)
关于Python中的if __name__ == __main__详情
2022-05-04 10:16:28
![](https://img.aspxhome.com/file/2023/2/127322_0s.png)
Python 中对 XML 文件的编码转换问题
2022-08-19 12:46:53
![](https://img.aspxhome.com/file/2023/2/77862_0s.png)
python抓取网页中的图片示例
2022-02-24 20:07:22
GoLang中的timer定时器实现原理分析
2024-04-23 09:36:12
![](https://img.aspxhome.com/file/2023/6/134406_0s.png)
MySQL 实现lastInfdexOf的功能案例
2024-01-20 15:16:51
MySQL批量更新的四种方式总结
2024-01-13 17:53:08