【问题标题】:Is there a way to get the x-axis and y-axis values of my seaborn plot?有没有办法获得我的 seaborn 图的 x 轴和 y 轴值?
【发布时间】:2020-10-18 13:07:39
【问题描述】:

我使用 seaborn 库来拟合我的数据的回归线。然后我还绘制了残差图。我现在需要查看残差的直方图分布吗?我怎么能这样做,因为我没有在图表中绘制的值。

这是我的代码:

fig,axes = plt.subplots(1,3,figsize=(15,5))
sns.regplot(x = 'Radio',y='Sales',data=df_advertising,ax = axes[0])
sns.residplot(x = 'Radio',y='Sales',data=df_advertising,ax = axes[1])

如何获取残差图的值,以便绘制相应的直方图以查看分布。

谢谢,我们将不胜感激。我只是一个初学者。

【问题讨论】:

    标签: python matplotlib statistics regression seaborn


    【解决方案1】:

    不太可能恢复拟合或值(另请参阅this question)。您知道自己适合什么,然后绘制残差也是有道理的。下面我使用一个示例数据集:

    import matplotlib. pyplot as plt
    import seaborn as sns
    import numpy as np
    import statsmodels.api as sm
    
    df_advertising = pd.DataFrame({'Radio':np.random.randint(1,10,100)})
    df_advertising['Sales'] = 3*df_advertising['Radio'] + np.random.normal(10,4,100)
    

    我们可以使用 seaborn 来绘制它:

    fig,axes = plt.subplots(1,2,figsize=(10,5))
    sns.regplot(x = 'Radio',y='Sales',data=df_advertising,ax = axes[0])
    sns.residplot(x = 'Radio',y='Sales',data=df_advertising,ax = axes[1])
    

    seaborn 使用 statsmodels,所以让我们使用它来拟合并获得预测:

    mod = sm.OLS(df_advertising['Sales'],sm.add_constant(df_advertising['Radio']))
    res = mod.fit()
    
    test = df_advertising[['Radio']].drop_duplicates().sort_values('Radio')
    predictions = res.get_prediction(sm.add_constant(test))
    predictions = pd.concat([test,predictions.summary_frame(alpha=0.05)],axis=1)
    predictions.head()
    
        Radio   mean    mean_se mean_ci_lower   mean_ci_upper   obs_ci_lower    obs_ci_upper
    13  1   11.132902   0.700578    9.742628    12.523175   3.862061    18.403742
    6   2   14.480520   0.582916    13.323742   15.637298   7.250693    21.710347
    2   3   17.828139   0.478925    16.877728   18.778550   10.628448   25.027829
    4   4   21.175757   0.399429    20.383104   21.968411   13.995189   28.356326
    10  5   24.523376   0.360990    23.807002   25.239750   17.350827   31.695924
    

    在上面,我创建了不重复数据点的测试(因为我的是计数)。现在我们可以绘制所有内容了。残差就在 statsmodels 对象的resid 之下:

    fig,axes = plt.subplots(1,3,figsize=(15,5))
    
    sns.scatterplot(x='Radio',y='Sales',ax=axes[0],data=df_advertising)
    axes[0].plot(predictions['Radio'], predictions['mean'], lw=2)
    axes[0].fill_between(x=predictions['Radio'],
                         y1=predictions['mean_ci_lower'],y2=predictions['mean_ci_upper'],
                         facecolor='blue', alpha=0.2)
    
    sns.scatterplot(x='Radio',y='Sales',ax=axes[1],
                    data=pd.DataFrame({'Radio':df_advertising['Radio'],
                                      'Sales':res.resid})
                   )
    axes[1].axhline(0, ls='--',color="k")
    
    sns.distplot(res.resid,ax=axes[2],bins=20)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-07
      • 2020-03-06
      相关资源
      最近更新 更多