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

具体步骤

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

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

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

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}")
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

# 生成模拟数据或导入真实数据
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.")
相关推荐
天天爱吃肉821837 分钟前
跟着创意天才周杰伦学新能源汽车研发测试!3年从工程师到领域专家的成长秘籍!
数据库·python·算法·分类·汽车
m0_715575341 小时前
使用PyTorch构建你的第一个神经网络
jvm·数据库·python
甄心爱学习1 小时前
【leetcode】判断平衡二叉树
python·算法·leetcode
深蓝电商API1 小时前
滑块验证码破解思路与常见绕过方法
爬虫·python
Ulyanov1 小时前
Pymunk物理引擎深度解析:从入门到实战的2D物理模拟全攻略
python·游戏开发·pygame·物理引擎·pymunk
sensen_kiss1 小时前
INT303 Coursework1 爬取影视网站数据(如何爬虫网站数据)
爬虫·python·学习
玄同7651 小时前
我的 Trae Skill 实践|使用 UV 工具一键搭建 Python 项目开发环境
开发语言·人工智能·python·langchain·uv·trae·vibe coding
Yorlen_Zhang2 小时前
Python Tkinter Text 控件完全指南:从基础编辑器到富文本应用
开发语言·python·c#
HAPPY酷2 小时前
C++ 和 Python 的“容器”对决:从万金油到核武器
开发语言·c++·python
gpfyyds6663 小时前
Python代码练习
开发语言·python