我有一个 4 维 numpy 数组 (x,y,z,time) 并且想numpy.polyfit
在每个 x,y,z 坐标处通过时间维度做一个。例如:
import numpy as np
n = 10 # size of my x,y,z dimensions
degree = 2 # degree of my polyfit
time_len = 5 # number of time samples
# Make some data
A = np.random.rand(n*n*n*time_len).reshape(n,n,n,time_len)
# An x vector to regress through evenly spaced samples
X = np.arange( time_len )
# A placeholder for the regressions
regressions = np.zeros(n*n*n*(degree+1)).reshape(n,n,n,degree+1)
# Loop over each index in the array (slow!)
for row in range(A.shape[0] ) :
for col in range(A.shape[1] ) :
for slice in range(A.shape[2] ):
fit = np.polyfit( X, A[row,col,slice,:], degree )
regressions[row,col,slice] = fit
我想进入regressions
数组而不必经历所有的循环。这可能吗?