hot*_*ana 14 python numpy scipy
我试图找到使用Numpy和Scipy计算斜率的最快和最有效的方法.我有一个包含三个Y变量和一个X变量的数据集,我需要计算它们各自的斜率.例如,我可以轻松地一次执行这一行,如下所示,但我希望有一种更有效的方法.我也不认为linregress是最好的方法,因为我的结果中不需要任何辅助变量,如拦截,标准错误等.任何帮助是极大的赞赏.
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]
Run Code Online (Sandbox Code Playgroud)
Sal*_*ali 26
最快和最有效的方式是使用从本地SciPy的功能linregress这一切计算:
斜率:回归线的斜率
截距:回归线的截距
r值:相关系数
p值:假设检验的双侧p值,其零假设是斜率为零
stderr:估计的标准误差
这是一个例子:
a = [15, 12, 8, 8, 7, 7, 7, 6, 5, 3]
b = [10, 25, 17, 11, 13, 17, 20, 13, 9, 15]
from scipy.stats import linregress
linregress(a, b)
Run Code Online (Sandbox Code Playgroud)
会回报你:
LinregressResult(slope=0.20833333333333337, intercept=13.375, rvalue=0.14499815458068521, pvalue=0.68940144811669501, stderr=0.50261704627083648)
Run Code Online (Sandbox Code Playgroud)
PS只是斜率的数学公式:
线性回归计算在一维中是矢量计算。这意味着我们可以在整个Y矩阵上组合乘法,然后使用numpy中的axis参数向量化拟合。在您的情况下可以解决以下问题
((X*Y).mean(axis=1) - X.mean()*Y.mean(axis=1)) / ((X**2).mean() - (X.mean())**2)
Run Code Online (Sandbox Code Playgroud)
您对拟合质量参数不感兴趣,但是大多数参数都可以类似的方式获得。
比接受的答案更简单的表示形式:
x = np.linspace(0, 10, 11)
y = np.linspace(0, 20, 11)
y = np.c_[y, y,y]
X = x - x.mean()
Y = y - y.mean()
slope = (X.dot(Y)) / (X.dot(X))
Run Code Online (Sandbox Code Playgroud)
斜率的方程式来自使用简单回归的直线斜率的矢量符号。
这个清晰的单线应该足够高效,没有 scipy:
slope = np.polyfit(X,Y,1)[0]
Run Code Online (Sandbox Code Playgroud)
最后你应该得到
import numpy as np
Y = np.array([
[ 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]]).T
X = [ 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999]
print np.polyfit(X,Y,1)[0]
Run Code Online (Sandbox Code Playgroud)
输出为 [1.54983152e+10 9.98749876e+01 1.84564349e+00]
小智 6
我这样做的方法是使用 np.diff() 函数:
dx = np.diff(xvals),
dy = np.diff(yvals)
斜率 = dy/dx
如前所述,您可以使用 scipy 的 linregress。以下是如何获得斜率:
from scipy.stats import linregress
x=[1,2,3,4,5]
y=[2,3,8,9,22]
slope, intercept, r_value, p_value, std_err = linregress(x, y)
print(slope)
Run Code Online (Sandbox Code Playgroud)
请记住,这样做,因为您正在计算 r_value 和 p_value 等额外值,将比手动仅计算斜率花费更长的时间。但是,Linregress 非常快。
资料来源:https : //docs.scipy.org/doc/scipy/reference/generated/scipy.stats.linregress.html