Multiprocessing simple way to map a dictionary

Viewed 32

I have a dictionary like so:

models = {
    'Dummy1': DummyClassifier(strategy="constant", constant=1),
    'Dummy2': DummyClassifier(strategy="constant", constant=0),
    'Dummy3': DummyClassifier(strategy="constant", constant=0.5)
}

And I want to parallelise using Pool.map or a related function:

with Pool(threads) as p:
    result = p.map(partial(cross_validate, X=X, y=y), models.values())

This works but completely disregards the keys of the models dictionary. The result is simply a list. How can I get the result to be a dictionary with the same keys as the original?

results = {
    'Dummy1': 'Result1',
    'Dummy2': 'Result2',
    'Dummy3': 'Result3'
}

I have considered going based on the order of models.items(), however the documentation is mute on the ordering of the list Pool.map returns so I don't know if I can rely on the order being the same.

1 Answers

Something like this should work:

models = {
    'Dummy1': DummyClassifier(strategy="constant", constant=1),
    'Dummy2': DummyClassifier(strategy="constant", constant=0),
    'Dummy3': DummyClassifier(strategy="constant", constant=0.5)
}


pairs = (k, v) for k, v in models.items()

with Pool(threads) as p:
    result_pairs = p.map(partial(cross_validate, X=X, y=y), pairs)

results = {k: v for k, v in result_pairs}
Related