I have a nested dictionary, that I transformed in a pickle file. The pickle file can be found here. To open the pickle file is just like thar:
import pickle
resultados_acoes_testvar = pickle.load(open('map_results_modelo_acoes_variandotest.pickle', 'rb'))
The file is a dictionary, with that structure:
{'amat': {'Test_Size_100': {'raw_0': array([1.39838652e 02, 1.42292998e 02, 1.45314363e 02, 1.49162546e 02....)]}}}
Where "amat" is the name of the dataset(it has 9 datasets in the dict), test_size is the length of my prediction(prediction of a time serie), and raw is the model(it has 6 models in the dict) and the _0 is the time that I run. I run each model 10 times(0 to 9).
I would like to to get one time serie and for test_size and each model, with the mean of the nines times that I run each model.
I'm trying to do that way:
resultado = {}
lista_modelos = ['raw','difference', 'logaritmica', 'box_cox', 'mas', 'pct']
for acao in resultados_acoes_testvar_transformadas.keys():
resultado[acao] = {}
for testsize in resultados_acoes_testvar_transformadas[acao].keys():
resultado[acao][testsize] = {}
for values in resultados_acoes_testvar_transformadas[acao][testsize].keys():
for prefix in lista_modelos:
resultado[acao][testsize][prefix] = []
for a, b, c, d, e, f, g, h, i, j in zip(values[prefix '_0'],values[prefix '_1'],values[prefix '_2'],values[prefix '_3'],values[prefix '_4'],values[prefix '_5'],values[prefix '_6'],values[prefix '_7'],values[prefix '_8'],values[prefix '_9']):
mean = float((a b c d e f g h i j)/10)
resultado[acao][testsize][prefix].append(mean)
But I'm getting a error:
TypeError Traceback (most recent call last)
<ipython-input-59-80ef15c9251e> in <module>
19
20
---> 21 for a, b, c, d, e, f, g, h, i, j in zip(values[prefix '_0'],values[prefix '_1'],values[prefix '_2'],values[prefix '_3'],values[prefix '_4'],values[prefix '_5'],values[prefix '_6'],values[prefix '_7'],values[prefix '_8'],values[prefix '_9']):
22
23 mean = float((a b c d e f g h i j)/10)
TypeError: string indices must be integers
Thanks for your help.
CodePudding user response:
Can you check if this works:
lista_modelos = ['raw','difference', 'logaritmica', 'box_cox', 'mas', 'pct']
for acao in resultados_acoes_testvar_transformadas.keys():
resultado[acao] = {}
for testsize in resultados_acoes_testvar_transformadas[acao].keys():
resultado[acao][testsize] = {}
for values in resultados_acoes_testvar_transformadas[acao][testsize].keys():
for prefix in lista_modelos:
resultado[acao][testsize][prefix] = []
subd = resultados_acoes_testvar_transformadas[acao][testsize] # <- HERE
for a, b, c, d, e, f, g, h, i, j in zip(subd[prefix '_0'],subd[prefix '_1'],subd[prefix '_2'],subd[prefix '_3'],subd[prefix '_4'],subd[prefix '_5'],subd[prefix '_6'],subd[prefix '_7'],subd[prefix '_8'],subd[prefix '_9']):
mean = float((a b c d e f g h i j)/10)
resultado[acao][testsize][prefix].append(mean)
CodePudding user response:
try printing the variable values, it's probably a string
CodePudding user response:
The problem is that values
is a string, not a dictionary like you try to use it. keys()
returns a list of strings. I suggest you use items()
instead to get the key, value pairs from the dictionary you are iterating. This will also let you avoid the long indexing syntax from the root data structure.