 # bit scan forward and reverse in numpy

• Last Update :
• Techknowledgy :

For numbers in `[0,2**63)`, we can use some arithmetic operations to get the leading and trailing zeros in their binary formats and hence skip the string manipulations -

```def get_leading_trailing_zeros(n):
a = (2 ** np.arange(64) & n)
lead_zeros = 64 - a.argmax() - 1
if n == 0:
trail_zeros = 1
else:
trail_zeros = (a == 0).argmin()

I am not sure about the speed of the following code. But you can certainly do it like this, without using strings.

```n = np.uint64(100)
i = 1
while ((n >> np.uint64(i)) % 2 == 0):
i += 1

trail_zeros = i```
`lead_zeros = int(64 - np.ceil(np.log2(n)))`

Suggestion : 2

I need to count the number of trailing and anycodings_python leading zeros in a numpy uint64 variable, so anycodings_python right now I'm doing it like this:,You right shift the value n until you anycodings_python get an odd number. Number of right anycodings_python shifts done is equal to trail_zeros.,For numbers in [0,2**63), we can use anycodings_python some arithmetic operations to get the anycodings_python leading and trailing zeros in their anycodings_python binary formats and hence skip the string anycodings_python manipulations -,Is there a better way of doing this, without anycodings_python using strings? The priority is speed. Thank anycodings_python you!

I need to count the number of trailing and anycodings_python leading zeros in a numpy uint64 variable, so anycodings_python right now I'm doing it like this:

```# n > 0
n = np.uint64(100)
s = np.binary_repr(n)
trail_zeros = len(s) - len(s.rstrip('0'))

For numbers in [0,2**63), we can use anycodings_python some arithmetic operations to get the anycodings_python leading and trailing zeros in their anycodings_python binary formats and hence skip the string anycodings_python manipulations -

```def get_leading_trailing_zeros(n):
a = (2 ** np.arange(64) & n)
lead_zeros = 64 - a.argmax() - 1
if n == 0:
trail_zeros = 1
else:
trail_zeros = (a == 0).argmin()

I am not sure about the speed of the anycodings_python following code. But you can certainly do anycodings_python it like this, without using strings.

```n = np.uint64(100)
i = 1
while ((n >> np.uint64(i)) % 2 == 0):
i += 1

trail_zeros = i```
`lead_zeros = int(64 - np.ceil(np.log2(n)))`

Suggestion : 3

Note that as the order of the arguments are reversed, the side must be too. The searchsorted call is marginally faster, as it does not do any monotonicity checks. Perhaps more importantly, it supports all dtypes.,np.digitize is implemented in terms of np.searchsorted. This means that a binary search is used to bin the values, which scales much better for larger number of bins than the previous linear search. It also removes the requirement for the input array to be 1-dimensional.,If values in x are such that they fall outside the bin range, attempting to index bins with the indices that digitize returns will result in an IndexError.,Indicating whether the intervals include the right or the left bin edge. Default behavior is (right==False) indicating that the interval does not include the right edge. The left bin end is open in this case, i.e., bins[i-1] <= x < bins[i] is the default behavior for monotonically increasing bins.

```np.digitize(x, bins, right = True)
np.searchsorted(bins, x, side = 'left')```
```>>> x = np.array([0.2, 6.4, 3.0, 1.6]) >>>
bins = np.array([0.0, 1.0, 2.5, 4.0, 10.0]) >>>
inds = np.digitize(x, bins) >>>
inds
array([1, 4, 3, 2]) >>>
for n in range(x.size):
...print(bins[inds[n] - 1], "<=", x[n], "<", bins[inds[n]])
...
0.0 <= 0.2 < 1.0
4.0 <= 6.4 < 10.0
2.5 <= 3.0 < 4.0
1.0 <= 1.6 < 2.5```
```>>> x = np.array([1.2, 10.0, 12.4, 15.5, 20.]) >>>
bins = np.array([0, 5, 10, 15, 20]) >>>
np.digitize(x, bins, right = True)
array([1, 2, 3, 4, 4]) >>>
np.digitize(x, bins, right = False)
array([1, 3, 3, 4, 5])```

Suggestion : 4

For most data analysis applications, the main areas of functionality I’ll focus on are:,Fast vectorized array operations for data munging and cleaning, subsetting and filtering, transformation, and any other kinds of computations,Standard mathematical functions for fast operations on entire arrays of data without having to write loops,See Table 4-1 for a short list of standard array creation functions. Since NumPy is focused on numerical computing, the data type, if not specified, will in many cases be float64 (floating point).

```In: data1 = [6, 7.5, 8, 0, 1]

