Time series data mining in R. Bratislava, Slovakia.
Multiple Data (Time Series) Streams Clustering
Written on 2019-02-03
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 .
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,
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.
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).
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.