model_names = ['is1', 'is5', 'is10', 'im1', 'im5', 'im10']
As an example,
models = [0.1, 0.2, 0.1, 0.3, 0.2, 0.3]
The maximum of the list is 0.3, which corresponds to model names 'im1' and 'im10'. I'd like the function
dominant_model(models)
to return
['im1', 'im10']
i.e, the most 'dominant' model(s).
Here's my code which does the job, but I'd like to know if there's a shorter way of doing the same thing rather than all of the conditionals:
def dominant_model(models):
m = max(models)
dom_models = [i for i,j in enumerate(models) if j==m]
for i in range(len(dom_models)):
if dom_models[i]==0:
dom_models[i]=model_names[0]
elif dom_models[i]==1:
dom_models[i]=model_names[1]
elif dom_models[i]==2:
dom_models[i]=model_names[2]
elif dom_models[i]==3:
dom_models[i]=model_names[3]
elif dom_models[i]==4:
dom_models[i]=model_names[4]
elif dom_models[i]==5:
dom_models[i]=model_names[5]
return dom_models
After finding the max m = max(model), zip the model names to the values and take the names where the value is equal to m .
def dominant(names, values):
m = max(values)
return [name for name, value in zip(names, values) if value == m]
Demo
>>> model_names = ['is1', 'is5', 'is10', 'im1', 'im5', 'im10']
>>> model_values = [0.1, 0.2, 0.1, 0.3, 0.2, 0.3]
>>> dominant(model_names, model_values)
['im1', 'im10']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With