0

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.

Ruby
  • 39
  • 5
  • 1
    your are re-initializing `corr_k` at each iteration, hence you only get the last one at the end. Try to define `corr_k=[]` before the loop – jeannej Sep 16 '19 at 20:26

1 Answers1

0

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]) )
jeannej
  • 1,135
  • 1
  • 9
  • 23
  • No .. it is not working. I got the total corr_k at the end of the loop, instead of corr_0, corr_1,... Actually I need to make adjacency matrix from list after this step. My data is actually netcdf files and T is temperature. – Ruby Sep 17 '19 at 06:28
  • No ... I am still not getting my desired result. I am getting corr_0 and so on as a single digit result, but I am expecting a list instead. I explained my problem in details here [https://stackoverflow.com/questions/57972598/how-to-convert-any-arbitrary-list-to-an-adjaceny-matrix-in-python] – Ruby Sep 17 '19 at 17:51
  • @Ruby with the dictionary, your output is `corr[0]`, not `corr_0`. Please clarify for *this* question your input (with a minimal working example for T) and desired output, we cannot help you as it is – jeannej Sep 17 '19 at 19:11
  • Sorry for typo mistake, I got corr[0] as a single digit. Here is the link for T [https://drive.google.com/drive/folders/1E-hGmdhCa7zQ3nx_u9hLNxdlE7WKr1de?usp=sharing] – Ruby Sep 17 '19 at 19:17
  • I edit my code with a minimal working example, you should **not** give your real data here! For your future questions, please check [this page](https://stackoverflow.com/help/minimal-reproducible-example) – jeannej Sep 17 '19 at 19:44
  • @Ruby make sure that you are calculating what you think with your `corrcoef` : print your `T[(365-k):(730-k)]` for instance to know what you are asking – jeannej Sep 17 '19 at 19:56