开始使用免费开始使用

多条时间序列之间的相关性

在上一个练习中,您提取了 jobs 数据框中每条时间序列的 seasonal 分量,并将结果保存到名为 seasonality_df 的新数据框中。在就业数据的语境下,对比季节性行为很有意义,这有助于发现哪些行业最相似,哪些差异最大。

我们可以利用 seasonality_df 数据框,计算数据集中各条时间序列两两之间的相关性来实现这一点。在本练习中,您将运用第 4 章学到的内容,计算相关性并创建 seasonality_df 数据框中时间序列相关性的聚类热图可视化。

本练习是课程的一部分

用 Python 可视化时间序列数据

查看课程

练习说明

  • 使用 Spearman 方法计算 seasonality_df 数据框所有列之间的相关性,并将结果赋给 seasonality_corr
  • 基于该相关性矩阵创建一个新的聚类热图。
  • 打印 Government 与 Education & Health 行业的季节性之间的相关系数值。

交互式实操练习

通过完成这段示例代码来试试这个练习。

# Get correlation matrix of the seasonality_df DataFrame
seasonality_corr = ____

# Customize the clustermap of the seasonality_corr correlation matrix
fig = ____(____, annot=True, annot_kws={"size": 4}, linewidths=.4, figsize=(15, 10))
plt.setp(fig.ax_heatmap.yaxis.get_majorticklabels(), rotation=0)
plt.setp(fig.ax_heatmap.xaxis.get_majorticklabels(), rotation=90)
plt.show()

# Print the correlation between the seasonalities of the Government and Education & Health industries
print(____)
编辑并运行代码