在数据集中,两个变量对之间的相关性的强度和方向通过相关性热图进行图形化展示,该图展示了相关矩阵。这是一种在大规模数据集中寻找模式和连接的有效技术。
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.5 | 的中文翻译为:3.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.6 | 的翻译为:4.6 | 3.1 | 1.5 | 0.2 | 丝滑 |
4 | 5.0 | 翻译成中文为:5.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
示例 2
在这个示例中,我们再次使用Python创建一个seaborn相关性热图。首先,我们导入seaborn和matplotlib库,并使用Seaborn的load dataset函数加载钻石数据集。钻石数据集包括钻石的成本和特征的详细信息,包括它们的克拉重量、切割、颜色和净度。这是一个信息的例子 −
序号 | 克拉 | cut | 的中文翻译为:cut | 颜色 | 清晰度 | depth | 的中文翻译为:深度 | 表 | 价格 | x | y | z | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 0.23 | Ideal | 的翻译为:Ideal | E | SI2 | 61.5 | 55.0 | 翻译成中文为:55.0 | 326 | 3.95 | 的中文翻译为:3.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 | 高级版 | I | 的中文翻译为:I | VS2 | 62.4 | 的中文翻译为:62.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.75 | 的中文翻译为:2.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
热图是一种有益的图形表示形式,seaborn 使其变得简单易用。
以上是如何在Python中创建seaborn相关热图?的详细内容。更多信息请关注PHP中文网其他相关文章!

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

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

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

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

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

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

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

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


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

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

热门文章

热工具

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

Atom编辑器mac版下载
最流行的的开源编辑器

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器

SublimeText3 Linux新版
SublimeText3 Linux最新版

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