Article Complete
AIM
All you need to know about time-series clustering
All you need to know about time-series clustering

All you need to know about time-series clustering

In this article, we are going to discuss time series clustering with its key concepts and we will also understand how it can be utilized practically.

Published Updated

Key takeaways

  • Time series data is increasingly prevalent across various industries, necessitating effective analysis methods.
  • Time series clustering is an unsupervised learning approach that incorporates time values into its grouping process.
  • The complexity of vast and fluctuating time series data challenges traditional exploratory data analysis techniques.
  • Key methods of clustering time series data include agglomerative clustering, K-means, and K-Medoids.
5 min

Time series data is one of the major types of data that is being generated in a huge way nowadays. From financial analysis to industrial machinery analysis has the existence of time series data. Generally, we think of time series data as supervised data but when its generation becomes vast and fluctuates it can behave like unsupervised data. Time-series clustering is a process that can help us in dealing with such fluctuating and vast time series data. In this article, we are going to discuss time series clustering with its key concepts and we will also understand how it can be utilized practically. The major points to be discussed in the article are listed below.

Table of content 

  1. What is time series clustering?
  2. Time-series as matrix
  3. Implementation 
    1. Data extraction 
    2. Data preprocessing
    3. Clustering 
      1. Agglomerative clustering
      2. K-means clustering
      3. K-Medoids clustering

What is time series clustering?

We can consider the time series clustering is an unsupervised learning problem similar to the clustering in other domains but one thing that makes this clustering different from other clustering is that this includes time values in clusters. 

In the present scenario where the generation of data, mainly the time series data, is at a very high rate and even the generation of data is also a time series, in such a situation, it becomes very difficult to understand the patterns in the data by just performing EDA on it. Let's take a look at the below graph:

Image source

This graph consists of the visualization of a noisy time series on the upside with blue color.  By just looking at the upper trend line it is very difficult to make any inference from the data. To make it easy the green line is the filtered signal of the upper trend. Looking at the signal it is not easy to interpret where the series is temporal and where it is spatial. 

With such a time series, we are required to segregate it into parts so that we can understand the scenario much better. It can be considered as the process of organizing data points into groups based on their similarities. We mainly perform time series clustering to minimize the data similarity across the clusters and increase the similarity within the cluster. There are various ways to cluster the time series such as:

  • Agglomerative clustering: This type of clustering includes the distance matrix to cluster the time series data set.  
  • Time series K-Means: It is a very basic way that can include euclidean, dynamic time warping, or soft dynamic time warping. 
  • Kernel K-Means: This method is similar to the time series k-means but in it for clustering data, we rely on the kernel trick that mainly introduces a positive semidefinite.
  • K-means DBA clustering: This type of clustering includes averaging strategy DTW barycenter averaging for clustering the time series data. 
  • K-Medoid clustering: This type of clustering includes a distance matrix to perform k-medoid clustering. 

In this section we got to know about what is this clustering, by the above points we can say that there is always a requirement to cluster a time-series data before modelling it. In this article, we are going to see how we can perform some of these above-given methods of clustering. One thing that is noticeable in this position is we need to think of time series as matrices not as series to perform clustering.

Are you looking for a complete repository of Python libraries used in data science, check out here.

Time-series as matrix

In the above points, we have seen that this type of clustering is required but this clustering should not be interpreted as segregating time series by time values or decomposing a time series into trend, seasonality, and noise. However, these things also matter at the time of time series modeling but there is no relation between them with this type of clustering. In this process, we try to segregate time-dependent variables into clusters so that we can increase the similarity between time-dependent values within the cluster.

As mentioned above this process can be considered a solution to unsupervised learning problems that do not consider time values but consider the time-dependent variables. Just like other clustering methods, we may think of this series as a matrix without taking time values in the process. The implementation we are going to do will convert our time series into the matrix and this concept will get clearer. Let’s take a look at how we can do that.

Clustering the Time-series

Data extraction 

In most data science-related works, we are required to have data. In this article, we are going to use the share market price data of the State bank of India. We can use this data using the following lines of codes.

import datetime as dt
from datetime import datetime as dt
from dateutil.relativedelta import relativedelta
import yfinance as yf  
end = dt.today()
start = dt.today() - relativedelta(months=1)
data = yf.download('SBIN.NS', start, end)
data.tail()

Output:

