Python 将网格搜索中的cv#u结果保存为csv
我想将网格搜索中使用的分数和参数保存为csv文件。问题是,所有参数都保存在同一列中。以下是一个例子:Python 将网格搜索中的cv#u结果保存为csv,python,csv,grid-search,Python,Csv,Grid Search,我想将网格搜索中使用的分数和参数保存为csv文件。问题是,所有参数都保存在同一列中。以下是一个例子: param_grid=dict(batch_size=batch_size, epochs=epochs) grid = GridSearchCV(estimator=model, param_grid=param_grid, n_jobs=-1, cv=3) grid_result = grid.fit(x_train, y_train, validation_data=(x_test, y
param_grid=dict(batch_size=batch_size, epochs=epochs)
grid = GridSearchCV(estimator=model, param_grid=param_grid, n_jobs=-1, cv=3)
grid_result = grid.fit(x_train, y_train, validation_data=(x_test, y_test), callbacks=[es])
means = grid_result.cv_results_['mean_test_score']
stds = grid_result.cv_results_['std_test_score']
param = grid_result.cv_results_['params']
exportfile='/Users/test.csv'
with open(exportfile, 'w', newline='') as file:
writer = csv.writer(file)
for mean, stdev, param in zip(means, stds, params):
writer.writerow([mean, stdev, param])
参数的输出如下所示:
{'batch_size':40,'epochs':1000}
但我想把它简化为:
40,1000如果
参数={'batch_size':40,'epochs':1000}
那就做吧
列表(params.values())
这将是[401000]
如果params
是一个散列数组,即:[{'batch\u size':40,'epochs':1000},{'batch\u size':30,'epochs':1100}]
则可以执行以下操作:
list(map(lambda p:list(p.values()),params))
这将导致:[[40,1000],[30,1100]
您好,您的第一行代码似乎很有希望。但是当像这样添加我的COLD:for mean,stdev,param in zip(means,stds,params):writer.writerow([mean,stdev,list(param.values())])时,我的CSV文件中的结果如下所示:“[30,0.0,0.0,1000,0.001,400,700,600]”