[SciPy-User] From 1-D boolean array to integer index
Oleksandr Huziy
guziy.sasha@gmail....
Tue Oct 11 16:19:35 CDT 2011
Try shifting before the loop in the Alok's suggestion.
--
Oleksandr
2011/10/11 Jacob Biesinger <jake.biesinger@gmail.com>
> Thanks for the ideas everyone. Some clarification: the 1d bool array
> represents the bits of the integer I'm interested in. I have binary
> observations in a vector of length ~10, so the integer index will be the
> particular combination of 1's and 0's in the vector.
>
> In this case, the array will not be very long (~10 elements) but the
> conversion needs to be fast (part of an inner loop).
>
> Here's some timings on your candidate suggestions:
>
> # setup
> size = 10
> b_array = sp.rand(size) < .5
>
> # eat's suggestion
> place_values = 2**sp.arange(size-1, -1, -1)
> %timeit (b_array * place_values).sum()
> 100000 loops, best of 3: 4.83 us per loop
>
> # Alok's suggestion
> bit_shift = sp.arange(size-1, -1, -1)
> %timeit sp.sum((1<<bits)[data])
> 100000 loops, best of 3: 7.58 us per loop
>
> # Oleksandr's suggestion
> string_format = size * '%d'
> %timeit int( string_format % tuple(b_array) ,2)
> 100000 loops, best of 3: 7.24 us per loop
>
> # my original idea
> %timeit int(''.join(['1' if e else '0' for e in b_array]), 2)
> 1000000 loops, best of 3: 1.49 us per loop
>
>
> I'm kinda surprised by the timings-- building a new list, doing a string
> join, then converting the string a an integer is faster than bit-shift &
> summation or multiplication & summation. And it seems that python's string
> formatting operator is pretty efficient as well! Just as fast as some of
> the other ops.
>
> Thanks again for the suggestions-- Guess I'll stick with my current
> (fastest) implementation.
> --
> Jake Biesinger
> Graduate Student
> Xie Lab, UC Irvine
>
>
>
> On Tue, Oct 11, 2011 at 12:25 PM, Alan G Isaac <alan.isaac@gmail.com>wrote:
>
>> On 10/11/2011 1:29 PM, Jacob Biesinger wrote:
>> > I have a 1-D bool array which I'd like to convert to an integer index.
>>
>> Sounds like you may be able to just use your bool array:
>>
>> >>> idx = np.array([True, False, True])
>> >>> a = np.random.random(3)
>> >>> a
>> array([ 0.04818879, 0.49417941, 0.70470834])
>> >>> a[idx]
>> array([ 0.04818879, 0.70470834])
>>
>> hth,
>> Alan Isaac
>>
>> _______________________________________________
>> SciPy-User mailing list
>> SciPy-User@scipy.org
>> http://mail.scipy.org/mailman/listinfo/scipy-user
>>
>
>
> _______________________________________________
> SciPy-User mailing list
> SciPy-User@scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/scipy-user/attachments/20111011/dbc0ee78/attachment.html
More information about the SciPy-User
mailing list