Change language

numpy.bincount () in Python

Around:
numpy.bincount (arr, weights = None, min_len = 0): in the + ve array of integers, it counts the occurrence of each element. Each bin value is an entry for its index. You can also set the size of the basket 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. 

Code 1: Working bincount () in NumPy

# Python program explaining
# working method numpy.bincount ()

 

import numpy as geek

 
# 1D array with + ve integers

array1 = [ 1 , 6 , 1 , 1 , 1 , < code class = "value"> 2 , 2 ]

bin = geek.bincount (array1)

print ( "Bincount output :" , bin )

print ( "size of bin:" , len ( bin ), " " )

  

array2 = [ 1 , 5 , 5 , 5 , 4 , 5 , 5 , 2 , 2 , 2 ]

bin = geek.bincount (array2)

print ( " Bincount output : " , bin )

print ( "size of bin:" , len ( bin ), "" )

 
# using min_length attribute

length = 10

bin1 = geek.bincount (array2, None , length)

print ( "Bincount output :" , bin1)

 

print ( "size of bin:" , len (bin1), "" )

Exit :

 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 add according to the element using bincount ()

# Python program explaining
# numpy.bincount () working 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:" , 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.] 

Links:
https://docs.scipy.org/doc/numpy/reference/generated/numpy.bincount.html#numpy.bincount
,
This article is provided by Mohit Gupta_OMG

Shop

Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best laptop for development

$499+
Gifts for programmers

Best laptop for Cricut Maker

$299+
Gifts for programmers

Best laptop for hacking

$890
Gifts for programmers

Best laptop for Machine Learning

$699+
Gifts for programmers

Raspberry Pi robot kit

$150

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically