三种Python比较两个时间序列在图形上是否相似的方法分享
作者:Python实用宝典 时间:2023-04-26 20:57:12
比较两个时间序列在图形上是否相似,可以通过以下方法:
1.可视化比较:将两个时间序列绘制在同一张图上,并使用相同的比例和轴标签进行比较。可以观察它们的趋势、峰值和谷值等特征,从而进行比较。
3.峰值和谷值比较:通过比较两个时间序列中的峰值和谷值来进行比较。可以比较它们的幅度和位置。
3.相关性分析:计算两个时间序列之间的相关系数,从而确定它们是否存在线性关系。如果它们的相关系数接近1,则它们趋势相似。
4.非线性方法:使用非线性方法来比较两个时间序列,如动态时间规整、小波变换等。这些方法可以帮助捕捉两个时间序列之间的相似性。
需要注意的是,图形上的相似性并不能完全代表两个时间序列之间的相似性,因为同一个图形可以对应着不同的时间序列。因此,在进行时间序列的比较时,需要综合考虑多个方面的信息。
1.准备
开始之前,你要确保Python和pip已经成功安装在电脑上,如果没有,可以访问这篇文章:超详细Python安装指南 进行安装。
(可选1) 如果你用Python的目的是数据分析,可以直接安装Anaconda,它内置了Python和pip.
(可选2) 此外,推荐大家用VSCode编辑器,它有许多的优点
请选择以下任一种方式输入命令安装依赖:
1. Windows 环境 打开 Cmd (开始-运行-CMD)。
2. MacOS 环境 打开 Terminal (command+空格输入Terminal)。
3. 如果你用的是 VSCode编辑器 或 Pycharm,可以直接使用界面下方的Terminal.
pip install matplotlib
pip install numpy
2. 使用Matplotlib可视化比较两个时间序列
import matplotlib.pyplot as plt
# 生成时间序列数据
x = [1, 2, 3, 4, 5]
y1 = [10, 15, 13, 17, 20]
y2 = [8, 12, 14, 18, 22]
# 绘制两个时间序列的折线图
plt.plot(x, y1, label='y1')
plt.plot(x, y2, label='y2')
# 设置图形属性
plt.xlabel('Time')
plt.ylabel('Value')
plt.title('Comparison of two time series')
plt.legend()
# 显示图形
plt.show()
2. 计算两个时间序列的相关系数
import numpy as np
# 生成时间序列数据
x = [1, 2, 3, 4, 5]
y1 = [10, 15, 13, 17, 20]
y2 = [8, 12, 14, 18, 22]
# 计算相关系数
corr = np.corrcoef(y1, y2)[0, 1]
# 输出结果
print('Correlation coefficient:', corr)
3.使用Python实现动态时间规整算法(DTW)
import numpy as np
# 生成时间序列数据
x = [1, 2, 3, 4, 5]
y1 = [10, 15, 13, 17, 20]
y2 = [8, 12, 14, 18, 22]
# 动态时间规整算法
def dtw_distance(ts_a, ts_b, d=lambda x, y: abs(x - y)):
DTW = {}
# 初始化边界条件
for i in range(len(ts_a)):
DTW[(i, -1)] = float('inf')
for i in range(len(ts_b)):
DTW[(-1, i)] = float('inf')
DTW[(-1, -1)] = 0
# 计算DTW矩阵
for i in range(len(ts_a)):
for j in range(len(ts_b)):
cost = d(ts_a[i], ts_b[j])
DTW[(i, j)] = cost + min(DTW[(i-1, j)], DTW[(i, j-1)], DTW[(i-1, j-1)])
# 返回DTW距离
return DTW[len(ts_a)-1, len(ts_b)-1]
# 计算两个时间序列之间的DTW距离
dtw_dist = dtw_distance(y1, y2)
# 输出结果
print('DTW distance:', dtw_dist)
来源:https://mp.weixin.qq.com/s/0txJVclaJp5kSYO-qL-ONw
标签:Python,时间序列
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python使用Pygame绘制时钟
2022-08-04 14:10:07
![](https://img.aspxhome.com/file/2023/5/105625_0s.jpg)
如何基于python操作json文件获取内容
2023-03-30 15:48:04
判断 iframe 是否加载完成的完美方法
2009-09-24 13:35:00
微软补丁KB967723造成MySQL频繁无法连接
2009-11-03 14:28:00
Sqlserver 高并发和大数据存储方案
2024-01-17 22:45:56
![](https://img.aspxhome.com/file/2023/6/116266_0s.png)
VueJs 将接口用webpack代理到本地的方法
2024-06-05 15:30:50
![](https://img.aspxhome.com/file/2023/3/123133_0s.png)
使用gunicorn部署django项目的问题
2022-07-16 16:39:55
ASP调试方法图文教程
2008-01-26 19:32:00
![](https://img.aspxhome.com/file/UploadPic/20081/26/2008126195146249s.jpg)
Python学习之书写格式及变量命名
2023-08-23 14:53:08
详细介绍Ruby中的正则表达式
2023-12-03 19:44:46
![](https://img.aspxhome.com/file/2023/8/101268_0s.jpg)
python中的标准库html
2022-01-09 22:04:25
![](https://img.aspxhome.com/file/2023/0/121780_0s.png)
PHP基于cookie与session统计网站访问量并输出显示的方法
2023-11-15 08:38:52
Pyqt+matplotlib 实现实时画图案例
2022-01-06 12:52:23
![](https://img.aspxhome.com/file/2023/7/68387_0s.jpg)
MySql多表查询 事务及DCL
2024-01-29 04:55:28
像表格一样用DIV+CSS给网页布局
2008-10-18 15:45:00
python ctypes库2_指定参数类型和返回类型详解
2021-10-17 14:34:50
利用Python绘制多种风玫瑰图
2023-05-05 03:41:50
![](https://img.aspxhome.com/file/2023/6/99266_0s.png)
python Graham求凸包问题并画图操作
2023-06-01 12:37:00
用python爬取今日说法每期数据
2022-07-23 05:30:22
![](https://img.aspxhome.com/file/2023/6/133736_0s.jpg)
基于python爬取链家二手房信息代码示例
2022-09-01 18:36:25
![](https://img.aspxhome.com/file/2023/5/105085_0s.jpg)