Here we can see the last values of our extracted data. For this experiment, I have extracted data from last month that needs to be clustered. In the data, we can see that there are 6 time-dependent values: 5 are for the price of a share and one is representing the volume of a share. Now start our preprocessing of the data.

Data preprocessing

As we discussed above, time series clustering is a process that makes us think of series as a matrix because it clusters the time series based on the time-dependent values, not by time. Before converting the series into the matrix, let's visualize the data.

data.plot()
data.Open.plot(secondary_y=True, label="open")
data.High.plot(secondary_y=True, label="high")
data.Low.plot(secondary_y=True, label="low")
data.Close.plot(secondary_y=True, label="close")
data['Adj Close'].plot(secondary_y=True, label="adj close")
plt.show()

Output:

Here we can see how the time series is moving but these values are pretty difficult to explain. Let's start its conversion to perform clustering of time series.

Converting a series into the matrix is very easy we just need to use the .to_numpy module of pandas in the following way:

series = data.to_numpy()
series

Output 

The above output represents the required form of data. Now let's plot this form of data.

plt.plot(series)

Output:

Here we can see the sum-up form of our converted data. Let's start the clustering.

Time-series clustering

In the first section of the article, we have seen that different types of clustering can be performed in the time series, and in this section, we will look at how we can perform them. For performing clustering we are going to use a package named as DTAIdistance, this package provides various functions to perform time-series processing related to a distance matrix and clustering analysis. Let's take a look at how we can perform agglomerative clustering.    

Agglomerative clustering 

We can perform hierarchically and linkage clustering using this method. Let's see how we can do that.

Hierarchical clustering 
from dtaidistance import clustering
model1 = clustering.Hierarchical(dtw.distance_matrix_fast, {})
cluster_idx = model1.fit(series)
model2 = clustering.HierarchicalTree(model1)
cluster_idx = model2.fit(series)

Let's plot this clustering.

import matplotlib.pyplot as plt
fig, ax = plt.subplots(nrows=1, ncols=2, figsize=(10, 10))
show_ts_label = lambda idx: "ts-" + str(idx)
model2.plot(axes=ax, show_ts_label=show_ts_label,
           show_tr_label=True, ts_label_margin=-10,
           ts_left_margin=10, ts_sample_length=1)

Output:

Here we can cluster time series using the distance between matrices. 

Linkage clustering 
model3 = clustering.LinkageTree(dtw.distance_matrix_fast, {})
cluster_idx = model3.fit(series)

Let's plot the clusters.

import matplotlib.pyplot as plt
fig, ax = plt.subplots(nrows=1, ncols=2, figsize=(10, 10))
show_ts_label = lambda idx: "ts-" + str(idx)
model3.plot(axes=ax, show_ts_label=show_ts_label,
           show_tr_label=True, ts_label_margin=-10,
           ts_left_margin=10, ts_sample_length=1)

Output:

K-means clustering 

from dtaidistance.clustering import kmeans
model4 = kmeans.KMeans(k=10)
cluster_idx, performed_it = model4.fit(series)

Here we have used 10 centers to make clusters using k-means clustering. Let's plot the clusters.

import matplotlib.pyplot as plt
fig, ax = plt.subplots(nrows=1, ncols=2, figsize=(10, 10))
show_ts_label = lambda idx: "ts-" + str(idx)
model4.plot(axes=ax, show_ts_label=show_ts_label,
           show_tr_label=True, ts_label_margin=-10,
           ts_left_margin=10, ts_sample_length=1)

Output:

K-Medoids clustering 

To perform this type of clustering between time series we may need to define the distance matrix method. The package we are using will require downloading a pyclustering package to perform the distance matrix method. We can perform this clustering using the following lines of codes.

from dtaidistance import dtw, clustering
 
model5 = clustering.KMedoids(dtw.distance_matrix_fast, {}, k=10)
cluster_idx = model5.fit(series)

Let's plot the clusters.

model5.plot()

Output:

Here we can see the clusters made using the k-medoids clustering algorithm.

Final words 

In this article, we have discussed the time series clustering that is making clusters of time series values without considering time factors in it. Along with this, we have looked at how we can do different types of clustering In time series using the python language.  

References

Membership

Stay ahead with AI insights.

Unlimited access to premium articles, exclusive analysis, and the stories shaping India's AI economy.

Subscribe now

AIM Americas

1190 Miraloma Way Suite P, Sunnyvale, CA 94085

AIM India

1st Floor, Sakti Statesman, Bellandur, Bengaluru – 560103

© 2026 AIM. All rights reserved.