<jupyter_start><jupyter_text>可视化线性关系  目录+ 绘制线型回归模型的函数    + sns.regplot()和sns.lmplot()函数的区别    + 某个变量是离散变量        + jitter参数        + x_estimator参数+ 拟合不同的模型    + 高阶多项式    + 异常值    + 逻辑回归    + 局部回归    + 残差图+ 条件于其他变量的回归线    + 条件于第3个变量    + 条件于第4个变量    + 条件于第5个变量+ 设置图表的大小、形状    + sns.regplot()和sns.lmplot()函数的区别+ 在其他情况下绘制回归线    + 双变量分布图    + 多个变量相对于一个变量的回归线<jupyter_code>import numpy as np
import pandas as pd
import matplotlib as mpl
import matplotlib.pyplot as plt
import seaborn as sns
sns.set(color_codes=True)
np.random.seed(sum(map(ord, "regression")))
%matplotlib inline
tips = sns.load_dataset("tips") # 所有的数据集在https://github.com/mwaskom/seaborn-data<jupyter_output><empty_output><jupyter_text>绘制线型回归模型的函数sns.regplot()和sns.lmplot()函数<jupyter_code>sns.regplot(x="total_bill", y="tip", data=tips) # 默认绘制散点图、线性回归线、95%置信区间
sns.lmplot(x="total_bill", y="tip", data=tips) # 默认绘制散点图、线性回归线、95%置信区间<jupyter_output><empty_output><jupyter_text>sns.regplot()和sns.lmplot()函数的区别+ 数据集    + sns.regplot()可以接受多种格式的数据，例如numpy arrays,Series、DataFrame等      + sns.lmplot()只能接受DataFrame，并传递给data参数，x、y参数必须是字符串，指代数据列名            + 参数sns.regplot()的参数是sns.lmplot()的子集，因而后续会以sns.lmplot()进行演示  某个变量是离散变量<jupyter_code>sns.lmplot(x="size", y="tip", data=tips) # 当某个变量是离散值时，散点图的效果比较差，难以观察数据的分布特征<jupyter_output><empty_output><jupyter_text>jitter参数给离散变量添加噪声<jupyter_code>sns.lmplot(x="size", y="tip", data=tips, x_jitter=.05) # 设置jitter参数，给离散变量添加噪声，噪声只会影响散点图的绘制，不影响回归线的拟合<jupyter_output><empty_output><jupyter_text>x_estimator参数Apply this function to each unique value of x and plot the resulting estimate. This is useful when x is a discrete variable. If x_ci is not None, this estimate will be bootstrapped and a confidence interval will be drawn.<jupyter_code>sns.lmplot(x="size", y="tip", data=tips, x_estimator=np.mean) # x_ci参数默认为'ci'，绘制在每个离散值上y的置信区间（图中原点两侧延伸出的线条）<jupyter_output><empty_output><jupyter_text>拟合不同的模型<jupyter_code>anscombe = sns.load_dataset("anscombe")<jupyter_output><empty_output><jupyter_text>高阶多项式  第一个数据集适合拟合线型模型<jupyter_code>sns.lmplot(x="x", y="y", data=anscombe.query("dataset == 'I'"),
           ci=None, scatter_kws={"s": 80}) # ci参数设置是否绘制置信区间，scatter_kws字典会将参数传给plt.scatter()方法（本例中设置散点大小）<jupyter_output><empty_output><jupyter_text>第二个数据集不适合拟合线型模型<jupyter_code>sns.lmplot(x="x", y="y", data=anscombe.query("dataset == 'II'"),
           ci=None, scatter_kws={"s": 80})<jupyter_output><empty_output><jupyter_text>绘制高阶多项式回归模型<jupyter_code>sns.lmplot(x="x", y="y", data=anscombe.query("dataset == 'II'"),
           order=2, ci=None, scatter_kws={"s": 80}) # 设置order=2，绘制2次多项式回归线<jupyter_output><empty_output><jupyter_text>异常值<jupyter_code>sns.lmplot(x="x", y="y", data=anscombe.query("dataset == 'III'"),
           ci=None, scatter_kws={"s": 80}) # 数据集中存在异常值
sns.lmplot(x="x", y="y", data=anscombe.query("dataset == 'III'"),
           robust=True, ci=None, scatter_kws={"s": 80}) # 设置robust=True，在有异常值存在时，降低残差较大观测点的权重，以得到更稳健的回归线<jupyter_output><empty_output><jupyter_text>逻辑回归<jupyter_code>tips["big_tip"] = (tips.tip / tips.total_bill) > .15
sns.lmplot(x="total_bill", y="big_tip", data=tips,
           y_jitter=.03)
