龙空技术网

Python数据可视化 | 7、Seaborn如何让分布更直观

AI阅读和图谱 186

前言:

目前看官们对“python 分布函数”大概比较关注,同学们都需要剖析一些“python 分布函数”的相关资讯。那么小编同时在网上网罗了一些有关“python 分布函数””的相关内容,希望咱们能喜欢,小伙伴们快快来学习一下吧!

目录单变量分布灰度图核密度估计(KDE)模型参数拟合双变量分布散点图六角箱图核密度估计数据集中的两两关系小结

%matplotlib inlineimport numpy as npimport pandas as pdfrom scipy import stats, integratefrom warnings import filterwarningsfilterwarnings('ignore')import matplotlib as mplimport matplotlib.pyplot as pltimport seaborn as snssns.set(color_codes=True)np.random.seed(sum(map(ord, "distributions")))
单变量分布灰度图

最方便快捷的方式~

x = np.random.normal(size=100)sns.distplot(x, kde=True)# 核密度估计kde是默认为True的
# 想得到更精细的刻画?# 调节bins!sns.distplot(x, kde=False, bins=30)# bins=30 三十个柱子!
# 想配合着实例一起看?sns.distplot(x, kde=False, bins=30, rug=True)# rug 控制是否显示观测的小细条(边际毛毯)# Whether to draw a rugplot on the support axis.
配合着实例一起看有什么好处?答:指导你设置合适的bins。

注:上面的kde参数的开启与否是存在默认的带宽的,大概0.3左右。

核密度估计(KDE)

通过观测估计概率密度函数的形状。有什么用呢?待定系数法求概率密度函数~

核密度估计的步骤:

每一个观测附近用一个正态分布曲线近似叠加所有观测的正态分布曲线归一化

在seaborn中怎么画呢?

sns.kdeplot(x)
bandwidth 的概念:用于近似的正态分布曲线的宽度bandwidth 越大,曲线越平缓
sns.kdeplot(x, label = "bw: 'scott'")sns.kdeplot(x, bw=.2, label="bw: 0.2")sns.kdeplot(x, bw=2, label="bw: 2") # 过于平滑plt.legend()
模型参数拟合
x = np.random.gamma(6, size=200)            # 一个gamma分布sns.distplot(x,              kde=True,              fit=stats.gamma            )  # 我们尝试性的猜是gamma函数
蓝色线是 sns.distplot(x) 所绘制的结果黑色线是 sns.distplot(x, fit=stats.gamma) 所绘制的结果双变量分布
mean, cov = [0, 1], [(1, 0.5), (0.5, 1)]data = np.random.multivariate_normal(mean, cov, 200) # np.random.multivariate_normal() 多元正态分布,依据指定的均值和协方差生成数据# 均值分别为0和1,方差都是1,点与点之间还有0.5的相关系数df = pd.DataFrame(data, columns=["x", "y"])df.head()

两个相关的正态分布~

散点图

对于两个相关的分布,有牛逼的 sns.jointplot() 函数可以利用:

sns.jointplot(x="x", y="y", data=df).annotate(stats.pearsonr)

图中信息:x与y散点图/x和y灰度图/personr相关性系数/p value抽样误差(p越小越好)

关于皮尔逊相关系数(Pearson Correlation Coefficient)相关链接:Discussion of Similarity Metrics,zh.wikipedia.orgpearsonr相关系数计算:简单的相关系数的分类:0.8-1.0 极强相关0.6-0.8 强相关0.4-0.6 中等程度相关0.2-0.4 弱相关0.0-0.2 极弱相关或无相关Pearson相关系数游戏:六角箱图

x, y = np.random.multivariate_normal(mean, cov, 1000).Twith sns.axes_style("ticks"):    sns.jointplot(x=x, y=y, kind="hex").annotate(stats.pearsonr)    # 可以指定什么形状(hex六角形)# np.random.multivariate_normal(mean, cov, 10).T
核密度估计
# 等高线型sns.jointplot(x="x", y="y", data=df, kind="kde").annotate(stats.pearsonr)
f, ax = plt.subplots(figsize=(8, 8)) #  axessns.kdeplot(df.x, df.y, ax=ax, shade=False)# shade=False 不要填充,不然成等高线sns.rugplot(df.x, color="b", ax=ax) sns.rugplot(df.y, vertical=True, ax=ax, color="r") # sns.rugplot 专门画rug ; vertical 水平化

想看到更连续梦幻的效果~

f, ax = plt.subplots(figsize=(6, 6))# cubehelix颜色系统,亮度正比于强度,用于天文学图像绘制。 = sns.cubehelix_palette(as_cmap=True, dark=1, light=0) # cmap: color map 颜色映射sns.kdeplot(df.x, df.y, cmap=cmap, n_levels=60, shade=True)
g = sns.jointplot(x="x", y="y", data=df, kind="kde", color="m")g.plot_joint(plt.scatter, c="w", s=30, linewidth=1, marker="+")g.ax_joint.collections[0].set_alpha(0) # 设置中间图片背景的透明度g.set_axis_labels("$X$", "$Y$") # Latex

注:关于kde图,一维的主要是猜分布的,二维的若能看出有好几个中心,就可以做聚类相关的工作。

数据集中的两两关系

iris =  pd.read_csv("iris.csv") # 鸢尾花数据库iris.head()
sns.pairplot(iris)  # 默认对角线hist,非对角线scatter

属性两两间的关系 + 属性的灰度图

g = sns.PairGrid(iris)g.map_diag(sns.kdeplot) # 对角线单个属性图g.map_offdiag(sns.kdeplot, cmap="Blues_d", n_levels=20) # 非对角线两个属性关系图
小结distplot(bins, rug)kdeplot(bw, fit)joinplot(kind)pairplot

标签: #python 分布函数