简体   繁体   English

将字典写入 csv 文件,其中两个项目彼此相邻,而不是在 python 中彼此下方

[英]Writing a dictionory into a csv file with both items next to eachother instead of below eachother in python

I think this is a simple question but I have been looking for so long and havent found a solution to my problem.我认为这是一个简单的问题,但我一直在寻找这么久,还没有找到解决我的问题的方法。

so I have a function that is being called multiple times(objective function in my code).所以我有一个被多次调用的 function(我的代码中的目标 function)。 Each time a set of parameters are choosen and the time of running my code is calculated.每次选择一组参数并计算运行我的代码的时间。 Now I want to write the set of parameters together with their belonging time value into a csv file.现在我想将这组参数连同它们所属的时间值一起写入一个 csv 文件中。

The parameters are stored in a list and the value is a simple float.参数存储在列表中,值是一个简单的浮点数。 Toegether I store them in a dictionary.我一起将它们存储在字典中。 Now when I write them to the csv file, I want the parameters to be stored in one cell and the belonging time value in the cell next to it.现在,当我将它们写入 csv 文件时,我希望将参数存储在一个单元格中,并将所属时间值存储在旁边的单元格中。 It will make it easy for me to sort by value and find the parameters that led to the highest time values.这将使我很容易按值排序并找到导致最高时间值的参数。 (Ideally the different parameters are stored in seperated cells as well but that is not that important) (理想情况下,不同的参数也存储在单独的单元格中,但这并不重要)

So I what I want would look like this:所以我想要的看起来像这样:

Parameters参数 Time时间
1,2,3 1,2,3 1 s 1 秒
10,20,30 10,20,30 2 s 2 秒

Or even better would be:或者更好的是:

Parameter A参数 A Parameter B参数 B Parameter c参数 c Time时间
1 1 2 2 3 3 1s 1s
10 10 20 20 30 30 2s 2s

What I have now is this:我现在拥有的是这样的:

Parameters参数 Another header另一个header
1,2,3 1,2,3 - -
- - - -
1s 1s - -
- - - -
10,20,30 10,20,30 - -
- - - -
2s 2s - -
- - - -

My Code now is looking like this:我的代码现在看起来像这样:

def objective(trial):

    start = time.perf_counter
    
    # Here some Parameters are choosen 

    # Here happens some calculations

    end = time.perf_counter()

    duration = end-start

    params_csv=[batch_size,model_type,n_layers,n_hidden,weight_decay,dropout,lr,eta_min,T0,T_mult]
    dict_csv = {"Params": params_csv,"Time": duration} 

    out_file = 'optuna_time_trials_9.csv'
    of_connection = open(out_file, 'a')
    writer_params = csv.writer(of_connection)

    for key, value in dict_csv.items():
        writer_params.writerow([key, value])
    return foo

study = optuna.create_study(direction='minimize',study_name='Optuna_Parameter_Time_11', storage='sqlite:///example2.db', load_if_exists=True )
study.optimize(objective, n_trials=15)

You should just use the list of parameters as the list to write with writerow() .您应该只使用参数列表作为使用writerow()编写的列表。 And add duration to the end of the list to add that column to the CSV.并将duration添加到列表末尾以将该列添加到 CSV。

There's no need for the dictionary.不需要字典。

def objective(trial):

    start = time.perf_counter
    
    # Here some Parameters are choosen 

    # Here happens some calculations

    end = time.perf_counter()

    duration = end-start

    out_file = 'optuna_time_trials_9.csv'
    with open(out_file, 'a') as of_connection:
        writer_params = csv.writer(of_connection)
        writer_params.writerow([batch_size,model_type,n_layers,n_hidden,weight_decay,dropout,lr,eta_min,T0,T_mult,duration]

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

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