In: arr1 = np.array(data1)

In: arr1
Out: array([6., 7.5, 8., 0., 1.])```
```In: arr1 = np.array([1, 2, 3], dtype = np.float64)

In: arr2 = np.array([1, 2, 3], dtype = np.int32)

In: arr1.dtype In: arr2.dtype
Out: dtype('float64') Out: dtype('int32')```
```In: arr = np.array([
[1., 2., 3.],
[4., 5., 6.]
])

In: arr
Out:
array([
[1., 2., 3.],
[4., 5., 6.]
])

In: arr * arr In: arr - arr
Out: Out:
array([
[1., 4., 9.], array([
[0., 0., 0.],
[16., 25., 36.]
])[0., 0., 0.]
])```
```In: arr = np.arange(10)

In: arr
Out: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

In: arr
Out: 5

In: arr[5: 8]
Out: array([5, 6, 7])

In: arr[5: 8] = 12

In: arr
Out: array([0, 1, 2, 3, 4, 12, 12, 12, 8, 9])```
```In: arr[1: 6]
Out: array([1, 2, 3, 4, 64])```
```In: names = np.array(['Bob', 'Joe', 'Will', 'Bob', 'Will', 'Joe', 'Joe'])

In: data = np.random.randn(7, 4)

In: names
Out:
array(['Bob', 'Joe', 'Will', 'Bob', 'Will', 'Joe', 'Joe'],
dtype = '|S4')

In: data
Out:
array([
[-0.048, 0.5433, -0.2349, 1.2792],
[-0.268, 0.5465, 0.0939, -2.0445],
[-0.047, -2.026, 0.7719, 0.3103],
[2.1452, 0.8799, -0.0523, 0.0672],
[-1.0023, -0.1698, 1.1503, 1.7289],
[0.1913, 0.4544, 0.4519, 0.5535],
[0.5994, 0.8174, -0.9297, -1.2564]
])```

Suggestion : 5

Images are arrays: use the whole numpy machinery.,Creating a numpy array from an image file:,dtype is uint8 for 8-bit images (0-255),More denoising filters are available in skimage.denoising, see the Scikit-image: image processing tutorial.

```>>> from scipy
import ndimage```
```from scipy
import misc
import imageio
f = misc.face()
imageio.imsave('face.png', f) # uses the Image module(PIL)

import matplotlib.pyplot as plt
plt.imshow(f)
plt.show()```
```>>> from scipy import misc
>>> import imageio
>>> face = misc.face()
>>> imageio.imsave('face.png', face) # First we need to create the PNG file

>>> type(face)
<class 'imageio.core.util.Array'>
>>> face.shape, face.dtype
((768, 1024, 3), dtype('uint8'))```
```>>> face.tofile('face.raw') # Create raw file >>>
face_from_raw = np.fromfile('face.raw', dtype = np.uint8) >>>
face_from_raw.shape(2359296, ) >>>
face_from_raw.shape = (768, 1024, 3)```
`>>> face_memmap = np.memmap('face.raw', dtype = np.uint8, shape = (768, 1024, 3))`
```>>>
for i in range(10):
...im = np.random.randint(0, 256, 10000).reshape((100, 100))
...imageio.imsave('random_%02d.png' % i, im) >>>
from glob
import glob
>>>
filelist = glob('random*.png') >>>
filelist.sort()```