【问题标题】:pandas - linear regression of dataframe columns valuespandas - 数据框列值的线性回归
【发布时间】:2016-04-25 06:41:44
【问题描述】:

我有一个 pandas 数据框 df,比如:

A,B,C
1,1,1
0.8,0.6,0.9
0.7,0.5,0.8
0.2,0.4,0.1
0.1,0,0

其中三列具有排序值 [0,1]。我正在尝试对三个系列进行线性回归。到目前为止,我可以使用scipy.stats,如下所示:

from scipy import stats

xi = np.arange(len(df))

slope, intercept, r_value, p_value, std_err = stats.linregress(xi,df['A'])
line1 = intercept + slope*xi
slope, intercept, r_value, p_value, std_err = stats.linregress(xi,df['B'])
line2 = intercept + slope*xi
slope, intercept, r_value, p_value, std_err = stats.linregress(xi,df['C'])
line3 = intercept + slope*xi

plt.plot(line1,'r-')
plt.plot(line2,'b-')
plt.plot(line3,'g-')

plt.plot(xi,df['A'],'ro')
plt.plot(xi,df['B'],'bo')
plt.plot(xi,df['C'],'go')

得到以下情节:

是否有可能获得一个单一的线性回归来总结scipy.stats 内的三个单一线性回归?

【问题讨论】:

  • 如果你想要一个总结三个回归的回归,为什么不把所有的数据结合起来,对这些数据做线性回归呢?

标签: python pandas scipy statistics regression


【解决方案1】:

大概是这样的:

x = pd.np.tile(xi, 3)
y = pd.np.r_[df['A'], df['B'], df['C']]

slope, intercept, r_value, p_value, std_err = stats.linregress(x, y)
line4 = intercept + slope * xi

plt.plot(line4,'k-')

【讨论】:

    猜你喜欢
    • 2013-10-23
    • 2015-08-06
    • 2015-06-27
    • 2022-11-11
    • 2017-05-21
    • 2021-09-11
    • 2020-08-26
    • 2019-12-29
    • 1970-01-01
    相关资源
    最近更新 更多