pandas 如何在Python中的pandas数据框中获取数据的斜率?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/49100471/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get slopes of data in pandas dataframe in Python?
提问by Mike
I want to get slopes of dataset in the dataframe (either using linear regression model or sk-learn model).
我想在数据框中获得数据集的斜率(使用线性回归模型或 sk-learn 模型)。
df1:
A B C D
0 15 25 55 100
1 15.5 25.5 56 101
2 14.8 24.5 54.2 99.8
3 15.5 25.5 55.5 102
4 16 26 57 108
I want to get slopes of each dolumn ('A', 'B', 'C', 'D') in the form of pd.Series. Can you help me on this? Thank you.
我想以 pd.Series 的形式获得每个 dolumn ('A', 'B', 'C', 'D') 的斜率。你能帮我解决这个问题吗?谢谢你。
The output I want is something like below (I used just dummy numbers, not real slopes!):
我想要的输出类似于下面的内容(我只使用了虚拟数字,而不是真正的斜率!):
slopes:
A 2.5
B 2.8
C 3.1
D 3.3
回答by sacuL
I believe this does it, it's a simple linear regression with numpy
我相信这样做了,这是一个简单的线性回归 numpy
import numpy as np
slopes = df.apply(lambda x: np.polyfit(df.index, x, 1)[0])
>>> slopes
A 0.20
B 0.20
C 0.35
D 1.70
And if you want to visualize the data and the fitted slopes:
如果您想可视化数据和拟合斜率:
for i in df.columns:
plt.scatter(df.index, df[i], label=i)
plt.plot(np.polyval(np.polyfit(df.index, df[i], 1), df.index))
plt.legend()
plt.show()