简体   繁体   English

计算嵌套字典中列表的均值

[英]Calculate de mean of a list inside of a Nested Dictionary

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:打开pickle文件就像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.其中“amat”是数据集的名称(dict 中有 9 个数据集),test_size 是我的预测(时间序列的预测)的长度,raw 是模型(dict 中有 6 个模型)和_0 是我跑步的时间。 I run each model 10 times(0 to 9).我运行每个模型 10 次(0 到 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.我想为 test_size 和每个模型获得一个时间序列,以及我运行每个模型的 9 次的平均值。

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.谢谢你的帮助。

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)

尝试打印变量值,它可能是一个字符串

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM