Ruby
Ruby

Reputation: 41

how to append more than one list of time series correlation in python?

I want to take correlation between two time series. I used np.correcoef to get the correlations as list. Now I introduced lag suppose 3, how to save 3 different list for 3 different lags.

I tried

 for k in range(0,4,1):
      corr_k= [] 
      corr_k.append( np.corrcoef ( T[(365-k):(730-k)] ,T[365:730]) )

I want my result as corr_0 , corr_1, ... for respective lags But I am getting corr_k only. I need to make adjacency matrix after this step for each k value. I am using netCDF file as m data and T is temperature. Any idea how to do that? thank you.

Upvotes: 0

Views: 174

Answers (1)

jeannej
jeannej

Reputation: 1216

EDITED WITH MINIMUM EXAMPLE

import numpy as np
T = np.random.rand(100) #defines a dummy T with size 100
corr = {}
for k in range(4):
   corr[k] = np.corrcoef(T[50-k:100-k],T[50:100])

returns a dictionary corr containing arrays. For instance :

corr[0]
> array([[1., 1.],
       [1., 1.]])

If this is not applicable to your case, maybe the issue is in the corrcoef, not the "appending" part!


EDITED WITH DICT

When you ask for corr_k in your loop, python has no way of knowing that you want k as a number. So it consider that your variable name is corr_k and not corr_0 and so on as you would like. Which is hopeful, think about the nightmare it would be if python changed all k character on its own! So, to specify a variable name which changes with the loop index, use a dictionary (as in this answer) and store the lists in it:

corr = {}
 for k in range(4):
      corr[k] = np.corrcoef ( T[(365-k):(730-k)] ,T[365:730] ) 

Then you will have your outputs with: corr[0], corr[1], corr[2], corr[3].


It is difficult to test your code as we do not have T here, but as a first insight I see that for each step your are defining again corr_k=[], hence overwriting your list rather than appending it. Try:

corr_k= [] 
for k in range(4):
      corr_k.append( np.corrcoef ( T[(365-k):(730-k)] ,T[365:730]) )

Upvotes: 0

Related Questions