Mathias711
Mathias711

Reputation: 6668

Difference np.where and this work-around?

In the question "Efficiently create a density plot for high-density regions, points for sparse regions" there is asked to replace the low density regions with NaNs. The relevant code in the accepted answer is the following:

hh, locx, locy = scipy.histogram2d(xdat, ydat, range=xyrange, bins=bins)
posx = np.digitize(xdat, locx)
posy = np.digitize(ydat, locy)

#select points within the histogram
ind = (posx > 0) & (posx <= bins[0]) & (posy > 0) & (posy <= bins[1])
hhsub = hh[posx[ind] - 1, posy[ind] - 1] # values of the histogram where the points are
xdat1 = xdat[ind][hhsub < thresh] # low density points
ydat1 = ydat[ind][hhsub < thresh]
hh[hh < thresh] = np.nan # fill the areas with low density by NaNs

I found that something like

hh = np.where(hh > thresh, hh, np.nan)

is also working. What are the differences, both in performing as results?

Upvotes: 2

Views: 280

Answers (1)

jkalden
jkalden

Reputation: 1588

Advanced indexing (i.e. your original approach) is more efficient while the results are equal!

I compared both options with the following code:

import numpy as np
import time

t1 = time.time()
for i in xrange(1000):
        a = np.random.rand(10)
        a[a>0.5] = np.nan
t2 = time.time()
print 'adv. idx.: ',t2-t1

t1 = time.time()
for i in xrange(1000):
        a = np.random.rand(10)
        a = np.where(a>0.5,np.nan,a)
t2 = time.time()
print 'np.where: ',t2-t1

The result is rather obvious:

adv. idx.:  0.00600004196167
np.where:  0.0339999198914

np.where is significantly slower! The results where identical. However, this is not verifiable via == comparison as np.nan == np.nan yields False.

Upvotes: 1

Related Questions