TensorFlow
group-by operation
machine learning
data processing
tutorial

How to do the group-by operation in Tensorflow?

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

TensorFlow does not have a single pandas-style groupby API in core, but it does have efficient segment operations that solve the most common group-and-aggregate problems. The usual pattern is to map each row to an integer group ID, then use tf.math.segment_* or tf.math.unsorted_segment_*. This article shows the standard approach and explains when TensorFlow is a good fit for this task.

Group-By in TensorFlow Usually Means Segment Reduction

Suppose you have values and a group ID for each value. Then operations such as sum, mean, max, and min can be expressed as segment reductions.

Example data:

  • values: [10, 20, 30, 40, 50]
  • groups: [0, 0, 1, 1, 1]

That means group 0 contains 10 and 20, while group 1 contains 30, 40, and 50.

Use unsorted_segment_sum for the Basic Case

If your group IDs are integer labels and may not already be sorted, tf.math.unsorted_segment_sum is a common choice.

python
1import tensorflow as tf
2
3values = tf.constant([10.0, 20.0, 30.0, 40.0, 50.0])
4group_ids = tf.constant([0, 0, 1, 1, 1])
5num_groups = 2
6
7result = tf.math.unsorted_segment_sum(values, group_ids, num_groups)
8print(result.numpy())

This returns one aggregated value per group index.

Mean and Max Work the Same Way

TensorFlow provides several related segment reductions.

python
1import tensorflow as tf
2
3values = tf.constant([10.0, 20.0, 30.0, 40.0, 50.0])
4group_ids = tf.constant([0, 0, 1, 1, 1])
5num_groups = 2
6
7means = tf.math.unsorted_segment_mean(values, group_ids, num_groups)
8maxes = tf.math.unsorted_segment_max(values, group_ids, num_groups)
9
10print(means.numpy())
11print(maxes.numpy())

If your real need is aggregation, this is usually the TensorFlow-native answer.

Convert Arbitrary Keys to Integer Group IDs

Sometimes your keys are strings or other arbitrary values rather than ready-made integer IDs. One way to handle that is to use tf.unique first.

python
1import tensorflow as tf
2
3keys = tf.constant(["A", "A", "B", "B", "B"])
4values = tf.constant([10.0, 20.0, 30.0, 40.0, 50.0])
5
6unique_keys, group_ids = tf.unique(keys)
7result = tf.math.unsorted_segment_sum(values, group_ids, tf.shape(unique_keys)[0])
8
9print(unique_keys.numpy())
10print(result.numpy())

Now you have a grouped sum for each unique key in the order TensorFlow encountered it.

Multi-Column Aggregation

If each row contains several numeric features, segment reductions still work as long as the first dimension aligns with the group IDs.

python
1import tensorflow as tf
2
3values = tf.constant([
4    [1.0, 10.0],
5    [2.0, 20.0],
6    [3.0, 30.0],
7    [4.0, 40.0],
8])
9group_ids = tf.constant([0, 0, 1, 1])
10
11result = tf.math.unsorted_segment_sum(values, group_ids, 2)
12print(result.numpy())

This gives one aggregated row per group.

segment_sum vs unsorted_segment_sum

There are two related families:

  • 'segment_* expects sorted group IDs'
  • 'unsorted_segment_* works without sorting'

If your group IDs are already grouped in order, segment_sum and similar functions are fine. If not, unsorted_segment_* is safer and usually easier for general-purpose code.

TensorFlow Is Not Always the Best Tool

If you are doing one-off data analysis on a CPU, Pandas is often the simpler choice for group-by work. TensorFlow group-by patterns are most useful when:

  • the data is already inside a TensorFlow pipeline
  • the aggregation is part of model preprocessing or training logic
  • you want to stay on TensorFlow tensors instead of converting out to another library

That is why the question is as much about workflow as it is about syntax.

Common Pitfalls

  • Looking for a pandas-style groupby object instead of using segment reductions.
  • Forgetting that segment ops require integer group IDs.
  • Using segment_sum on unsorted group labels and getting incorrect results.
  • Converting to TensorFlow just for aggregation when a simpler data-processing library would be more appropriate.
  • Mismatching the number of rows in the values tensor and the group ID tensor.

Summary

  • In TensorFlow, group-by is usually expressed through segment reduction operations.
  • Use unsorted_segment_sum, unsorted_segment_mean, or related ops for general grouping.
  • Convert arbitrary keys to integer group IDs with tf.unique when needed.
  • Use segment_* only when your group IDs are already sorted by group.
  • TensorFlow group-by is most useful when the data is already in a TensorFlow pipeline.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.