搜尋
首頁後端開發Python教學如何在Python中創建seaborn相關熱圖?

如何在Python中創建seaborn相關熱圖?

Aug 29, 2023 pm 08:09 PM
python創建seaborn

在資料集中,兩個變數對之間的相關性的強度和方向透過相關性熱圖進行圖形化展示,該圖展示了相關矩陣。這是一種在大規模資料集中尋找模式和連接的有效技術。

Python資料視覺化工具Seaborn提供了簡單的工具來產生統計視覺化圖形。使用者可以透過其創建相關熱圖的功能快速查看資料集的相關矩陣。

我們必須匯入資料集,計算變數的相關矩陣,然後使用 Seaborn 熱圖函數產生熱圖來建立相關熱圖。熱圖顯示一個矩陣,其顏色表示變數之間的相關程度。此外,使用者也可以在熱圖上顯示相關係數。

Seaborn 相關熱圖是一種有效的視覺化技術,用於檢查資料集中的模式和關係,可用於確定關鍵變數以進行進一步調查。

使用Heatmap()函數

heatmap函數產生一個顏色編碼的矩陣,用來說明資料集中兩對變數之間的相關性強度。 heatmap函數需要我們提供變數的相關矩陣,可以使用Pandas資料框的corr方法計算。 heatmap函數提供了許多可選選項,使用戶能夠修改熱圖的視覺效果,包括顏色方案、註釋、圖表大小和位置。

文法

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

上述函數中的參數data是表示輸入資料集的相關矩陣。用於著色熱力圖的顏色映射被稱為cmap。

Example 1

的中文翻譯為:

範例1

#在這個範例中,我們使用 Python 建立一個 seaborn 相關熱圖。首先,我們導入seaborn和matplotlib函式庫,並使用Seaborn的載入資料集函數載入iris資料集。此資料集包含 SepalLength、SepalWidth、PetalLength 和 PetalWidth 變數。鳶尾花資料集包括鳶尾花的萼片長度、萼片寬度、花瓣長度和花瓣寬度的測量。這是資訊的範例 -

的中文翻譯為: 的翻譯為: 翻譯成中文為:
序號 sepal_length sepal_width 花瓣長度 花瓣寬度 物種
0 5.1 3.53.5 1.4 0.2 絲滑
1 4.9 3.0 1.4 0.2 絲滑
2 4.7 3.2 1.3 0.2 絲滑
3 4.64.6 3.1 1.5 0.2 絲滑
4 5.05.0 #3.6 1.4 0.2 絲滑

使用者可以使用Seaborn的load dataset方法將鳶尾花資料集載入到Pandas DataFrame中。然後使用Pandas資料幀的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

如何在Python中創建seaborn相關熱圖?

範例 2

在這個範例中,我們再次使用Python建立一個seaborn相關性熱圖。首先,我們匯入seaborn和matplotlib函式庫,並使用Seaborn的load dataset函數載入鑽石資料集。鑽石資料集包括鑽石的成本和特徵的詳細信息,包括它們的克拉重量、切割、顏色和淨度。這是一個訊息的例子 −

的中文翻譯為:的中文翻譯為: 的翻譯為: 翻譯成中文為:的中文翻譯為:的中文翻譯為: 的中文翻譯為:的中文翻譯為:
序號 克拉 cutcut 顏色 清晰度 depth深度 價格 x y z
0 0.23 IdealIdeal E SI2 61.5 55.055.0 326 3.953.95 3.98 2.43
1 0.21 進階版 E SI1 59.8 61.0 326 3.89 3.84 2.31
2 0.23 E VS1 56.9 65.0 327 4.05 4.07 2.31
3 0.29 進階版 II VS2 62.462.4 58.0 334 4.20 4.23 2.63
4 0.31 J SI2 63.3 58.0 335 4.34 4.35 2.752.75

可以使用 Seaborn 的加载数据集函数将钻石数据集加载到 Pandas DataFrame 中。接下来,使用 Pandas 数据帧的 corr 方法,计算变量的相关矩阵并将其存储在名为 Diamond_corr_matrix 的变量中。为了利用不同的颜色来表示与函数的正相关和负相关,我们传递相关矩阵 corr 矩阵并将 cmap 选项设置为“coolwarm”。最后,我们使用 matplotlib 的 show 方法中的 pyplot 模块来显示热图。

