Count the number of occurrences of each value in a tensor of integers.
View aliases
Main aliases
tf.keras.ops.bincount(
x, weights=None, minlength=0, sparse=False
)
Returns | |
---|---|
1D tensor where each element gives the number of occurrence(s) of its
index value in x. Its length is the maximum between max(x) + 1 and
minlength.
|
Examples:
x = keras.ops.array([1, 2, 2, 3], dtype="uint8")
keras.ops.bincount(x)
array([0, 1, 2, 1], dtype=int32)
weights = x / 2
weights
array([0.5, 1., 1., 1.5], dtype=float64)
keras.ops.bincount(x, weights=weights)
array([0., 0.5, 2., 1.5], dtype=float64)
minlength = (keras.ops.max(x).numpy() + 1) + 2 # 6
keras.ops.bincount(x, minlength=minlength)
array([0, 1, 2, 1, 0, 0], dtype=int32)