📜  如何在Python中使用滚动平均值制作时间序列图?

📅  最后修改于: 2022-05-13 01:54:48.424000             🧑  作者: Mango

如何在Python中使用滚动平均值制作时间序列图?

时间序列图用于观察一段时间内数据集中的各种趋势。在此类问题中,数据按时间排序,并且可以根据数据集中考虑的时间单位(日、月、秒、小时等)波动。在绘制时间序列数据时,这些波动可能会阻止我们清楚地了解图中的波峰和波谷。所以为了清楚地从数据中获取价值,我们使用滚动平均概念来制作时间序列图。

滚动平均值或移动平均值是最后 'n' 个值的简单平均值。它可以帮助我们找到原本难以察觉的趋势。此外,它们还可用于确定长期趋势。您可以通过将先前的“n”值相加并将它们除以“n”本身来简单地计算滚动平均值。但为此,滚动平均值的前 (n-1) 个值将是 Nan。

在本文中,我们将学习如何使用 Pandas 和 Seaborn 库在Python中制作具有滚动平均值的时间序列图。以下是使用 Pandas 计算滚动平均值的语法。

我们将使用“每日女性出生数据集”。该数据集描述了 1959 年加利福尼亚州每日女性出生的数量。从 01-01-1959 到 31-12-1959 有 365 次观察。您可以从此链接下载数据集。

让我们逐步实施:

第 1 步:导入库。

Python3
# import the libraries
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt


Python3
# import the dataset
data = pd.read_csv( "https://raw.githubusercontent.com/jbrownlee/ \
                   Datasets/master/daily-total-female-births.csv")
  
#view the dataset
display( data.head())


Python3
# set figure size
plt.figure( figsize = ( 12, 5))
  
# plot a simple time series plot
# using seaborn.lineplot()
sns.lineplot( x = 'Date',
             y = 'Births',
             data = data,
             label = 'DailyBirths')
  
plt.xlabel( 'Months of the year 1959')
  
# setting customized ticklabels for x axis
pos = [ '1959-01-01', '1959-02-01', '1959-03-01', '1959-04-01', 
       '1959-05-01', '1959-06-01', '1959-07-01', '1959-08-01',
       '1959-09-01', '1959-10-01', '1959-11-01', '1959-12-01']
  
lab = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'June', 
       'July', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec']
  
plt.xticks( pos, lab)
  
plt.ylabel('Female Births')


Python3
# computing a 7 day rolling average
data[ '7day_rolling_avg' ] = data.Births.rolling( 7).mean()
  
# viewing the dataset
Display(data.head(10))


Python3
# set figure size
plt.figure( figsize = ( 12, 5))
  
# plot a simple time series plot
# using seaborn.lineplot()
sns.lineplot( x = 'Date',
             y = 'Births',
             data = data,
             label = 'DailyBirths')
  
# plot using rolling average
sns.lineplot( x = 'Date',
             y = '7day_rolling_avg',
             data = data,
             label = 'Rollingavg')
  
plt.xlabel('Months of the year 1959')
  
# setting customized ticklabels for x axis
pos = [ '1959-01-01', '1959-02-01', '1959-03-01', '1959-04-01', 
       '1959-05-01', '1959-06-01', '1959-07-01', '1959-08-01',
       '1959-09-01', '1959-10-01', '1959-11-01', '1959-12-01']
  
lab = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'June', 
       'July', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec']
  
plt.xticks( pos, lab)
  
plt.ylabel('Female Births')


第 2 步:导入数据集

蟒蛇3

# import the dataset
data = pd.read_csv( "https://raw.githubusercontent.com/jbrownlee/ \
                   Datasets/master/daily-total-female-births.csv")
  
#view the dataset
display( data.head())

输出:

第 3 步:使用 seaborn.lineplot() 绘制一个简单的时间序列图

蟒蛇3

# set figure size
plt.figure( figsize = ( 12, 5))
  
# plot a simple time series plot
# using seaborn.lineplot()
sns.lineplot( x = 'Date',
             y = 'Births',
             data = data,
             label = 'DailyBirths')
  
plt.xlabel( 'Months of the year 1959')
  
# setting customized ticklabels for x axis
pos = [ '1959-01-01', '1959-02-01', '1959-03-01', '1959-04-01', 
       '1959-05-01', '1959-06-01', '1959-07-01', '1959-08-01',
       '1959-09-01', '1959-10-01', '1959-11-01', '1959-12-01']
  
lab = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'June', 
       'July', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec']
  
plt.xticks( pos, lab)
  
plt.ylabel('Female Births')

输出:

我们可以注意到,由于数据波动很大,因此很难从上图中获取知识。所以,让我们再次绘制它,但这次使用滚动平均概念。

第 4 步:使用 pandas.DataFrame.rolling.mean() 计算滚动平均值。

对于滚动平均,我们必须取一定的窗口大小。在这里,我们采用了窗口大小 = 7,即 7 天或 1 周的滚动平均值。

蟒蛇3

# computing a 7 day rolling average
data[ '7day_rolling_avg' ] = data.Births.rolling( 7).mean()
  
# viewing the dataset
Display(data.head(10))

输出:

我们可以观察到 '7day_rolling_avg' 列的前 6 个值是 NaN 值。这是因为这 6 个值没有足够的数据来计算 7 天的滚动平均值。因此,在图中,对于前六个值,也不会绘制任何值。

第 5 步:使用第 4 步中计算的滚动平均值绘制时间序列图

蟒蛇3

# set figure size
plt.figure( figsize = ( 12, 5))
  
# plot a simple time series plot
# using seaborn.lineplot()
sns.lineplot( x = 'Date',
             y = 'Births',
             data = data,
             label = 'DailyBirths')
  
# plot using rolling average
sns.lineplot( x = 'Date',
             y = '7day_rolling_avg',
             data = data,
             label = 'Rollingavg')
  
plt.xlabel('Months of the year 1959')
  
# setting customized ticklabels for x axis
pos = [ '1959-01-01', '1959-02-01', '1959-03-01', '1959-04-01', 
       '1959-05-01', '1959-06-01', '1959-07-01', '1959-08-01',
       '1959-09-01', '1959-10-01', '1959-11-01', '1959-12-01']
  
lab = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'June', 
       'July', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec']
  
plt.xticks( pos, lab)
  
plt.ylabel('Female Births')

输出:

通过上图我们可以清楚地看到,滚动平均已经平滑了女性出生的数量,我们可以更明显地注意到高峰。