利用线性回归模型对数据进行拟合。
案例代码已上传:Github地址
import seaborn as sns import matplotlib.pyplot as plt # 设置风格样式 sns.set(color_codes=True) # 构建数据 tips = sns.load_dataset("tips") """ 案例1: 绘制双变量的线性关系 """ sns.regplot(x="total_bill", y="tip", data=tips) plt.show() import seaborn as sns import matplotlib.pyplot as plt import numpy as np # 设置风格样式 sns.set(color_codes=True) # 构建数据 np.random.seed(8) mean, cov = [4, 6], [(1.5, .7), (.7, 1)] x, y = np.random.multivariate_normal(mean, cov, 80).T """ 案例2: 构建随机数据,绘制双变量的线性关系 """ sns.regplot(x=x, y=y, color="g") plt.show() import seaborn as sns import matplotlib.pyplot as plt import numpy as np import pandas as pd # 设置风格样式 sns.set(color_codes=True) # 构建数据 np.random.seed(8) mean, cov = [4, 6], [(1.5, .7), (.7, 1)] x, y = np.random.multivariate_normal(mean, cov, 80).T x, y = pd.Series(x, name="x_var"), pd.Series(y, name="y_var") """ 案例3: 构建随机数据,并对数据Series,并指定x,y对应的变量名,绘制双变量的线性关系 """ sns.regplot(x=x, y=y, marker="+") plt.show() import seaborn as sns import matplotlib.pyplot as plt import numpy as np import pandas as pd # 设置风格样式 sns.set(color_codes=True) # 构建数据 np.random.seed(8) mean, cov = [4, 6], [(1.5, .7), (.7, 1)] x, y = np.random.multivariate_normal(mean, cov, 80).T x, y = pd.Series(x, name="x_var"), pd.Series(y, name="y_var") """ 案例4: 参考案例3,并设置ci=68:使用68%的置信区间,该区间对应于估计的标准误差 """ sns.regplot(x=x, y=y, ci=68) plt.show() import seaborn as sns import matplotlib.pyplot as plt # 设置风格样式 sns.set(color_codes=True) # 构建数据 tips = sns.load_dataset("tips") """ 案例5: 根据数据的实际情况,指定按x轴进行分组, 并对x轴分组数据增加一些抖动(x_jitter=.1) """ sns.regplot(x="size", y="total_bill", data=tips, x_jitter=.1) plt.show() import seaborn as sns import matplotlib.pyplot as plt # 设置风格样式 sns.set(color_codes=True) # 构建数据 tips = sns.load_dataset("tips") """ 案例6: 根据数据的实际情况,指定按x轴进行分组,只显示每一分组数据的均值和置信区间 """ sns.regplot(x="size", y="total_bill", data=tips,x_estimator=np.mean) plt.show() import seaborn as sns import matplotlib.pyplot as plt import numpy as np import pandas as pd # 设置风格样式 sns.set(color_codes=True) # 构建数据 np.random.seed(8) mean, cov = [4, 6], [(1.5, .7), (.7, 1)] x, y = np.random.multivariate_normal(mean, cov, 80).T x, y = pd.Series(x, name="x_var"), pd.Series(y, name="y_var") """ 案例7: 将连续变量绘制成不连续的区域 """ sns.regplot(x=x, y=y, x_bins=4) plt.show() import seaborn as sns import matplotlib.pyplot as plt # 设置风格样式 sns.set(color_codes=True) # 构建数据 ans = sns.load_dataset("anscombe") """ 案例8: 拟合高阶多项式回归并阶段模型预测 """ sns.regplot(x="x", y="y", data=ans.loc[ans.dataset == "II"], scatter_kws={"s": 80}, order=2, ci=None, truncate=True) plt.show() import seaborn as sns import matplotlib.pyplot as plt # 设置风格样式 sns.set(color_codes=True) # 构建数据 ans = sns.load_dataset("anscombe") """ 案例9: 拟合稳健回归,不要置信区间 """ sns.regplot(x="x", y="y", data=ans.loc[ans.dataset == "III"], scatter_kws={"s": 80}, robust=True, ci=None) plt.show() import seaborn as sns import matplotlib.pyplot as plt # 设置风格样式 sns.set(color_codes=True) # 构建数据 tips = sns.load_dataset("tips") tips["big_tip"] = (tips.tip / tips.total_bill) > .175 """ 案例10: 拟合logistic 回归(logistic=True), 对y轴数据进行抖动(y_jitter=.03), 并进行适当的迭代(n_boot=500) """ sns.regplot(x="total_bill", y="big_tip", data=tips,logistic=True, n_boot=500, y_jitter=.03) plt.show() import seaborn as sns import matplotlib.pyplot as plt # 设置风格样式 sns.set(color_codes=True) # 构建数据 tips = sns.load_dataset("tips") """ 案例11: 使用log(x)拟合回归模型,并阶段模型预测 """ sns.regplot(x="size", y="total_bill", data=tips,x_estimator=np.mean, logx=True, truncate=True) plt.show()上述案例代码已上传:Github地址 Github地址https://github.com/Vambooo/SeabornCN
更多技术干货在公众号:数据分析与可视化学研社