geekdoc-python-zh/docs/askpython/density-plots-in-python.md

6.6 KiB
Raw Permalink Blame History

Python 中的密度图–全面概述

原文:https://www.askpython.com/python/examples/density-plots-in-python

密度图用于显示数据集中连续数值变量的分布。它也被称为核密度图。

在开始应用任何机器学习技术之前,很好地了解你的数据是一个很好的做法。

作为一名优秀的 ML 从业者,我们应该问一些问题,如:

  • 我们的数据是什么样的?
  • 它是正态分布还是有一些不同的形状?
  • 我们打算应用于数据的算法,是否有任何关于数据分布的潜在假设?

在我们获得数据后立即解决这些问题可以极大地改善后续阶段的结果,并为我们节省大量时间。

直方图和密度图这样的图为我们提供了回答上述问题的方法。

为什么要在了解密度图之前了解直方图?

密度图非常类似于直方图。我们用直方图来显示分布的形状。直方图可以通过对数据进行宁滨处理并保存每个箱中的观察数量的计数来创建。在直方图中y 轴通常表示容器计数,但也可以用每单位计数表示,也称为密度。

A Histogram With Less Number Of Bins

A Histogram With Less Number Of Bins

如果我们增加直方图中的箱数,分布的形状看起来会更平滑。

Histogram Having More Number Of Bins

Histogram Having More Number Of Bins

现在,想象一条平滑连续的线穿过每个箱的顶部,创建我们的分布形状的轮廓。我们得到的结果就是我们所说的密度图。

Density Plot With Histogram

Density Plot With Histogram

理解密度图

我们可以把密度图想象成平滑的直方图,这很直观。密度图大多使用一个ernel 密度估计值 。核密度估计通过平滑噪声允许更平滑的分布。

密度图不受仓数量的影响,仓数量是考虑直方图时的主要参数,因此允许我们更好地可视化数据的分布。

因此,总的来说,它就像一个直方图,但有一条平滑的曲线穿过每个箱的顶部。

在野外存在几种分布形式。我们很可能会遇到的一些最常见的形状是:

Some Shapes Of Distributions

Some Shapes Of Distributions

使用 Python 绘制密度图

我们可以使用 python 以多种方式绘制密度图。我们来看几个常用的方法。

1.使用 Python scipy.stats 模块

scipy.stats模块为我们提供了gaussian_kde类来找出给定数据的密度。

import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import gaussian_kde

data = np.random.normal(10,3,100) # Generate Data
density = gaussian_kde(data)

x_vals = np.linspace(0,20,200) # Specifying the limits of our data
density.covariance_factor = lambda : .5 #Smoothing parameter

density._compute_covariance()
plt.plot(x_vals,density(x_vals))
plt.show()

Density Plot Using Scipy

Density Plot Using Scipy

我们改变gaussian_kde类的函数covariance_factor并传递不同的值以获得更平滑的图形。改变功能后记得打电话给_compute_covariance

2.使用 Seaborn kdeplot模块

Seaborn module 为我们提供了一种更简单、更灵活的方式来执行上述任务。

import numpy as np
import seaborn as sb
import matplotlib.pyplot as plt

data = np.random.normal(10,3,300) #Generating data.
plt.figure(figsize = (5,5))
sb.kdeplot(data , bw = 0.5 , fill = True)
plt.show()

Density Plot Using Kdeplot

Density Plot Using Kdeplot

Seaborn kdeplot需要一个单变量数据数组或一个 pandas 系列对象作为它的输入参数。bw参数相当于上面演示的gaussian_kde类的covariance_factor。我们可以让fill = False不用颜色填充曲线下的区域,而是简单地绘制一条曲线。

3.使用熊猫绘图功能

Pandas plot方法也可以通过提供kind = 'density'作为输入参数来绘制密度图。

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

x_values = np.random.random(10,3,300) #Generating Data
df = pd.DataFrame(x_values, columns = ['var_name'] ) #Converting array to pandas DataFrame
df.plot(kind = 'density)

Density Plot Using Pandas

Density Plot Using Pandas

4。使用 Seaborn distplot

我们还可以使用 seaborn distplot方法来可视化连续数值数据的分布。seaborn.distplot( )方法需要一个单变量数据变量作为输入参数,它可以是 pandas 系列、1d 数组或列表。

我们可以传递给seaborn.distplot( )的一些重要参数来根据我们的需要调整情节:

  • hist:(TypeBool)是否绘制直方图。
  • kde:(*TypeBool)*是否绘制高斯核密度估计。
  • **bins**:(TypeNumber)指定直方图中面元的数量。
  • hist_kws:(TypeDict)关键字参数的 Dict 为matplotlib . axes . axes . hist()
  • kde_kws:(TypeDict)关键字参数为 kdeplot() 作为字典传递。
***import numpy as np
import matplotlib.pyplot as plt
import seaborn as sb

data = np.random.normal(10, 3, 1000) #Generating data randomly from a normal distribution.

sb.set_style("whitegrid")  # Setting style(Optional)
plt.figure(figsize = (10,5)) #Specify the size of figure we want(Optional)
sb.distplot(x = data  ,  bins = 10 , kde = True , color = 'teal'\
             , kde_kws=dict(linewidth = 4 , color = 'black'))
plt.show()*** 

***Density Plot Using Distplot 1

Density Plot Using Seaborn distplot***

要了解更多关于 seaborn 的信息,你可以参考这篇关于 seaborn Distplots 的文章。

结论

这就把我们带到了文章的结尾!我们希望你今天已经学到了很多关于不同密度图的知识。您可以阅读这些文章来了解更多关于我们在本文中使用的 PandasMatplotlib 库的信息。