seaborn-liner

可视化线性关系

1
2
3
4
5
6
7
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

sns.set(color_codes=True)

tips = sns.load_dataset("tips")

绘制线性回归

Two main functions in seaborn are used to visualize a linear relationship as determined through regression. These functions, regplot() and lmplot() are closely related, and share much of their core functionality. It is important to understand the ways they differ, however, so that you can quickly choose the correct tool for particular job.

1
2
3
4
#先看一个最简单的例子
#默认置信区间为95%

sns.regplot(x='total_bill',y='tip',data=tips)
<matplotlib.axes._subplots.AxesSubplot at 0x110f64d68>

png

1
sns.lmplot(x='total_bill',y='tip',data=tips)
<seaborn.axisgrid.FacetGrid at 0x110f15780>

png

regplot()可以接受更加灵活的输入,lmplot()接受整形输入,同时regplot()也拥有部分lmplot()的功能。

1
sns.lmplot(x='size',y='tip',data=tips)
<seaborn.axisgrid.FacetGrid at 0x112574400>

png

Fitting different kinds of models(拟合不同的模型)

1
2
anscombe = sns.load_dataset("anscombe")
anscombe.head()
dataset x y
0 I 10.0 8.04
1 I 8.0 6.95
2 I 13.0 7.58
3 I 9.0 8.81
4 I 11.0 8.33
1
sns.lmplot(x='x',y='y',data=anscombe.query("dataset == 'I'"),ci=None,scatter_kws={"s": 80})
<seaborn.axisgrid.FacetGrid at 0x1a1e166978>

png

1
2
sns.lmplot(x="x", y="y", data=anscombe.query("dataset == 'II'"),
ci=None, scatter_kws={"s": 80})

png

1
2
3
#上一个明明显拟合不到位,我们可以猜测这是一个多项式回归,利用order参数调用numpy.polyfit()
sns.lmplot(x="x", y="y", data=anscombe.query("dataset == 'II'"),order = 2 ,
ci=None, scatter_kws={"s": 80})
<seaborn.axisgrid.FacetGrid at 0x1a1e3b3cc0>

png

1
2
3
4
sns.lmplot(x="x", y="y", data=anscombe.query("dataset == 'III'"),
ci=None, scatter_kws={"s": 80});

## 存在异常值

png

1
2
3
# 不同的损失函数来减轻相对较大的残差
sns.lmplot(x="x", y="y", data=anscombe.query("dataset == 'III'"),
robust=True, ci=None, scatter_kws={"s": 80});

png