Size regression in numpy array

I have a 4-dimensional numpy array (x, y, z, time) and would like to make numpy.polyfit through a 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 would like to get into the regressions array without going through the whole loop. Is it possible?

+7
python numpy
source share
1 answer

Modify the data so that each individual slice is in a column of the 2d array. Then run the polyphyte once.

 A2 = A.reshape(time_len, -1) regressions = np.polyfit(X, A2, degree) regressions = regressions.reshape(A.shape) 

Or something like that ... I don’t quite understand that all sizes correspond to your data set, so I’m not sure exactly what shape you want. But the fact is that each separate data set for polyfit should occupy a column in the matrix A2 .

By the way, if you are interested in performance, then you should profile your code using a profile module or something like that. Generally speaking, you cannot always predict how fast code will work just by looking at it. You must run it. Although in this case, deleting loops will also make your 100x code more readable, which is even more important.

+9
source share

All Articles