Aside from the obvious imports I've added (import numpy as np, etc),
there are still undefined objects (`names`, `times_`, `feat`,
`bind_ind`) and indentation errors. Can you post a *working* code to be
sped up and a benchmarking test?
As you're using Python 2, you can improve the code with the following:
- Use `xrange` in place of `range`.
- Printing in loops dramatically slows things down.
- Loop directly over an iterable (and maybe use
`collections.defaultdict`):
# This:
def pos_per_user_(self):
pos_per_user = {}
for k, v in self.userItems.items():
for i in range(len(self.userItems[k])):
item__ = self.userItems[k][i][0]
if k not in pos_per_user:
pos_per_user[k] = [item__]
else:
pos_per_user[k].append(item__)
return pos_per_user
# Becomes like this:
from collections import defaultdict
def pos_per_user(self):
pos_per_user = defaultdict(list)
for k, v in self.userItems.iteritems():
for item in items:
pos_per_user[k].append(item[0])
return pos_per_user
- You also have too many list creations inside loops, which is also slow:
# If you do nothing, use xrange here for it will not construct
# a list. Avoid materializing a list if all you do is iterate.
for bin in range(10):
for i in range(len(self.list_of_items)):
for k in range(self.K2):
for f in range(4096):
--
~ Jugurtha Hadjar,
--
https://mail.python.org/mailman/listinfo/python-list