搜索
首页后端开发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阵列上可以执行哪些常见操作?Apr 26, 2025 am 12:22 AM

Pythonarrayssupportvariousoperations:1)Slicingextractssubsets,2)Appending/Extendingaddselements,3)Insertingplaceselementsatspecificpositions,4)Removingdeleteselements,5)Sorting/Reversingchangesorder,and6)Listcomprehensionscreatenewlistsbasedonexistin

在哪些类型的应用程序中,Numpy数组常用?在哪些类型的应用程序中,Numpy数组常用?Apr 26, 2025 am 12:13 AM

NumPyarraysareessentialforapplicationsrequiringefficientnumericalcomputationsanddatamanipulation.Theyarecrucialindatascience,machinelearning,physics,engineering,andfinanceduetotheirabilitytohandlelarge-scaledataefficiently.Forexample,infinancialanaly

您什么时候选择在Python中的列表上使用数组?您什么时候选择在Python中的列表上使用数组?Apr 26, 2025 am 12:12 AM

useanArray.ArarayoveralistinpythonwhendeAlingwithHomeSdata,performance-Caliticalcode,orinterFacingWithCcccode.1)同质性data:arrayssavememorywithtypedelements.2)绩效code-performance-clitionalcode-clitadialcode-critical-clitical-clitical-clitical-clitaine code:araysofferferbetterperperperformenterperformanceformanceformancefornalumericalicalialical.3)

所有列表操作是否由数组支持,反之亦然?为什么或为什么不呢?所有列表操作是否由数组支持,反之亦然?为什么或为什么不呢?Apr 26, 2025 am 12:05 AM

不,notalllistoperationsareSupportedByArrays,andviceversa.1)arraysdonotsupportdynamicoperationslikeappendorinsertwithoutresizing,wheremactssperformance.2)listssdonotguaranteeconeeconeconstanttanttanttanttanttanttanttanttimecomplecomecomecomplecomecomecomecomecomecomplecomectaccesslikearrikearraysodo。

您如何在python列表中访问元素?您如何在python列表中访问元素?Apr 26, 2025 am 12:03 AM

toAccesselementsInapythonlist,useIndIndexing,负索引,切片,口头化。1)indexingStartSat0.2)否定indexingAccessesessessessesfomtheend.3)slicingextractsportions.4)iterationerationUsistorationUsisturessoreTionsforloopsoreNumeratorseforeporloopsorenumerate.alwaysCheckListListListListlentePtotoVoidToavoIndexIndexIndexIndexIndexIndExerror。

Python的科学计算中如何使用阵列?Python的科学计算中如何使用阵列?Apr 25, 2025 am 12:28 AM

Arraysinpython,尤其是Vianumpy,ArecrucialInsCientificComputingfortheireftheireffertheireffertheirefferthe.1)Heasuedfornumerericalicerationalation,dataAnalysis和Machinelearning.2)Numpy'Simpy'Simpy'simplementIncressionSressirestrionsfasteroperoperoperationspasterationspasterationspasterationspasterationspasterationsthanpythonlists.3)inthanypythonlists.3)andAreseNableAblequick

您如何处理同一系统上的不同Python版本?您如何处理同一系统上的不同Python版本?Apr 25, 2025 am 12:24 AM

你可以通过使用pyenv、venv和Anaconda来管理不同的Python版本。1)使用pyenv管理多个Python版本:安装pyenv,设置全局和本地版本。2)使用venv创建虚拟环境以隔离项目依赖。3)使用Anaconda管理数据科学项目中的Python版本。4)保留系统Python用于系统级任务。通过这些工具和策略,你可以有效地管理不同版本的Python,确保项目顺利运行。

与标准Python阵列相比,使用Numpy数组的一些优点是什么?与标准Python阵列相比,使用Numpy数组的一些优点是什么?Apr 25, 2025 am 12:21 AM

numpyarrayshaveseveraladagesoverandastardandpythonarrays:1)基于基于duetoc的iMplation,2)2)他们的aremoremoremorymorymoremorymoremorymoremorymoremoremory,尤其是WithlargedAtasets和3)效率化,效率化,矢量化函数函数函数函数构成和稳定性构成和稳定性的操作,制造

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

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

MinGW - 适用于 Windows 的极简 GNU

MinGW - 适用于 Windows 的极简 GNU

这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

Atom编辑器mac版下载

Atom编辑器mac版下载

最流行的的开源编辑器

VSCode Windows 64位 下载

VSCode Windows 64位 下载

微软推出的免费、功能强大的一款IDE编辑器

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中