Multiple Data (Time Series) Streams Clustering
Nowadays, data streams occur in many real scenarios. For example, they are generated from sensors, web traffic, satellites, and other interesting use cases. We have to process them in a fast way and extract from them as much knowledge as we can. Data streams have their own specific characteristics for processing and data mining. For example, they can be very fast, we can not process the whole history of data streams in memory, so we have to do it incrementally or in (e.g. sliding) windows.
In this post, I will cover one data streams clustering method that was developed by me. In R, you can do data stream clustering by stream package, BUT! there are methods only for one stream clustering (not multiple streams). However, I want to show you clustering of multiple data streams, so from multiple sources (e.g. sensors).
I created clustering method that is adapted for time series streams - called ClipStream [1]. This method is attribute based, so multiple attributes (features) are gathered from multiple streams windows. You can read more about it from my journal paper.
I will show you use case on smart meter data - electricity consumption time series (consumers) from London ( openly available here). Motivation for clustering such a data is to:
- extract typical patterns of consumption,
- detect outliers,
- unsupervised classification of consumers,
- create more forecastable groups of time series,
- monitor changes and behavior.
The motivation to process them as streams is the fact that the data of electricity consumption (or production) are measured every 5, 15 or 30 minutes.
ClipStream has following steps (very briefly):
- computing data streams representation for every stream (dimensionality reduction) - i.e. synopsis,
- detect outlier (anomal) streams directly from extracted representations,
- cluster non-outlier representations by K-medoids,
- assign outlier streams to nearest clusters (medoids),
- aggregate time series by clusters,
- detect changes in aggregated time series.
In this post, I will mainly focus on the first 4 parts of my approach, since there are applicable to various use cases - not only on smart meter data and its forecasting. The whole method is developed in unsupervised fashion (yey!), so representation, clustering and outlier detection of time series streams are “learned” unsupervised.
Data exploration
Firstly, read the London smart meter data, which compromise more than 4000 consumers and load all the needed packages.
Let’s subset first 1000 consumers for more simple operations.
Let’s plot random consumer for better imagination.
We can see stochastic behavior, but also some consumption pattern.
We can plot, for example, its average daily or weekly profile. We will do it by TSrepr’s package function repr_seas_profile
.
The peak at the start of a day…
Now, let’s plot weekly pattern:
There is some change during a week.
Let’s explore the whole consumer base, but how when we have more than 1000 time series? We can cluster time series and just plot its daily patterns for example by created clusters. We will reduce the length of the visualized time series and also a number of time series in one plot.
First, extract average daily patterns, we will make it by repr_matrix
function from TSrepr.
Normalization of every consumer time series - row-wise by z-score is necessary!
You can use your own normalization function (z-score and min-max methods are implemented).
Let’s cluster computed mean daily profiles for example by K-means and to 12 clusters:
Preprocess clustering results with data.table
package and plot them:
Various interesting patterns of daily consumption!
BUT (here comes motivation)! When we would like to cluster just most recent data (for example 2-3 weeks), and update it regularly every day (or even thought half-hour), automatically detect changes in time series streams and detect anomalies (outlier consumers), detect automatically the number of clusters, and make it as quickly as possible… We can not do it like in the previous case, because of computational and memory load and also accuracy. Here comes on screen more sophisticated multiple data streams clustering methods. In ClipStream that uses FeaClip time series streams representation (see my previous post about time series represetnations), a representation can be computed incrementally, clusterings are computed in data batches, outliers are detected straight from representation and etc.
FeaClip is interpretable time series representation. It extracts 8 interpretable features from clipped representation (again please see the mentioned blog post). Let’s plot representation from one day consumption time series:
Let’s compute it also for consumption of length 2 weeks - by windows.
We can see the typical pattern during working days and some changes when consumption goes high - the representation adapts to these changes, but not drastically - because of the average of a window (normalization).
FeaClip animation
Now, let’s play a little bit with gganimate
and av
packages and visualize the behavior of FeaClip through time (stream).
First, prepare whole one time series as a stream.
Now, create ggplot animation object by ggplot
and gganimate
functions.
Let’s animate!
We can see how FeaClip representation adapts on different behavior of electricity consumption - when it’s low around zero, or it gets high etc. - it makes drastic noise reduction and extracts only key features from the time series.
Multiple Data Streams
We already got a picture of one consumer time series stream. Let’s again play with the whole customer base (in our playing scenario with 1000 consumers). In this part, I would like to make nice animation of outlier detection and clustering phase in one image.
First iteration
Let’s get through the first iteration (window) of streams processing and clustering of ClipStream. So, first thing to do is to subset window of time series streams and compute FeaClip representations (the most recent window of length 14 days as in the previous case).
The second phase is an outlier detection from streams. This is done by “simple” IQR-quantile method from 2 key FeaClip features - Sum_1 and number of crossings.
The source code for function detectOutliersIQR
is in the ClipStream method repo. Let’s do it.
Let’s show outlier consumers with PCA:
They are nicely located at the periphery of the feature space. Let’s find some outlier consumers and plot them:
The first scenario is a consumer with a few large moments of high consumption and lot of crossings. The second scenario is the zero consumption type of a consumer.
The clustering phase is executed by an automatic K-medoids method. The number of clusters is automatically selected by the Davies-Bouldin index from the predefined range of clusters. We will cluster only non-outlier representations, for the reduction of data and also higher stability of clustering results.
Let’s assign outliers to nearest clusters (i.e. to nearest medoids).
In the plot, average daily FeaClip representations will be plotted, because of the longer length of original FeaClip representation.
Nice distinguishable patterns.
Streaming
Now, let’s flow the data! I will make an update every new day during 50 days period and plot the results - detected outliers and clusters with also assigned outliers. The code is available on my GitHub repo. For full resolution - right click on the gif and click on the view image option.
We can see that the number of outliers and clusters dynamically changes through time (daily windows). You can build your own data streams clustering algorithm with a different streams representation - for example, see TSrepr list of methods.
Conclusion
In this post, I showed you how to process and cluster multiple seasonal time series streams with the ClipStream method. I showed you also detected outliers that were automatically found from the FeaClip representation. The results of clustering can be also used for improving forecasting accuracy of aggregated consumption. For more information, read my publications about this topic in the research section.
References
[1] Laurinec, P. & Lucká, M.: Interpretable multiple data streams clustering with clipped streams representation for the improvement of electricity consumption forecasting, Data Min Knowl Disc (2018). DOI link
Seven coffees were consumed while writing this article.
If you’ve found it valuable, please consider supporting my work and...