pandas - 数据帧列值的线性回归

时间:2016-01-19 17:04:49

标签: python pandas scipy statistics regression

我有一个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')

获得以下情节:

enter image description here

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

1 个答案:

答案 0 :(得分:2)

也许是这样的:

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-')
相关问题