Counts the number of occurrences of each value in an integer array. If `minlength` and `maxlength` are not given, returns a vector with length `tf.reduce_max(arr) + 1` if `arr` is non-empty, and length 0 otherwise. If `weights` are non-None, then index `i` of the output stores the sum of the
(arr,
weights=None,
minlength=None,
maxlength=None,
dtype=dtypes.int32,
name=None)
| 3144 | |
| 3145 | @tf_export("math.bincount", v1=[]) |
| 3146 | def bincount(arr, |
| 3147 | weights=None, |
| 3148 | minlength=None, |
| 3149 | maxlength=None, |
| 3150 | dtype=dtypes.int32, |
| 3151 | name=None): |
| 3152 | """Counts the number of occurrences of each value in an integer array. |
| 3153 | |
| 3154 | If `minlength` and `maxlength` are not given, returns a vector with length |
| 3155 | `tf.reduce_max(arr) + 1` if `arr` is non-empty, and length 0 otherwise. |
| 3156 | If `weights` are non-None, then index `i` of the output stores the sum of the |
| 3157 | value in `weights` at each index where the corresponding value in `arr` is |
| 3158 | `i`. |
| 3159 | |
| 3160 | ```python |
| 3161 | values = tf.constant([1,1,2,3,2,4,4,5]) |
| 3162 | tf.math.bincount(values) #[0 2 2 1 2 1] |
| 3163 | ``` |
| 3164 | Vector length = Maximum element in vector `values` is 5. Adding 1, which is 6 |
| 3165 | will be the vector length. |
| 3166 | |
| 3167 | Each bin value in the output indicates number of occurrences of the particular |
| 3168 | index. Here, index 1 in output has a value 2. This indicates value 1 occurs |
| 3169 | two times in `values`. |
| 3170 | |
| 3171 | ```python |
| 3172 | values = tf.constant([1,1,2,3,2,4,4,5]) |
| 3173 | weights = tf.constant([1,5,0,1,0,5,4,5]) |
| 3174 | tf.math.bincount(values, weights=weights) #[0 6 0 1 9 5] |
| 3175 | ``` |
| 3176 | Bin will be incremented by the corresponding weight instead of 1. |
| 3177 | Here, index 1 in output has a value 6. This is the summation of weights |
| 3178 | corresponding to the value in `values`. |
| 3179 | |
| 3180 | Args: |
| 3181 | arr: An int32 tensor of non-negative values. |
| 3182 | weights: If non-None, must be the same shape as arr. For each value in |
| 3183 | `arr`, the bin will be incremented by the corresponding weight instead of |
| 3184 | 1. |
| 3185 | minlength: If given, ensures the output has length at least `minlength`, |
| 3186 | padding with zeros at the end if necessary. |
| 3187 | maxlength: If given, skips values in `arr` that are equal or greater than |
| 3188 | `maxlength`, ensuring that the output has length at most `maxlength`. |
| 3189 | dtype: If `weights` is None, determines the type of the output bins. |
| 3190 | name: A name scope for the associated operations (optional). |
| 3191 | |
| 3192 | Returns: |
| 3193 | A vector with the same dtype as `weights` or the given `dtype`. The bin |
| 3194 | values. |
| 3195 | |
| 3196 | Raises: |
| 3197 | `InvalidArgumentError` if negative values are provided as an input. |
| 3198 | |
| 3199 | """ |
| 3200 | name = "bincount" if name is None else name |
| 3201 | with ops.name_scope(name): |
| 3202 | arr = ops.convert_to_tensor(arr, name="arr", dtype=dtypes.int32) |
| 3203 | array_is_nonempty = reduce_prod(array_ops.shape(arr)) > 0 |
no test coverage detected