Just understanding List Comprehensions, as in a recent interview the tech guy asked me this question and being a self learner I answered lambda which is NOT list comprehension.
let say we have a time series data "Shiller", http://us.spindices.com/indices/real-estate/sp-corelogic-case-shiller-us-national-home-price-nsa-index
I calculated aic/bic using following loop:
shiller = [please use some random data or use the link above]
import matplotlib.pyplot as plt
import statsmodels.api as sm
import pandas as pd
def aicbic(shiller):
arimaijk = []
aicijk = []
bicijk = []
index = []
for i in range(1,3):
for j in range(1,2):
for k in range(0,5):
arimaijk.append(sm.tsa.ARIMA(shiller,(i,j,k)).fit())
index.append([i,j,k])
aicijk.append(arimaijk[k].aic)
bicijk.append(arimaijk[k].bic)
return aicijk, bicijk
aicbic(shiller)
Out[9]:
([-235.77314152121426,-233.9375761653174,-233.3841011331017,-241.65994870973782,-240.2975620564456,-235.77314152121426,-233.9375761653174,-233.3841011331017,-241.65994870973782,-240.2975620564456],
[-227.98778197081049,-223.55709676477906,-220.40850188242874,-226.08922960893028,-222.13172310550345,-227.98778197081049,-223.55709676477906,-220.40850188242874,-226.08922960893028,-222.13172310550345])
Now, I want this result using List Comprehension, so I wrote following lines, which are returning error:
def aicbic(data):
arimaijk = []
aicijk = []
bicijk = []
index = []
[(sm.tsa.ARIMA(data,(i,j,k)).fit(),index.append([i,j,k]),\
aicijk.append(arimaijk[k].aic),bicijk.append(arimaijk[k].bic)) \
for i in range(1,3) for j in range(1,2) for k in range(0,5)]
IndexError: list index out of range