Calculate TensorFlow Median Value

tf.contrib.distributions.percentile - Calculate TensorFlow Median Value using the percentile distribution and the interpolation methods

tf.contrib.distributions.percentile - Calculate TensorFlow Median Value using the percentile distribution and the interpolation methods

Video Transcript


In this tutorial, I’m going to show you how to find median of a TensorFlow tensor.


Let’s go ahead and import TensorFlow as tf.

import tensorflow as tf


I’m using Python version 3.6.7 and TensorFlow 1.12.0.

tf.VERSION


Let’s start by creating a simple tensor a with an odd number of elements.

a = tf.constant([1,2,3,4,5])

We can see that the median is 3 since it’s the middle element of this list.


We can find the median using the percentile function.

median = tf.contrib.distributions.percentile(a,50.)

The function is in tf.contrib.distributions.

The first argument to the function is the tensor whose median we want to find.

The second argument is the percentile we’re looking for to find the tensor, a number between 0 and 100.

Since we’re looking for the middle number, we’ll use 50 with a decimal point so Python casts it to a float.


Next, we can run this as a session which outputs the median tensor 3.

with tf.Session() as sess: sess.run(median)


Let’s rebuild our tensor a so it has an even number of elements.

a=tf.constant([1,2,3,4,5,6])

For even length tensors, the default behaviour is to output one of the two elements rather than their average.

For this behavior, we can use one of the interpolation options.


Here, our median should be 3.5 which is the average of 3 and 4.

with tf.Session() as sess: sess.run(median)


Our higher number will be mu:

mu = tf.contrib.distributions.percentile(a,50.,interpolation='higher')


And our lower number ml:

ml = tf.contrib.distributions.percentile(a,50.,interpolation='lower')

Here, I’m using the interpolation option.


The median can simply be computed by adding these two tensors and dividing by two.

median = (mu+ml)/2


Running this gives us the expected answer of 3.5.

with tf.Session() as sess: sess.run(median)


In general, the second method will give us the correct median for a list of both even and odd lengths but it will be slower.


So these are two different methods that you can use to find the median of a tensor.

Receive the Data Science Weekly Newsletter every Thursday

Easy to unsubscribe at any time. Your e-mail address is safe.