About :
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.
Parameters :
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
Return :
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. Here size of the output array would be max(input_arr)+1.
Code 1 : Working of bincount() in NumPy
# Python Program explaining # working of numpy.bincount() method import numpy as geek # 1D array with +ve integers array1 = [1, 6, 1, 1, 1, 2, 2] bin = geek.bincount(array1) print("Bincount output : \n ", bin) print("size of bin : ", len(bin), "\n") array2 = [1, 5, 5, 5, 4, 5, 5, 2, 2, 2] bin = geek.bincount(array2) print("Bincount output : \n ", bin) print("size of bin : ", len(bin), "\n") # using min_length attribute length = 10bin1 = geek.bincount(array2, None, length) print("Bincount output : \n ", bin1) print("size of bin : ", len(bin1), "\n") |
Output :
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
# Python Program explaining # working of numpy.bincount() method import numpy as geek # 1D array with +ve integers array2 = [10, 11, 4, 6, 2, 1, 9] array1 = [1, 3, 1, 3, 1, 2, 2] # array2 : weight bin = geek.bincount(array1, array2) print("Summation element-wise : \n", bin) #index 0 : 0 #index 1 : 10 + 4 + 2 = 16 #index 2 : 1 + 9 = 10 #index 3 : 11 + 6 = 17 |
Output :
Summation element-wise : [ 0. 16. 10. 17.]
References :
https://docs.scipy.org/doc/numpy/reference/generated/numpy.bincount.html#numpy.bincount
.
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 contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Recommended Posts:
- Reusable piece of python functionality for wrapping arbitrary blocks of code : Python Context Managers
- Python - Read blob object in python using wand library
- Python | Merge Python key values to list
- Important differences between Python 2.x and Python 3.x with examples
- Reading Python File-Like Objects from C | Python
- Python | Convert list to Python array
- Python | Index of Non-Zero elements in Python list
- Python | PRAW - Python Reddit API Wrapper
- MySQL-Connector-Python module in Python
- twitter-text-python (ttp) module - Python
- 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 | Visualizing O(n) using Python
- JavaScript vs Python : Can Python Overtop JavaScript by 2020?
- SQL using Python | Set 1
- abs() in Python
- try-except vs If in Python
- chr() in Python
Improved By : CodeWithPriyansh

