当前位置: 首页 > article >正文

用ACF和PACF计算出一堆数据的周期个数以及周期时长,数据分析python

具体步骤
1使用ACF和PACF:可以通过查看ACF图中的周期性峰值,找到数据中的周期性。如果ACF图在某个滞后期处出现显著的正相关峰值,并且这种模式在多个滞后周期中重复出现,这就是周期性信号的特征。而PACF则可以帮助确定延迟的直接影响。

2找周期数和周期长度:周期的时长可以通过ACF中第一个显著的峰值(排除滞后期为0时的峰值)来确定,而周期的个数则可以通过分析整个序列中的周期性重复次数来估计。

下面是一个使用 statsmodels 库来计算并绘制ACF和PACF,并分析周期的Python代码。

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from statsmodels.graphics.tsaplots import plot_acf, plot_pacf
from statsmodels.tsa.stattools import acf, pacf

# 生成模拟数据或导入真实数据
# 假设你的数据是一个时间序列 DataFrame 或 NumPy 数组
# data = pd.read_csv('your_data.csv')  # 你的真实数据
data = np.sin(np.linspace(0, 10 * np.pi, 500))  # 模拟数据

# 绘制ACF和PACF
fig, ax = plt.subplots(2, 1, figsize=(10, 8))

# ACF图
plot_acf(data, lags=50, ax=ax[0])
ax[0].set_title('Autocorrelation (ACF)')

# PACF图
plot_pacf(data, lags=50, ax=ax[1])
ax[1].set_title('Partial Autocorrelation (PACF)')

plt.tight_layout()
plt.show()

# 计算ACF和PACF值
acf_values = acf(data, nlags=50)
pacf_values = pacf(data, nlags=50)

# 寻找周期长度
def find_period(acf_values):
    # 查找第一个显著峰值的位置作为周期
    for lag in range(1, len(acf_values)):
        if acf_values[lag] > 0.5:  # 设定一个阈值,例如0.5,可以调整
            return lag
    return None

period = find_period(acf_values)
print(f"Detected period length: {period}")

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from statsmodels.graphics.tsaplots import plot_acf, plot_pacf
from statsmodels.tsa.stattools import acf, pacf

# 生成模拟数据或导入真实数据
data = np.sin(np.linspace(0, 20 * np.pi, 1000))  # 生成正弦波数据,假设有多个周期

# 绘制ACF和PACF
fig, ax = plt.subplots(2, 1, figsize=(10, 8))

# ACF图
plot_acf(data, lags=100, ax=ax[0])
ax[0].set_title('Autocorrelation (ACF)')

# PACF图
plot_pacf(data, lags=100, ax=ax[1])
ax[1].set_title('Partial Autocorrelation (PACF)')

plt.tight_layout()
plt.show()

# 计算ACF值
acf_values = acf(data, nlags=100)

# 寻找周期长度函数
def find_period(acf_values, threshold=0.5):
    # 查找第一个显著峰值的位置作为周期长度
    for lag in range(1, len(acf_values)):
        if acf_values[lag] > threshold:  # 使用阈值筛选显著峰值
            return lag
    return None

# 确定周期长度
period_length = find_period(acf_values)
print(f"Detected period length: {period_length}")

# 计算周期个数
if period_length:
    total_data_points = len(data)
    num_periods = total_data_points // period_length
    print(f"Detected number of periods: {num_periods}")
else:
    print("No significant period detected.")


http://www.kler.cn/news/293583.html

相关文章:

  • Linux系统练习笔记【完整版】
  • .NET/C#⾯试题汇总系列:⾯向对象
  • SpringBoot整合openApi
  • 数据分析的革命:Docker容器化在数据分析中的应用
  • ssm微信小程序校园失物招领论文源码调试讲解
  • 【HTML】置换元素(替换元素)
  • 什么是区块链?
  • vue3+ts项目import导入路径用@/报错找不到模块“@/components/也没有快捷提示
  • 小米红米系列机型 机型代码查询总目录 adb指令查询步骤
  • 【论文精读】SCINet-基于降采样和交互学习的时序卷积模型
  • 2024国赛数学建模ABC题思路模型
  • 软件测试学习笔记丨Pytest的使用
  • 【Pytorch实用教程】【分布式】torch.distributed.all_reduce用法详细介绍
  • 【.NET全栈】ASP.NET开发Web应用——LINQ技术
  • CentOS7.9下安装snmp
  • 目标检测-YOLOv6
  • Lenze伦茨E82ZBC, E82ZBB E82ZMBRB安装说明手测
  • 1-10 图像增强对比度 opencv树莓派4B 入门系列笔记
  • Swift知识点---RxSwift学习
  • Centos配置双网卡绑定(bond)
  • Apache Tomcat 6.0.45 下载、安装和配置教程
  • YOLO缺陷检测学习笔记(3)
  • 【西安交通大学】高等计算机网络与通信期末题(回忆版)
  • 单片机学习笔记
  • 在VMware中的centos stream 9上用packstack安装openstack的单机版
  • 用于资产定价的FAFA三因素模型的案例实现
  • 如何在VSCode中同时打开多个页面?
  • 适用于手机/相机/电脑的照片恢复应用程序
  • 结合Python与GUI实现比赛预测与游戏数据分析
  • 微信小程序显示后台文章副文本,图片和视频正常显示