Friday, January 16, 2026
HomeLanguagesPython – tensorflow.math.bincount()

Python – tensorflow.math.bincount()

TensorFlow is open-source Python library designed by Google to develop Machine Learning models and deep learning  neural networks. bincount() is present in TensorFlow’s math module. It is used to count occurrences of each number in integer array.

Syntax: tensorflow.math.bincount( arr, weights, minlength, maxlength, dtype, name)

Parameters:

  • arr: It’s tensor of dtype int32 with non-negative values.
  • weights(optional): It’s a tensor of same shape as arr. Count of each value in arr is incremented by it’s corresponding weight.
  • minlength(optional): It defines the minimum length of returned output.
  • maxlength(optional): It defines the maximum length of returned output. Bin of the values in arr that are greater than or equal to maxlength is not calculated.
  • dtype(optional): It determines the dtype of returned output if weight is none.
  • name(optional): It’s an optional argument that defines the name for the operation.
     

Returns:
It returns a vector with the same dtype as weights or the given dtype. Index of the vector defines the value and it’s value defines the bin of index in arr.
 

Example 1:

Python3




# importing the library
import tensorflow as tf
 
# initializing the input
a = tf.constant([1,2,3,4,5,1,7,3,1,1,5], dtype = tf.int32)
 
# printing the input
print('a: ',a)
 
# evaluating bin
r = tf.math.bincount(a)
 
# printing result
print("Result: ",r)


Output:

a:  tf.Tensor([1 2 3 4 5 1 7 3 1 1 5], shape=(11,), dtype=int32)
Result:  tf.Tensor([0 4 1 2 1 2 0 1], shape=(8,), dtype=int32)

# bin of 0 in input is 0, bin of 1 in input is 4 and so on

Example 2: This example provides weights, so instead of 1 values are incremented by the corresponding weight.

Python3




# importing the library
import tensorflow as tf
 
# initializing the input
a = tf.constant([1,2,3,4,5,1,7,3,1,1,5], dtype = tf.int32)
weight = tf.constant([0,2,1,0,2,1,3,3,1,0,5], dtype = tf.int32)
 
# printing the input
print('a: ',a)
print('weight: ',weight)
 
# evaluating bin
r = tf.math.bincount(arr = a,weights = weight)
 
# printing result
print("Result: ",r)


Output:

a:  tf.Tensor([1 2 3 4 5 1 7 3 1 1 5], shape=(11,), dtype=int32)
weight:  tf.Tensor([0 2 1 0 2 1 3 3 1 0 5], shape=(11,), dtype=int32)
Result:  tf.Tensor([0 2 2 4 0 7 0 3], shape=(8,), dtype=int32)
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32474 POSTS0 COMMENTS
Milvus
117 POSTS0 COMMENTS
Nango Kala
6846 POSTS0 COMMENTS
Nicole Veronica
11977 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12062 POSTS0 COMMENTS
Shaida Kate Naidoo
6984 POSTS0 COMMENTS
Ted Musemwa
7219 POSTS0 COMMENTS
Thapelo Manthata
6933 POSTS0 COMMENTS
Umr Jansen
6911 POSTS0 COMMENTS