gpt4 book ai didi

python - pandas - 数据框列值的线性回归

转载 作者:太空宇宙 更新时间:2023-11-04 03:20:22 25 4
gpt4 key购买 nike

我有一个 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 中获得总结三个单一线性回归的单一线性回归?

最佳答案

也许是这样的:

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-')

关于python - pandas - 数据框列值的线性回归,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34882764/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com