python - Conversion: np.array of indices to np.array of corresponding dict entries -


i have numpy array of indices in python 2.7 correspond value in dictionary. want create numpy array of corresponding values dictionary. code might clear immediately:

import numpy np indices = np.array([(0, 1), (2, 0), (2, 0)], dtype=[('a', int), ('b', int)]) d = {(0, 1): 10,      (2, 0): 9} values = d[(indices['a'], indices['b'])] 

the call in last line not hashable (i tried find way make np.array hashable did not work):

typeerror: unhashable type: 'numpy.ndarray' 

i substitute loop takes ages write variable values:

np.array([d[(indices[i]['a'], indices[i]['b'])] in range(len(indices))]) 

or there alternative dict make such task pythonic, i.e. faster? variable indices can not changed can change type of dict.

edit

the actual index array contains other entries. why wrote calls complicated:

indices = np.array([(0, 1, 's'), (2, 0, 's'), (2, 0, 't')],                    dtype=[('a', int), ('b', int), ('c', str)]) 

i believe can use list comprehension (it bit faster normal for loop method). example -

values = [d[tuple(a)] in indices] 

please note, using d instead of dict, since not recommended use dict variable name, shadow built-in type dict.

demo -

in [73]: import numpy np  in [74]: indices = np.array([(0, 1), (2, 0), (2, 0)], dtype=[('a', int), ('b', int)])  in [76]: d = {(0, 1): 10,    ....:  (2, 0): 9}  in [78]: values = [d[tuple(a)] in indices]  in [79]: values out[79]: [10, 9, 9] 

a faster method larger arrays use np.vectorize() vectorize dict.get() method , apply on indices array. example -

vecdget = np.vectorize(lambda x: d.get(tuple(x))) vecdget(indices) 

demo timing results -

in [88]: vecdget = np.vectorize(lambda x: d.get(tuple(x)))  in [89]: vecdget(indices) out[89]: array([10,  9,  9])  in [98]: indices = np.array([(0, 1), (2, 0), (2, 0)] * 100, dtype=[('a', int), ('b', int)])  in [99]: %timeit [d[tuple(a)] in indices] 100 loops, best of 3: 1.72 ms per loop  in [100]: %timeit vecdget(indices) 1000 loops, best of 3: 341 µs per loop 

timing test new method suggested @hpaulj in comments - [d.get(x.item()) x in indices] -

in [114]: %timeit [d.get(x.item()) x in indices] 1000 loops, best of 3: 417 µs per loop  in [115]: %timeit vecdget(indices) 1000 loops, best of 3: 331 µs per loop  in [116]: %timeit [d.get(x.item()) x in indices] 1000 loops, best of 3: 354 µs per loop  in [117]: %timeit vecdget(indices) 1000 loops, best of 3: 262 µs per loop 

Comments

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -