chyikwei / recommend

recommendation system with python
310 stars 118 forks source link

can 'numpy.take()' be replaced by “fancy” indexing (indexing arrays using arrays)? #21

Closed zhaok12 closed 6 years ago

zhaok12 commented 6 years ago

why using numpy.take() rather than “fancy” indexing (indexing arrays using arrays) , for example in pmf.py, while the “fancy” indexing is much faster.

chyikwei commented 6 years ago

I remember numpy.take() is faster than fancy index.

In [3]: import numpy as np
   ...: import random
   ...:
   ...: arr = np.random.randn(10000, 5)
   ...: indexer = np.arange(10000)
   ...: random.shuffle(indexer)
   ...:

In [4]: timeit arr[indexer]
1000 loops, best of 3: 241 µs per loop

In [5]: timeit arr.take(indexer, axis=0)
10000 loops, best of 3: 96.2 µs per loop

In [6]: timeit np.take(arr, indexer, axis=0)
10000 loops, best of 3: 101 µs per loop

Reference: http://wesmckinney.com/blog/numpy-indexing-peculiarities/

chyikwei commented 6 years ago

close this now