Reputation: 105
Good morning experts,
I have an array which contain integer numbers, and I have a list with the unique values that are in the array sorted in special order. What I want is to make another array which will contain the indexes of each value in the a array.
#a numpy array with integer values
#size_x and size_y: array dimensions of a
#index_list contain the unique values of a sorted in a special order.
#b New array with the index values
for i in xrange(0,size_x):
for j in xrange(0,size_y):
b[i][j]=index_list.index(a[i][j])
This works but it takes long time to do it. Is there a faster way to do it?
Many thanks for your help
German
Upvotes: 0
Views: 143
Reputation: 8975
Didn't try, but as this is pure numpy, it should be much faster then a dictionary based approach:
# note that the code will use the next higher value if a value is
# missing from index_list.
new_vals, old_index = np.unique(index_list, return_index=True)
# use searchsorted to find the index:
b_new_index = np.searchsorted(new_vals, a)
# And the original index:
b = old_index[b_new_index]
Alternatively you could simply fill any wholes in index_list.
Edited code, it was as such quite simply wrong (or very limited)...
Upvotes: 1
Reputation: 375485
The slow part is the lookup
index_list.index(a[i][j])
It will be much quicker to use a Python dictionary for this task, ie. rather than
index_list = [ item_0, item_1, item_2, ...]
use
index_dict = { item_0:0, item_1:1, item_2:2, ...}
Which can be created using:
index_dict = dict( (item, i) for i, item in enumerate(index_list) )
Upvotes: 2