问题描述
我正在尝试找到使用Numpy和Scipy计算坡度的最快,最有效的方法.我有一个包含三个Y变量和一个X变量的数据集,我需要计算它们各自的斜率.例如,如下所示,我可以轻松地一次完成这一行,但是我希望有一种更有效的方法.我也不认为linregress是最好的方法,因为我的结果中不需要任何辅助变量,例如拦截,标准错误等.任何帮助是极大的赞赏.
I am trying to find the fastest and most efficient way to calculate slopes using Numpy and Scipy. I have a data set of three Y variables and one X variable and I need to calculate their individual slopes. For example, I can easily do this one row at a time, as shown below, but I was hoping there was a more efficient way of doing this. I also don't think linregress is the best way to go because I don't need any of the auxiliary variables like intercept, standard error, etc in my results. Any help is greatly appreciated.
import numpy as np
from scipy import stats
Y = [[ 2.62710000e+11 3.14454000e+11 3.63609000e+11 4.03196000e+11
4.21725000e+11 2.86698000e+11 3.32909000e+11 4.01480000e+11
4.21215000e+11 4.81202000e+11]
[ 3.11612352e+03 3.65968334e+03 4.15442691e+03 4.52470938e+03
4.65011423e+03 3.10707392e+03 3.54692896e+03 4.20656404e+03
4.34233412e+03 4.88462501e+03]
[ 2.21536396e+01 2.59098311e+01 2.97401268e+01 3.04784552e+01
3.13667639e+01 2.76377113e+01 3.27846013e+01 3.73223417e+01
3.51249997e+01 4.42563658e+01]]
X = [ 1990. 1991. 1992. 1993. 1994. 1995. 1996. 1997. 1998. 1999.]
slope_0, intercept, r_value, p_value, std_err = stats.linregress(X, Y[0,:])
slope_1, intercept, r_value, p_value, std_err = stats.linregress(X, Y[1,:])
slope_2, intercept, r_value, p_value, std_err = stats.linregress(X, Y[2,:])
slope_0 = slope/Y[0,:][0]
slope_1 = slope/Y[1,:][0]
slope_2 = slope/Y[2,:][0]
b, a = polyfit(X, Y[1,:], 1)
slope_1_a = b/Y[1,:][0]
推荐答案
在一个维度上,线性回归计算是向量计算.这意味着我们可以在整个 Y 矩阵上组合乘法,然后使用numpy中的 axis 参数向量化拟合.在您的情况下,可以解决以下问题
The linear regression calculation is, in one dimension, a vector calculation. This means we can combine the multiplications on the entire Y matrix, and then vectorize the fits using the axis parameter in numpy. In your case that works out to the following
((X*Y).mean(axis=1) - X.mean()*Y.mean(axis=1)) / ((X**2).mean() - (X.mean())**2)
您对拟合质量参数不感兴趣,但是大多数参数都可以类似的方式获得.
You're not interested in fit quality parameters but most of them can be obtained in a similar manner.
这篇关于计算Numpy(或Scipy)中的坡度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!