3 ms·
So in addition to what akasaka said (another thumbs up for line profiler from me, great tool) this isn’t a problem with linalg.norm being slow. It’s plenty fast
by brahbrah 4y ago
So in addition to what akasaka said (another thumbs up for line profiler from me, great tool) this isn’t a problem with linalg.norm being slow. It’s plenty fast, but calling it thousands of separate times in a Python loop will be slow. This is more just about learning how to vectorize properly. If you’re working in numpy land and you’re calling a numpy function in a loop that’s iterating over more than a handful of items, chances are you’re not vectorizing properly
- akasakahakada 4y agoI realized that I should say more specifically about numpy usage. If you see a piece of code like this, it rings the bell that the person has no idea what he/she is doing: Bad Pattern: my_list = np.array(xxx) summed = [] for row in my_list: summed.append(np.sum(row)) Worst Pattern: my_list = np.array(xxx) def get_summed(arr): return np.sum(arr) summed = [] for i in range(len(my_list)): summed.append( get_summed(my_list[i]) ) Perfered: # np.array(xxx) is redundant summed = np.sum(xxx, axis=1) Same applies to all numpy operations.