用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.")
相关推荐
袁袁袁袁满15 分钟前
Haystack与亮数据MCP工具结合实现自动化爬虫
爬虫·python·网络爬虫·数据采集·爬虫实战·视频爬虫·特推爬虫
newbiai18 分钟前
2026马年春晚:火山引擎驱动AI新体验?
人工智能·python·火山引擎
小鸡吃米…1 小时前
TensorFlow 实现异或(XOR)运算
人工智能·python·tensorflow·neo4j
深蓝电商API1 小时前
Redis 作为爬虫去重与任务队列实战
爬虫·python
郝学胜-神的一滴1 小时前
FastAPI:Python 高性能 Web 框架的优雅之选
开发语言·前端·数据结构·python·算法·fastapi
柒.梧.1 小时前
Java位运算详解:原理、用法及实战场景(面试重点)
开发语言·数据库·python
Scott.W1 小时前
跟我学Easyi3C Tower Adapter Console(9)
人工智能·python·嵌入式硬件·i3c
多恩Stone2 小时前
【3D-AICG 系列-14】Trellis 2 的 Texturing Pipeline 保留单层薄壳,而 Textured GLB 会变成双层
人工智能·python·算法·3d·aigc
刘恒1234567892 小时前
Windows 电脑文件夹手动分类指南
java·windows·python·电脑·php
测试渣2 小时前
持续集成中的自动化测试框架优化实战指南
python·ci/cd·单元测试·自动化·pytest