The 365 Data Science team is proud to invite you to our own community forum. A very well built system to support your queries, questions and give the chance to show your knowledge and help others in their path of becoming Data Science specialists.
Ask
Anybody can ask a question
Answer
Anybody can answer
Vote
The best answers are voted up and moderated by our team

How to define a 2d array?

How to define a 2d array?

0
Votes
2
Answer

There is some problem during defining 2d array:

model_arma = [[0 in x in range(10)] for y in range(10)]
results_arma = [[0 in x in range(10)] for y in range(10)]
def arma(i,j):
model_arma[i][j]=ARMA(df.market_value, order=(i,j))
results_arma[i][j]=model_arma[i][j].fit()
return print("\n ARMA(",i,")(",j,"): \t LL=",
round(results_arma[i][j].llf,3),
"\t AIC= ", round(results_arma[i][j].aic,3))


This error is coming:
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-59-df8ddbd5de4e> in <module>
----> 1 model_arma = [[0 in x in range(10)] for y in range(10)]
      2 results_arma = [[0 in x in range(10)] for y in range(10)]
      3 def arma(i,j):
      4     model_arma[i][j]=ARMA(df.market_value, order=(i,j))
      5     results_arma[i][j]=model_arma[i][j].fit()

<ipython-input-59-df8ddbd5de4e> in <listcomp>(.0)
----> 1 model_arma = [[0 in x in range(10)] for y in range(10)]
      2 results_arma = [[0 in x in range(10)] for y in range(10)]
      3 def arma(i,j):
      4     model_arma[i][j]=ARMA(df.market_value, order=(i,j))
      5     results_arma[i][j]=model_arma[i][j].fit()

TypeError: argument of type 'int' is not iterable

What is my mistake?
2 Answers

365 Team
0
Votes

Hey Tushar,
 
Can you provide a little more feedback with what you’re trying to achieve in the first two lines?
 
It looks like you’re trying to set up two “empty” 10 by 10 lists rather than arrays. If you want to define two such arrays, you can do so with the help of the numpy package and add:
 

import numpy as np
model_arma = np.empty(shape = (10,10))
results_arma = np.empty(shape = (10,10))

 
Hope this helps!
Best,
365 Vik

Thank you.

3 weeks

If I want to replicate the below code, but in the form of 2d array(or lists), so that I don't have to use the x variable, how will I do it? I don't want to initialize any fixed value for either rows or column:->

model_arma = {}
results_arma = {}
def arma(i,j):
x=10*i+j
model_arma[x]=ARMA(df.market_value, order=(i,j))
results_arma[x]=model_arma[x].fit(start_ar_lags=11)
return print("\n ARMA(",i,",",j,"): \t LL=",
round(results_arma[x].llf,3),
"\t AIC= ", round(results_arma[x].aic,3))

3 weeks

Hey Tushar, I’ll provide the answer in another “Answer” to this thread, rather than another “Comment”, since it’ll allow me to format the code properly.

3 weeks

365 Team
0
Votes

Hello again!

If you want to use lists, you can do so the following way:

model_arma = []          # Define model_arma as a list
results_arma = []        # Define results_arma as a list

def arma(i,j):
    model_arma.append(ARMA(df.ret_vol, order = (i,j))) # The append method adds the input to the back of the list
    results_arma.append(model_arma[-1].fit(start_ar_lags = 11)) # [-1] will always refer to the latest value we just added to the list
    
    return print("\n ARMA(",i,",",j,"): \t LL=",
                 round(results_arma[-1].llf,3),
                 "\t AIC= ", round(results_arma[-1].aic,3))

Hope this helps!
Best,
365 Vik

P.S. Additionally, if you want to run multiple different ARMA models simultaneously, you can check out the AutoARIMA section of the course. 

Thank you so much, it works.

3 weeks