0

Need to find a faster alternative to python's dict(zip(list1,list2))

I'm using dict(zip(list1,list2)) in a script for random forest classification which creates a dictionary from two lists. This takes about 0.002 seconds per execution. However its too slow for bulk prediction(when it needs to be executed 80K+ times)

for inp in listOfInputs[:]:
    pp = clf.predict_proba(inp)[0]  # clf is the classifier
    probaDict = dict(zip(clf.classes_,pp))

This loop takes 0.6 seconds if len(listOfInputs)=290 which is slow. I need an efficient alternative for use with large bulk inputs for which len(listOfInputs)=80,000+

chepner
  • 497,756
  • 71
  • 530
  • 681
  • 1
    Please check this out. Seems to be already happended exhaustive discussion on this. https://stackoverflow.com/questions/209840/convert-two-lists-into-a-dictionary-in-python – Jim Todd Feb 16 '19 at 14:08
  • Yes, I've seen that page but i'm using the fastest technique from them. Still isn't efficient enough! Need another way – EmperorPenguin Feb 16 '19 at 14:11
  • Why are you making a copy of `listOfInput`? `for int in listOfInputs`. You need to confirm that the call to `dict` is actually your bottleneck. – chepner Feb 16 '19 at 14:18

1 Answers1

0

I guess classes are fixed? So try re-using the same dictionary and change the key values istead of constructing a new dict every iteration

orli
  • 181
  • 1
  • 5