開始使用免費開始

計算時間序列之間的相關性

相關係數可用來判斷多個變數(或一組時間序列)彼此之間的關聯程度。其結果是一個相關矩陣,用來描述各時間序列之間的相關性。請注意,相關矩陣的對角線值永遠是 1,因為一個時間序列與自身的相關性必然是完全相關。

相關係數可以用 pearson、kendall 與 spearman 方法來計算。本課程不詳述這些方法的理論差異;一般而言,當你認為變數間關係為線性時,應使用 pearson 方法;若變數間關係被認為是非線性,則應使用 kendall 或 spearman 方法。

本練習屬於課程

使用 Python 視覺化時間序列資料

檢視課程

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Compute the correlation between the beef and pork columns using the spearman method
print(meat[['beef', 'pork']].corr(method=____))

# Print the correlation between beef and pork columns
print(____)
編輯並執行程式碼