bool_array * 1
and bool_array.astype(int)
appear to do the same thing. Which is more efficient?
bool_array.astype(int)
is faster ( Colab notebook ):
import numpy as np
bool_array = np.random.choice([0, 1], size=1_000_000).astype(bool)
%timeit int_array = bool_array * 1
1000 loops, best of 5: 1.5 ms per loop
%timeit int_array = bool_array.astype("int")
1000 loops, best of 5: 973 µs per loop
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.