# 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

如何在Python中創建seaborn相關熱圖?

热图是一种有益的图形表示形式,seaborn 使其变得简单易用。

以上是如何在Python中創建seaborn相關熱圖?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文轉載於:tutorialspoint。如有侵權,請聯絡admin@php.cn刪除
您如何切成python列表?您如何切成python列表?May 02, 2025 am 12:14 AM

SlicingaPythonlistisdoneusingthesyntaxlist[start:stop:step].Here'showitworks:1)Startistheindexofthefirstelementtoinclude.2)Stopistheindexofthefirstelementtoexclude.3)Stepistheincrementbetweenelements.It'susefulforextractingportionsoflistsandcanuseneg

在Numpy陣列上可以執行哪些常見操作?在Numpy陣列上可以執行哪些常見操作?May 02, 2025 am 12:09 AM

numpyallowsforvariousoperationsonArrays:1)basicarithmeticlikeaddition,減法,乘法和division; 2)evationAperationssuchasmatrixmultiplication; 3)element-wiseOperations wiseOperationswithOutexpliitloops; 4)

Python的數據分析中如何使用陣列?Python的數據分析中如何使用陣列?May 02, 2025 am 12:09 AM

Arresinpython,尤其是Throughnumpyandpandas,weessentialFordataAnalysis,offeringSpeedAndeffied.1)NumpyArseNable efflaysenable efficefliceHandlingAtaSetSetSetSetSetSetSetSetSetSetSetsetSetSetSetSetsopplexoperationslikemovingaverages.2)

列表的內存足跡與python數組的內存足跡相比如何?列表的內存足跡與python數組的內存足跡相比如何?May 02, 2025 am 12:08 AM

列表sandnumpyArraysInpythonHavedIfferentMemoryfootprints:listSaremoreFlexibleButlessMemory-效率,而alenumpyArraySareSareOptimizedFornumericalData.1)listsStorReereReereReereReereFerenceStoObjects,with withOverHeadeBheadaroundAroundaround64byty64-bitsysysysysysysysysyssyssyssyssysssyssys2)

部署可執行的Python腳本時,如何處理特定環境的配置?部署可執行的Python腳本時,如何處理特定環境的配置?May 02, 2025 am 12:07 AM

toensurepythonscriptsbehavecorrectlyacrycrosdevelvermations,分期和生產,USETHESTERTATE:1)Environment varriablesForsimplesettings,2)configurationfilesfilesForcomPlexSetups,3)dynamiCofforComplexSetups,dynamiqualloadingForaptaptibality.eachmethodoffersuniquebeneiquebeneqeniquebenefitsandrefitsandrequiresandrequiresandrequiresca

您如何切成python陣列?您如何切成python陣列?May 01, 2025 am 12:18 AM

Python列表切片的基本語法是list[start:stop:step]。 1.start是包含的第一個元素索引,2.stop是排除的第一個元素索引,3.step決定元素之間的步長。切片不僅用於提取數據,還可以修改和反轉列表。

在什麼情況下,列表的表現比數組表現更好?在什麼情況下,列表的表現比數組表現更好?May 01, 2025 am 12:06 AM

ListSoutPerformarRaysin:1)DynamicsizicsizingandFrequentInsertions/刪除,2)儲存的二聚體和3)MemoryFeliceFiceForceforseforsparsedata,butmayhaveslightperformancecostsinclentoperations。

如何將Python數組轉換為Python列表?如何將Python數組轉換為Python列表?May 01, 2025 am 12:05 AM

toConvertapythonarraytoalist,usEthelist()constructororageneratorexpression.1)intimpthearraymoduleandcreateanArray.2)USELIST(ARR)或[XFORXINARR] to ConconverTittoalist,請考慮performorefformanceandmemoryfformanceandmemoryfformienceforlargedAtasetset。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

VSCode Windows 64位元 下載

VSCode Windows 64位元 下載

微軟推出的免費、功能強大的一款IDE編輯器

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

Dreamweaver Mac版

Dreamweaver Mac版

視覺化網頁開發工具

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版