## creating and manipulating 3d arrays

This is the place for queries that don't fit in any of the other categories.

### creating and manipulating 3d arrays

I am (very) new to python, previously a Matlab user.

I try to create code to load times series into the rows of a matrix [serie1, 2, 3, etc.] and then manipulate those time series into a loop (see code below). My problem is threefold (3 loops below):

1-how to create a loop to fill the array's rows one by one. 2-fill a 3d array of with stuff I calculate off those time series (correlation coefficients) 3-calculate the average correlation of the lower triangular matrix for each time period

what i am doing wrong?

thanks a lot for your help!

Code: Select all
i=0
while i<length(fx_tech):
for f in fx_tech:
i=1+i

#i want to populate a matrix with all series, fx_tech is a list of series i have
#to feed through the "loadseries" command. I know the first set of loops is wrong.
#then loop over each series pair and populate the matrix "corr"

corr = zeros((len(time_series)-1,len(fx_tech),len(fx_tech)))  #allocate matrix

i=0
j=0
while i < len(fx_tech):
while j < len(fx_tech):
corr[:,i,j] = gencorrelation(time_series[:,i],time_series[:,j],lambda1)
j=1+j
i=1+i

#finally calculate the average of the elements below the diagonal for each time period.

mean_fx = []
i=0
while i < len(time_series)-1:
diag[:,:] = corr[i,:,:]-eye(len(fx_tech))
mean_fx.append = sum(diag)/((len(fx_tech)**2)/2-len(fx_tech))
i=1+i
ebordeleau

Posts: 1
Joined: Thu Jun 06, 2013 3:32 pm

### Re: creating and manipulating 3d arrays

There is a lot missing (e.g. what are you loading, there are a lot of function/module calls here I don't recognize), which is making it difficult to help.

I am a semi-former MatLab user, and have made a semi-complete switch to Python. One thing to get over is calling things a matrix. In numpy we typically deal with arrays, which are quite different than a matrix. In your case, you will have to stick with arrays since matrices in Python are limited to two dimensions.

It looks like you are populating your array more or less correctly:

You say fx_tech is a list, so I would do away with the while loop (I don't see what it adds):

Code: Select all
import numpy as np
time_series = ones( (rows,0) ) # Where rows is the expected number of rows in your time series
for f in fx_tech:
time_series = np.hstack( (time_series, LoadSeries(xxx)) )

So there is a problem doing it this way. If you are dealing with large data sets the constant re-shaping of your array will slow you down substantially. Otherwise, you could do:

Code: Select all
import numpy as np
time_series = ones( (rows,cols) )
for f in xrange(len(fx_tech)-1):

I wrote the above code assuming that the output of LoadSeries() is a 2d array. If it is a list, convert it to an array by:
Code: Select all
out = np.atleast_2d( np.array( LoadSeries() ) )

Since conversion to an array via np.array() will most likely give you a one-dimensional array (I know, we NEVER encounter those in MatLab) you will have to convert it to 2d via np.atleast_2d(). After conversion, and prior to inserting into time_series make sure the orientation of "out" is correct. If it is not, use out.T to transpose.

I have to go, but hopefully that gets you started. If you provide more detail, including all imports and any errors you get, you will get more help.
Python: 2.7 via Anaconda
Numpy: 1.7
Pandas: 0.11
OS: Windows 7
IDE: Spyder/IPython

tnknepp

Posts: 153
Joined: Mon Mar 11, 2013 7:41 pm