How to get the numerical fitting results when plotting a regression in seaborn?

前端 未结 3 1279
孤街浪徒
孤街浪徒 2020-12-02 17:09

If I use the seaborn library in Python to plot the result of a linear regression, is there a way to find out the numerical results of the regression? For example, I might wa

3条回答
  •  失恋的感觉
    2020-12-02 17:20

    Looking thru the currently available doc, the closest I've been able to determine if this functionality can now be met is if one uses the scipy.stats.pearsonr module.

    r2 = stats.pearsonr("pct", "rdiff", df)
    

    In attempting to make it work directly within a Pandas dataframe, there's an error kicked out from violating the basic scipy input requirements:

    TypeError: pearsonr() takes exactly 2 arguments (3 given)
    

    I managed to locate another Pandas Seaborn user who evidently solved it: https://github.com/scipy/scipy/blob/v0.14.0/scipy/stats/stats.py#L2392

    sns.regplot("rdiff", "pct", df, corr_func=stats.pearsonr);
    

    But, unfortunately I haven't managed to get that to work as it appears the author created his own custom 'corr_func' or either there's an undocumented Seaborn arguement passing method that's available using a more manual method:

    # x and y should have same length.
        x = np.asarray(x)
        y = np.asarray(y)
        n = len(x)
        mx = x.mean()
        my = y.mean()
        xm, ym = x-mx, y-my
        r_num = np.add.reduce(xm * ym)
        r_den = np.sqrt(ss(xm) * ss(ym))
        r = r_num / r_den
    
    # Presumably, if abs(r) > 1, then it is only some small artifact of floating
    # point arithmetic.
    r = max(min(r, 1.0), -1.0)
    df = n-2
    if abs(r) == 1.0:
        prob = 0.0
    else:
        t_squared = r*r * (df / ((1.0 - r) * (1.0 + r)))
        prob = betai(0.5*df, 0.5, df / (df + t_squared))
    return r, prob
    

    Hope this helps to advance this original request along toward an interim solution as there's much needed utility to add the regression fitness stats to the Seaborn package as a replacement to what one can easily get from MS-Excel or a stock Matplotlib lineplot.

提交回复
热议问题