I've got a 4-dimensional numpy array (x,y,z,time) and would like to do a numpy.polyfit
through the time dimension, at each x,y,z coordinate. For example:
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
I'd like to get to the regressions
array without having to go through all of the looping. Is this possible?