Python 避免循环在三维阵列上运行线性回归
我需要在3d数组中运行线性回归,例如:- 数组=np.arange(3*4*5)。重塑(3,4,5) 我需要在[0,20,40]、[1,21,41]…[5,25,45]…[19,39,59]上运行LR 我正在使用以下代码:-Python 避免循环在三维阵列上运行线性回归,python,arrays,numpy,linear-regression,least-squares,Python,Arrays,Numpy,Linear Regression,Least Squares,我需要在3d数组中运行线性回归,例如:- 数组=np.arange(3*4*5)。重塑(3,4,5) 我需要在[0,20,40]、[1,21,41]…[5,25,45]…[19,39,59]上运行LR 我正在使用以下代码:- LrCalcRate = np.array([]) col_nums = 3 for j in range(Array.shape[1]): for i in range(Array.shape[2]): y = Array[:, j, i].T
LrCalcRate = np.array([])
col_nums = 3
for j in range(Array.shape[1]):
for i in range(Array.shape[2]):
y = Array[:, j, i].T
x = np.arange(col_nums)
A = np.vstack([x, np.ones(len(x))]).T
m, c = np.linalg.lstsq(A, y)[0]
LrCalcRate = np.append(LrCalcRate, -m)
两个for循环需要花费大量时间,不使用for循环是否可以对其进行优化。您编写的内容与
x = np.arange(col_nums)
A = np.vstack([x, np.ones(len(x))]).T
-np.linalg.lstsq(A, a.reshape(a.shape[0], -1))[0][0]
例如,使用为每个目标生成不同坡度的数据:
In [9]: a = (np.linspace(0, 2, num=3*4*5)**2).reshape(3,4,5)
In [11]: lr_calc_rate = np.array([])
...: col_nums = 3
...: for j in range(a.shape[1]):
...: for i in range(a.shape[2]):
...: y = a[:, j, i].T
...: x = np.arange(col_nums)
...: A = np.vstack([x, np.ones(len(x))]).T
...: m, c = np.linalg.lstsq(A, y)[0]
...: lr_calc_rate = np.append(lr_calc_rate, -m)
...: lr_calc_rate
Out[11]:
array([-0.91927607, -0.96523987, -1.01120368, -1.05716748, -1.10313128,
-1.14909509, -1.19505889, -1.24102269, -1.2869865 , -1.3329503 ,
-1.37891411, -1.42487791, -1.47084171, -1.51680552, -1.56276932,
-1.60873312, -1.65469693, -1.70066073, -1.74662453, -1.79258834])
In [12]: x = np.arange(col_nums)
...: A = np.vstack([x, np.ones(len(x))]).T
...: -np.linalg.lstsq(A, a.reshape(3, -1))[0][0]
Out[12]:
array([-0.91927607, -0.96523987, -1.01120368, -1.05716748, -1.10313128,
-1.14909509, -1.19505889, -1.24102269, -1.2869865 , -1.3329503 ,
-1.37891411, -1.42487791, -1.47084171, -1.51680552, -1.56276932,
-1.60873312, -1.65469693, -1.70066073, -1.74662453, -1.79258834])
这个例子是不可复制的。请定义
col\u nums
,noisemeasuresnp
等。这看起来需要使用并从那里开始线性回归。但是这个例子太混乱了,无法理解,为什么你不能适当地重塑呢?嗨@yatu,我已经更正了代码谢谢fuglede,你能解释一下它是如何工作的,或者你能告诉我怎么做的吗?嗯,它正是你用手所做的。From:“如果b是二维的,则计算b的K列的最小二乘解”。
In [9]: a = (np.linspace(0, 2, num=3*4*5)**2).reshape(3,4,5)
In [11]: lr_calc_rate = np.array([])
...: col_nums = 3
...: for j in range(a.shape[1]):
...: for i in range(a.shape[2]):
...: y = a[:, j, i].T
...: x = np.arange(col_nums)
...: A = np.vstack([x, np.ones(len(x))]).T
...: m, c = np.linalg.lstsq(A, y)[0]
...: lr_calc_rate = np.append(lr_calc_rate, -m)
...: lr_calc_rate
Out[11]:
array([-0.91927607, -0.96523987, -1.01120368, -1.05716748, -1.10313128,
-1.14909509, -1.19505889, -1.24102269, -1.2869865 , -1.3329503 ,
-1.37891411, -1.42487791, -1.47084171, -1.51680552, -1.56276932,
-1.60873312, -1.65469693, -1.70066073, -1.74662453, -1.79258834])
In [12]: x = np.arange(col_nums)
...: A = np.vstack([x, np.ones(len(x))]).T
...: -np.linalg.lstsq(A, a.reshape(3, -1))[0][0]
Out[12]:
array([-0.91927607, -0.96523987, -1.01120368, -1.05716748, -1.10313128,
-1.14909509, -1.19505889, -1.24102269, -1.2869865 , -1.3329503 ,
-1.37891411, -1.42487791, -1.47084171, -1.51680552, -1.56276932,
-1.60873312, -1.65469693, -1.70066073, -1.74662453, -1.79258834])