Apply Clustering Analysis to group the regions with similar average temperatures

Difficulty Level
Programming Languages

In this tutorial, we will show you how to set the TabPy integration on Tableau Online and how to use it. TabPy (the Tableau Python Server) is an Analytics Extension implementation that expands Tableau's capabilities by allowing users to execute Python scripts and saved functions via Tableau's table calculations. For this tutorial, you are going to apply some advanced analytics techniques such as clustering algorithms to group the regions with similar average temperatures over time together.

Deploy TabPy to your Heroku account

  1. Log in to Heroku with your account via a browser.
  2. Go to the TabPy repository on GitHub.
  3. Click the "Deploy to Heroku" button.
  4. Configure the new TabPy server by setting environment variables. Be sure to remember all the information that you are entering: App name, PASSWORD, and USERNAME.

Configure Tableau Online

All the instructions can be found here

  1. Log in to your Tableau Online Developer Site.
  2. Go to Settings > Extensions.
  3. On the Settings page, click the Extensions tab and then scroll to Analytics Extensions.
  4. Select "Enable analytics extensions for site" (uncheck by default).
  5. Click Create new connection.
  6. Select "TabPy" for connection type
  7. In Analytics Extension Connection, enter the configuration settings for your analytics service:
Setting Current Value
Connection Name


Require SSL






Sign in with username and password






  1. Click Save

Apply Affinity Propagation clustering algorithm to cluster the regions based on the similarity of average temperature

Now that we have set up our Tableau Online Site to use the TabPy Server that we deployed on Heroku, it is time for us to use it.

  1. Download the dataset here

  2. Connect to the data a. Log in to your Tableau Online Developer Site. b. Open the Connect to the Data page. you can open this page two ways: Home > New > Workbook Explore > New > Workbook c. Select the tab "Files"

  3. Create a map of all regions in France. Drag and drop the field "Regions" to Detail on the worksheet.

  4. In this new workbook, create a new Calculated Field

  5. Use the function SCRIPT_INT

    Tableau can pass code to TabPy through four different functions: SCRIPT_INT, SCRIPT_REAL, SCRIPT_STR and SCRIPT_BOOL to accommodate the different return types.

  6. We are now going to apply the Affinity Propagation clustering algorithm to cluster the regions based on similarity temperature for months of the year. You can copy/paste the script below

import numpy as np
from sklearn.cluster import AffinityPropagation

X = np.column_stack([_arg1, _arg2, _arg3, _arg4, _arg5, _arg6, _arg7, _arg8, _arg9, _arg10, _arg11, _arg12])

db = AffinityPropagation().fit(X)

return db.labels_.tolist()

  1.  Add the cluster as color on the map: Drag and drop the calculated field that you created at the previous step on colors. You may not see any change in the map as all regions are grouped together which is a surprise.

  1. Edit the way that your calculation is computed. Click on your calculated field that is on Colors, and select "Compute Using", and "Regions". SCRIPT_X is a table calculation and as the result, you need to adjust the dimensions. You can learn more about table calculation dimensions here.


Use two other clustering algorithms to compare with the climate zones of France

In this section, we are using OPTIC and K-mean clustering algorithms. The goal is to compare the results of the three clustering algorithms against the climate zones of France. You can find the climate zones of France here.

  1. Create a new calculated field.
  2. Copy/Paste this script for OPTIC clustering.
import numpy as np
from sklearn.cluster import OPTICS

X = np.column_stack([_arg1, _arg2, _arg3, _arg4, _arg5, _arg6, _arg7, _arg8, _arg9, _arg10, _arg11, _arg12])

db = OPTICS().fit(X)

return db.labels_.tolist()


  1. Create another calculated field for k-mean clustering. For k-mean clustering you can specify the number of clusters, we are going to set it to 5.
import numpy as np
from sklearn.cluster import KMeans

X = np.column_stack([_arg1, _arg2, _arg3, _arg4, _arg5, _arg6, _arg7, _arg8, _arg9, _arg10, _arg11, _arg12])

db = KMeans(n_clusters=5).fit(X)

return db.labels_.tolist()


  1.  Try the two other clustering algorithms by adding the calculation to color to see which results are closer to the actual result.

Bonus: Compare the three methods

 Take advantage of dynamic parameters in Tableau to be able to dynamically iterate over different clustering methods and compare it with the actual result in the same dashboard.

Test in your own Sandbox

Last updated: September 13, 2021