LoginSignup
0
1

More than 3 years have passed since last update.

おしゃれ並列Grid Search

Last updated at Posted at 2019-04-17

Joblibでぱぱっと書きました

def job(X, y, params):
    ...

def grid_search(X, y, search_space, n_jobs=-1):
    search_space = [[(k, v) for v in space]
                    for k, space in search_space.items()]

    tasks = []
    for params in product(*search_space):
        tasks.append({
            'job': delayed(job)(X, y, *[p for _, p in params]),
            **dict(params)
        })

    with Parallel(n_jobs=n_jobs) as p:
        results = p([task.pop('job') for task in tasks])

    results = [{**result, **task} for task, result in zip(tasks, results)]
    results = pd.DataFrame(results)
    return results

X, y = get_data()

search_space = {
    'param_a': [1, 3, 5, 7],
    'param_b': ['a', 'b', 'c'],
    ...,
}

results = grid_search(X, y, search_space)
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1