Reputation: 3609
I have to compare data between 3 or more arrays that look like this:
{'Class1': {'diag': {'Class1': array([-138.49760438, -133.93161498, ...]),
'Class2': array([-20.78214408, -19.33358523, ...])
'Class3': array([-338.81699294, -345.05932059, ...])}
}
}
I want to compare the first 3 values in each array, search for the biggest number and output the corresponding class (1,2 or 3) into a new array. Then go to the second 3 values and do the same. Each array contains around 100,000 values so the process should be fairly fast.
In the above example I should end up with the following array: ['Class2', 'Class2'].
I only found functions that work with 2 arrays but not more. In fact I need it to work with any number of arrays as the classes will increase during my experiments.
I'm looking forward to hearing your ideas!
Upvotes: 0
Views: 109
Reputation: 42421
from itertools import izip
from operator import itemgetter
# Ignoring the outer levels of the dict and shortening the numbers.
data = {
'Class1' : [-138, -133, 33, 999],
'Class2' : [ -20, -19, 100, 777],
'Class3' : [-338, -345, 200, 111],
}
lookup = dict(enumerate(data.keys()))
max_index = lambda ns: max(enumerate(ns), key = itemgetter(1))[0]
# Zip the arrays together.
# For each zipped-set of numbers, find the index of the max.
# Use that index to look up the class name.
classes = [lookup[max_index(ns)] for ns in izip(*data.values())]
print classes # ['Class2', 'Class2', 'Class3', 'Class1']
Upvotes: 2
Reputation: 2287
Assuming your arrays have the same length, you iterate from 0 to len(array1). In each iteration i you build a list from the i-th elements of all your arrays. Then you find the biggest element and its index in the resulting list using something like:
import operator
index, value = max(enumerate(my_list), key=operator.itemgetter(1))
The index corresponds to the class your max-value is from e.g. 0 --> class1, 1--->class2 and so on. Thus the time-complexity of your solution should be linear in the length of the arrays (and the number of class, depending on the implementation of max).
Upvotes: 1