numpy.bincount(arr, weights = None, min_len = 0) : In an array of +ve integers, it counts the occurrence of each element. Each bin value is the occurrence of its index. One can also set the bin size accordingly.
arr : [array_like, 1D]Input array, having positive numbers weights : [array_like, optional]same shape as that of arr min_len : Minimum number of bins we want in the output array
Output array with no. of occurrence of index value of bin in input - arr. Output array, by default is of the length max element of arr + 1.
Code 1 : Working of bincount() in NumPy
Bincount output : [0 4 2 0 0 0 1] size of bin : 7 Bincount output : [0 1 3 0 1 5] size of bin : 6 Bincount output : [0 1 3 0 1 5 0 0 0 0] size of bin : 10
Code 2 : We can perform addition as per element with bincount() weight
Summation element-wise : [ 0. 16. 10. 17.]
This article is contributed by Mohit Gupta_OMG 😀. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
- Reading Python File-Like Objects from C | Python
- Important differences between Python 2.x and Python 3.x with examples
- Python | Add Logging to Python Libraries
- Python | Add Logging to a Python Script
- Python | Sort Python Dictionaries by Key or Value
- Python | Set 4 (Dictionary, Keywords in Python)
- Python Set | pop()
- pow() in Python
- try and except in Python
- bin() in Python
- zip() in Python
- Any & All in Python
- max() and min() in Python
- set add() in python