sns.lmplot(x="total_bill", y="big_tip", data=tips,
           logistic=True, y_jitter=.03) # 设置logistic=True，绘制逻辑回归线<jupyter_output><empty_output><jupyter_text>局部回归<jupyter_code>sns.lmplot(x="total_bill", y="tip", data=tips,
           lowess=True) # 设置lowess=True，绘制局部回归线，由于计算量很大，不会绘制置信区间<jupyter_output><empty_output><jupyter_text>残差图sns.residplot()<jupyter_code>sns.residplot(x="x", y="y", data=anscombe.query("dataset == 'I'"),
              scatter_kws={"s": 80}) # 理想情况下，残差应该随机分布于直线y=0的上下
sns.residplot(x="x", y="y", data=anscombe.query("dataset == 'II'"),
              scatter_kws={"s": 80}) # 散点图如果呈现出特定分布，说明线性回归不合适<jupyter_output><empty_output><jupyter_text>条件于其他变量的回归线sns.lmplot()与FacetGrid对象  条件于第3个变量<jupyter_code>sns.lmplot(x="total_bill", y="tip", hue="smoker", data=tips) # 将两条回归线置于同一坐标系内，并用不同颜色加以区分
sns.lmplot(x="total_bill", y="tip", hue="smoker", data=tips,
           markers=["o", "x"], palette="Set1") # markers参数设置散点形状，palette参数设置散点颜色<jupyter_output><empty_output><jupyter_text>条件于第4个变量<jupyter_code>sns.lmplot(x="total_bill", y="tip", hue="smoker", col="time", data=tips) # 设置col参数，条件于第四个变量<jupyter_output><empty_output><jupyter_text>条件于第5个变量<jupyter_code>sns.lmplot(x="total_bill", y="tip", hue="smoker",
           col="time", row="sex", data=tips) # 设置col、row参数，条件于第五个变量<jupyter_output><empty_output><jupyter_text>设置图表的大小、形状  sns.regplot()和sns.lmplot()函数的区别+ lmplot() uses regplot() internally and takes most of its parameters  + regplot() is an axes-level function, so it draws directly onto an axes (either the currently active axes or the one provided by the ax parameter)      + regplot() can happily coexist in a figure with other kinds of plots and will follow the global matplotlib color cycle+ lmplot() is a figure-level function and creates its own figure, which is managed through a FacetGrid    + lmplot() needs to occupy an entire figure, and the size and color cycle are controlled through function parameters, ignoring the global defaults<jupyter_code>f, ax = plt.subplots(figsize=(5, 6))
sns.regplot(x="total_bill", y="tip", data=tips, ax=ax) # regplot()函数，为了设置图表的大小，需要创建一个figure对象
sns.lmplot(x="total_bill", y="tip", col="day", data=tips,
           col_wrap=2, size=3) # size、aspect参数控制每个分面的高度、宽度（而非整个图表的大小）
sns.lmplot(x="total_bill", y="tip", col="day", data=tips,
           aspect=.5) # size是每个分面的高度，aspec*size是每个分面的宽度<jupyter_output><empty_output><jupyter_text>在其他情况下绘制回归线  双变量分布图sns.jointplot()和kind="reg"<jupyter_code>sns.jointplot(x="total_bill", y="tip", data=tips, kind="reg") # sns.jointplot()中设置kind="reg"，绘制回归线<jupyter_output><empty_output><jupyter_text>多个变量相对于一个变量的回归线PairGrid对象 + map方法、sns.pairplot() + kind参数，注意同条件于其他变量的回归线相区别+ 条件于其他变量的回归线  展示在不同的第三变量水平下，两个变量之间关系的变化情况+ 多个变量相对于一个变量的回归线  展示不同的变量与某个变量之间的关系<jupyter_code>g = sns.PairGrid(tips, x_vars=["total_bill", "size"], y_vars=["tip"],
             size=5, aspect=.8) # 生成PairGrid对象
g.map(sns.regplot) # 使用PairGrid对象的map方法，效果与下面pairplot函数中设置kind参数相同
sns.pairplot(tips, x_vars=["total_bill", "size"], y_vars=["tip"],
             size=5, aspect=.8, kind="reg") # 设置x_vars，y_vars参数
sns.pairplot(tips, x_vars=["total_bill", "size"], y_vars=["tip"],
             hue="smoker", size=5, aspect=.8, kind="reg") # pairplot也可以通过参数hue，绘制条件于其他变量的回归线<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15529889183705323, 'fraction_numerical': 0.007491805837365382, 'mean_word_length': 7.0705289672544085, 'pattern_counts': {'":': 7, '<': 77, '<?xml version=': 0, '>': 78, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '19', 'dir': 'jupyter-structured-clean-dedup', 'id': '337603', 'license': 'Apache-2.0', 'n_tokens_mistral': 3215, 'n_tokens_neox': 3374, 'n_words': 357, 'path': 'ext/DataMining/1_DataExploration/3_Seaborn/4_Visualizing linear relationships.ipynb', 'repo_name': 'Jie-Yuan/1_DataMining', 'size': '6407'}