# Numpy Digitize Function Implementation in Python with Steps Numpy is very popular python packages that allow you to manipulate NumPy array. There are many NumPy function that does so. Numpy digitize is one of them. Using this function you can find the indices of the bins corresponding to the input array. The syntax for it is below.

``numpy.digitize(x, bins, right=False)``

Here x is the input array. Bins are of array type and they must be monotonic and one dimensional. The right parameters allow you to include the right or left bin edge of not.

## Steps to implement Numpy Digitize function

### Step 1: Import all the necessary libraries

The first step is to import the necessary libraries. In our example, I am using the NumPy module only. So let’s import them using the import statement.

``import numpy as np``

### Step 2: Create a Sample Numpy array

In our example, we will use both the one-dimensional array. So let’s create it.

``array1d = np.array([0.2, 6.4, 3.0, 1.6])``

### Step 3: Create sample bins for the array

In this step, I will create bins for the above sample array. It will be of NumPy array type.

``sample_bins = np.array([0.0, 1.0, 2.5, 4.0, 10.0])``

### Step 4: Apply the numpy digitize method

Now let’s apply the numpy.digitize method. It will find the indices from the bin corresponding to the input array that lies in the range. If you run the below lines of code then you will get the following output.

``````import numpy as np
array1d = np.array([0.2, 6.4, 3.0, 1.6])
sample_bins = np.array([0.0, 1.0, 2.5, 4.0, 10.0])
inds = np.digitize(array1d, sample_bins)
print(inds)``````

Output

You can see in the output index tell which highest value the sample array lies in the range. Like 1 for 0.2, 10 for 6.4 , 4 for 3 and 2.5 for 6. Ther are also.

You can also use the right =True or right = False to change the index range of the input array. In our example, if we use it then the output will be the same.

## Conclusion

Numpy digitize helps you find the range of the bin for your input array. These are steps to implement digitize function. I hope it has cleared your queries on how to use NumPy digitize. If you have any queries then you can contact us is for more help.

Source:

Digitize Offical Documentation 