Numpy bincount()

numpy.bincount(x, weights=None, minlength=0)
Return int ndarray .
xInput array of non negative of 1 D
weight(optional ), array of same size of input array. Weight of the elements to be used
minlengthInt (optional ), minimum length of bins

Size of the return array

The length of output array is maximum value of input array + 1.If highest value of input array is 5 then there will be 6 elements in output array.

The output array will show number of occurrence of elements starting from 0 to highest value of the input array.

Examples 1

bincount of ID array
import numpy as np 
ma=np.array([0,2,2,6,5]) 
print(np.bincount(ma))
Output
[1 0 2 0 0 1 1]
If the input array has the highest value of 6, then our output array will have 7 elements. Each element of our output array will show frequency of occurrence of numbers starting from 0 to highest number.

Example 2

Using weight
import numpy as np 
my_array1 = np.array([0,2,2,6,5]) 
my_array2 = np.array([2,2,1,3,4])
my_bin=np.bincount(my_array2,weights=my_array1)
print(my_bin)
Output
[0. 2. 2. 6. 5.]

Example 3

import numpy as np 
my_array1 = np.array([0,2,2,6,5]) 
my_array2 = np.array([2,2,1,3,4])
my_bin=np.bincount(my_array1,my_array2)
print(my_bin)
Output
[2. 0. 3. 0. 0. 4. 3.]

Example 4

import numpy as np 
my_array1 = np.array([0,3,3,5]) 
my_array2 = np.array([2,1,4,6])
my_bin=np.bincount(my_array1,my_array2)
print(my_bin)
Output
[2. 0. 0. 5. 0. 6.]
Numpy ones()
Subhendu Mohapatra — author at plus2net
Subhendu Mohapatra

Author

🎥 Join me live on YouTube

Passionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.



Subscribe to our YouTube Channel here



plus2net.com







Python Video Tutorials
Python SQLite Video Tutorials
Python MySQL Video Tutorials
Python Tkinter Video Tutorials
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2025   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer