如何在Python中建立Seaborn相關性熱力圖?


相關性熱力圖以圖形方式顯示資料集變數對之間相關性的強度和方向,它描繪了相關性矩陣。這是一種有效的方法,可以發現大型資料集中的模式和關係。

Python資料視覺化工具包Seaborn提供簡單的實用程式來生成統計圖形。其建立相關性熱力圖的功能使使用者可以快速檢視資料集的相關性矩陣。

為了構建相關性熱力圖,我們必須匯入資料集,計算變數的相關性矩陣,然後使用Seaborn的heatmap函式生成熱力圖。熱力圖顯示一個矩陣,顏色表示變數之間相關性的程度。此外,使用者可以在熱力圖上顯示相關係數。

Seaborn相關性熱力圖是檢查資料集模式和關係的有效視覺化技術,可用於識別需要進一步研究的關鍵變數。

使用Heatmap()函式

heatmap函式生成一個顏色編碼矩陣,該矩陣顯示資料集中的兩對變數之間相互關聯的強度。heatmap函式要求我們將變數的相關性矩陣提供給它,這可以使用Pandas資料框的corr方法來計算。heatmap函式提供廣泛的可選引數,使使用者能夠更改熱力圖的視覺外觀,包括顏色方案、註釋、繪圖大小和位置。

語法

import seaborn as sns
sns.heatmap(data, cmap=None, annot=None)

上述函式中的引數data是一個表示輸入資料集的相關性矩陣。cmap是用於為熱力圖著色的顏色圖。

示例1

在這個例子中,我們在Python中建立了一個seaborn相關性熱力圖。首先,我們匯入seaborn和matplotlib庫,並使用Seaborn的load_dataset函式載入iris資料集。該資料集包含SepalLength、SepalWidth、PetalLength和PetalWidth變數。iris資料集包含鳶尾花萼片長度、萼片寬度、花瓣長度和花瓣寬度的測量值。這是一個示例資訊:

序號 sepal_length sepal_width petal_length petal_width species
0 5.1 3.5 1.4 0.2 setosa
1 4.9 3.0 1.4 0.2 Setosa
2 4.7 3.2 1.3 0.2 Setosa
3 4.6 3.1 1.5 0.2 Setosa
4 5.0 3.6 1.4 0.2 setosa

使用者可以使用Seaborn的load_dataset方法將iris資料集載入到Pandas DataFrame中。然後使用Pandas DataFrame的corr方法計算變數的相關性矩陣,並將其儲存在名為corr_matrix的變數中。我們使用Seaborn的heatmap方法生成熱力圖。我們將相關性矩陣corr_matrix傳遞給函式,並將cmap引數設定為“coolwarm”,以使用不同的顏色來表示正相關和負相關。最後,我們使用matplotlib的pyplot模組的show方法顯示熱力圖。

# Required libraries 
import seaborn as sns
import matplotlib.pyplot as plt

# Load the iris dataset into a Pandas dataframe
iris_data = sns.load_dataset('iris')

# Creating the correlation matrix of the iris dataset
iris_corr_matrix = iris_data.corr()
print(iris_corr_matrix)

# Create the heatmap using the `heatmap` function of Seaborn
sns.heatmap(iris_corr_matrix, cmap='coolwarm', annot=True)

# Display the heatmap using the `show` method of the `pyplot` module from matplotlib.
plt.show()

輸出

              sepal_length  sepal_width  petal_length  petal_width
sepal_length      1.000000    -0.117570      0.871754     0.817941
sepal_width      -0.117570     1.000000     -0.428440    -0.366126
petal_length      0.871754    -0.428440      1.000000     0.962865
petal_width       0.817941    -0.366126      0.962865     1.000000

示例2

在這個例子中,我們再次在Python中建立一個seaborn相關性熱力圖。首先,我們匯入seaborn和matplotlib庫,並使用Seaborn的load_dataset函式載入diamonds資料集。diamonds資料集包含有關鑽石成本和特徵的詳細資訊,包括其克拉重量、切割、顏色和淨度。這是一個示例資訊:

序號 carat cut color clarity depth table price x y z
0 0.23 Ideal E SI2 61.5 55.0 326 3.95 3.98 2.43
1 0.21 Premium E SI1 59.8 61.0 326 3.89 3.84 2.31
2 0.23 Good E VS1 56.9 65.0 327 4.05 4.07 2.31
3 0.29 Premium I VS2 62.4 58.0 334 4.20 4.23 2.63
4 0.31 Good J SI2 63.3 58.0 335 4.34 4.35 2.75

可以使用Seaborn的load_dataset函式將diamonds資料集載入到Pandas DataFrame中。接下來,使用Pandas DataFrame的corr方法計算變數的相關性矩陣,並將其儲存在名為diamond_corr_matrix的變數中。為了使用不同的顏色來表示正相關和負相關,我們將相關性矩陣corr_matrix傳遞給函式,並將cmap引數設定為“coolwarm”。最後,我們使用matplotlib的pyplot模組的show方法顯示熱力圖。

# Required libraries 
import seaborn as sns
import matplotlib.pyplot as plt

# Load the diamond dataset into a Pandas dataframe
diamonds_data = sns.load_dataset('diamonds')

# Compute the correlation matrix of the variables
diamonds_corr_matrix = diamonds_data.corr()
print(diamonds_corr_matrix)

# Create the heatmap using the `heatmap` function of Seaborn
sns.heatmap(diamonds_corr_matrix, cmap='coolwarm', annot=True)

# Display the heatmap using the `show` method of the `pyplot` module from matplotlib.
plt.show()

輸出

          carat     depth     table     price         x         y         z
carat  1.000000  0.028224  0.181618  0.921591  0.975094  0.951722  0.953387
depth  0.028224  1.000000 -0.295779 -0.010647 -0.025289 -0.029341  0.094924
table  0.181618 -0.295779  1.000000  0.127134  0.195344  0.183760  0.150929
price  0.921591 -0.010647  0.127134  1.000000  0.884435  0.865421  0.861249
x      0.975094 -0.025289  0.195344  0.884435  1.000000  0.974701  0.970772
y      0.951722 -0.029341  0.183760  0.865421  0.974701  1.000000  0.952006
z      0.953387  0.094924  0.150929  0.861249  0.970772  0.952006  1.000000

熱力圖是一種有益的圖形表示,seaborn使其簡單易用。

更新於:2023年5月10日

8K+ 次瀏覽

啟動您的職業生涯

完成課程獲得認證

開始
廣告