在python/numpy中进行优雅的网格搜索 [英] Elegant grid search in python/numpy

查看:312
本文介绍了在python/numpy中进行优雅的网格搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有一堆参数的函数.我要执行网格搜索,而不是手动设置所有参数.我为每个参数列出了可能的值.对于每种可能的参数组合,我都想运行我的函数,该函数报告我的算法在这些参数上的性能.我想将其结果存储在多维矩阵中,以便后续工作可以找到性能最高的指标,这反过来又可以为我提供最佳的参数.现在是这样编写代码的方式:

I have a function that has a bunch of parameters. Rather than setting all of the parameters manually, I want to perform a grid search. I have a list of possible values for each parameter. For every possible combination of parameters, I want to run my function which reports the performance of my algorithm on those parameters. I want to store the results of this in a many-dimensional matrix, so that afterwords I can just find the index of the maximum performance, which would in turn give me the best parameters. Here is how the code is written now:

param1_list = [p11, p12, p13,...]
param2_list = [p21, p22, p23,...] # not necessarily the same number of values
...

results_size = (len(param1_list), len(param2_list),...)
results = np.zeros(results_size, dtype = np.float)

for param1_idx in range(len(param1_list)):
  for param2_idx in range(len(param2_list)):
    ...
    param1 = param1_list[param1_idx]
    param2 = param2_list[param2_idx]
    ...
    results[param1_idx, param2_idx, ...] = my_func(param1, param2, ...)

max_index = np.argmax(results) # indices of best parameters!

我想保留第一部分,即按原样定义列表,因为我希望能够轻松地操纵搜索值.

I want to keep the first part, where I define the lists as-is, since I want to easily be able to manipulate the values over which I search.

我还希望按原样结束结果矩阵,因为我将可视化更改不同的参数如何影响算法的性能.

I also want to end up with the results matrix as is, since I will be visualizing how changing different parameters affects the performance of the algorithm.

但是,中间的位是相当重复和庞大的(特别是因为我有很多参数,并且我可能想添加或删除参数),我觉得应该有一种更简洁/优雅的方法初始化结果矩阵,遍历所有索引,并设置适当的参数.

The bit in the middle, though, is quite repetitive and bulky (especially because I have lots of parameters, and I might want to add or remove parameters), and I feel like there should be a more succinct/elegant way to initialize the results matrix, iterate over all of the indices, and set the appropriate parameters.

那么,在那里吗?

推荐答案

您可以使用sklearn模块中的ParameterGrid

You can use the ParameterGrid from the sklearn module

http://scikit-learn.org/stable/modules/generation/sklearn.grid_search.ParameterGrid.html

示例

from sklearn.grid_search import ParameterGrid
param_grid = {'param1': [value1, value2, value3], 'paramN' : [value1, value2, valueM]}

grid = ParameterGrid(param_grid)

for params in grid:
    your_function(params['param1'], params['param2'])

这篇关于在python/numpy中进行优雅的网格搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