控制图时间序列数据集_如何创建具有不同模式的时间序列数据集(Python)

013584071eb399d7936bed4c45605099.png

时间序列是按时间顺序排列的一系列值。不管任何领域,我们都可能会遇到时间序列数据。典型的例子包括天气预报、汇率、销售数据、声波等。时间序列可以是表示为有序序列的任何类型的数据。

在这篇文章中,我们将创建不同模式的时间序列数据。合成数据集的一个优点是,我们可以测量机器学习模型的性能,并了解它在实际数据中的表现。

时间序列的常见模式包括:

  • 趋势:整体上升或下降的方向。
  • 季节性:以固定时间间隔重复的模式。
  • 白噪音:时间序列并不总是遵循某种模式(或包含季节性)。一些过程仅产生随机数据,这种时间序列称为白噪声。

注意:图像并非总是平滑的,通常会有一些噪音。此外,时间序列可能包括不同模式的组合。

我们将使用numpy来生成数值数组,并使用matplotlib来绘制序列。让我们从导入所需的Python库开始:

import numpy as npimport matplotlib.pyplot as plt%matplotlib inline

我们可以定义一个将数组作为输入并创建图形的Python函数:

def plot_time_series(time, values, label):    plt.figure(figsize=(10,6))    plt.plot(time, values)    plt.xlabel("Time", fontsize=20)    plt.ylabel("Value", fontsize=20)    plt.title(label, fontsize=20)    plt.grid(True)

时间序列趋势

这是最简单的一个,它是一个具有上升趋势的时间序列。我们创建一个具有时间和斜率的数组。然后将这些数组作为参数传递给我们的函数,Python代码如下:

time = np.arange(100)values = time*0.4plot_time_series(time, values, "Upward Trend")
c67a13130f10ec6aebe1d23f8de9d7f6.png

时间序列中的季节性

现在,我们可以绘制具有季节性的时间序列。我我们需要一个重复相同模式的序列。Python实现如下:

# Just a random patterntime = np.arange(50)values = np.where(time < 10, time**3, (time-9)**2)# Repeat the pattern 5 timesseasonal = []for i in range(5):    for j in range(50):        seasonal.append(values[j])# Plottime_seasonal = np.arange(250)plot_time_series(time_seasonal, seasonal, label="Seasonality")
085d9d6e32f17ce0a90aa8bda48a7fdb.png

这只是一个随机模式。您可以用numpy尝试不同的模式。

噪声

我们可以使用np.random.randn函数创建随机噪声。然后将该噪声添加到季节性序列中,Python实现如下:

noise = np.random.randn(250)*100seasonal += noisetime_seasonal = np.arange(250)plot_time_series(time_seasonal, seasonal, label="Seasonality with Noise")
b31861e610769d7701ae45dd52b37d05.png

多种模式组合

我们可以在时间序列中看到不同模式的组合。例如,下面的时间序列包含上升趋势和季节性,也含有一些噪声。

seasonal_upward = seasonal + np.arange(250)*10time_seasonal = np.arange(250)plot_time_series(time_seasonal, seasonal_upward, label="Seasonality + Upward Trend + Noise")
3d28b5aa54b6ce7e556b7416e9a7ceb2.png

白噪声

有些过程会产生不遵循任何模式的随机数据。这种时间序列被称为白噪声,很难分析和预测。让我们使用Python来创建一个白噪声示例:

time = np.arange(200)values = np.random.randn(200)*100plot_time_series(time, values, label="White Noise")
b27024ec446043de0dc76155e10077c4.png

非平稳时间序列

到目前为止,我们看到的时间序列总是遵循某种模式,这种时间序列称为平稳序列。让我们创建一个非平稳时间序列:

big_event = np.zeros(250)big_event[-50:] = np.arange(50)*-50non_stationary = seasonal_upward + big_eventtime_seasonal = np.arange(250)plot_time_series(time_seasonal, non_stationary, label="Non-stationary Time Series")
25f2a540d258165d49efa73e8f94ac35.png

最后

时间序列分析是数据科学领域的一个大领域,对时间序列分析的全面理解需要机器学习、统计知识以及领域专业知识。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值