Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regression along a dimension in a numpy array

Tags:

python

numpy

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?

like image 531
ajwood Avatar asked Oct 09 '13 20:10

ajwood


People also ask

How do you do regression in NumPy?

Linear Regression using NumPyStep 1: Import all the necessary package will be used for computation . Step 2 : Read the input file using pandas library . Step 4: Convert the pandas data frame in to numpy array . Step 5: Let's assign input and target variable , x and y for further computation.

Does NumPy support multidimensional array?

In general numpy arrays can have more than one dimension. One way to create such array is to start with a 1-dimensional array and use the numpy reshape() function that rearranges elements of that array into a new shape.

What is the dimension of a NumPy array?

NumPy's main object is the homogeneous multidimensional array. It is a table of elements (usually numbers), all of the same type, indexed by a tuple of non-negative integers. In NumPy dimensions are called axes. For example, the array for the coordinates of a point in 3D space, [1, 2, 1] , has one axis.

How do dimensions work in NumPy?

In Numpy dimensions are called axes. The number of axes is rank. For example, the coordinates of a point in 3D space [1, 2, 1] is an array of rank 1, because it has one axis. That axis has a length of 3.


1 Answers

Reshape your data such that each individual slice is on a column of a 2d array. Then run polyfit 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 really understand what all of the dimensions correspond to in your dataset, so I'm not sure exactly what shape you will want. But the point is, each individual dataset 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 the profile module or something like that. Generally speaking, you can't always predict how quickly code will run just by eyeballing it. You have to run it. Although in this case removing the loops will also make your code 100x more readable, which is even more important.

like image 134
cxrodgers Avatar answered Oct 01 '22 07:10

cxrodgers