Unsupervised Anomaly Detection for Time Series

What is an anomaly and why should I be concerned?

“Anomalies” or “outliers” are those data points in a sample space, which are abnormal, or out of trend. Now, the question is, “How do you define something being abnormal or outlier?”

Answer: Mathematically, data point(s) which are not in the same trend as data points in its neighbourhood.

As a business associate or technologists, detecting anomalous pattern from a huge set of data in your day-to-day task. And here we are going to discuss a method which can detect all (almost) anomalies within the data, in near real-time format.

In this article, we will try to learn about detecting anomalies from data without training the model before-hand, because you can’t train a model on data, which we don’t know about!

That’s where the idea of unsupervised learning comes into the picture.

The reason to select time series data is, they are one of the most occurring real world data, we analyze as a data scientist.

Coming to the model — “DeepAnT” is an Unsupervised time based anomaly detection model, which consists of Convolutional neural network layers.

It works really well in detecting all sorts of anomalies in the time series data. But this might have a caveat of also detecting noise, which can be handled by tuning of hyper-parameters like kernel size, ‘lookback’ (time series window size), units in hidden layers and many more.

The link for the code and data is provided in the Github link here —


Deep Learning based technique for Unsupervised Anomaly Detection using DeepAnT and LSTM Autoencoder Data Description …


Number of features in data = 27 (Including ‘timestamp’ feature)

Data type of features = Numerical

Now that we know about the data, let’s get into the code base and work upon the problem we have.

Problem Description :- We have around 80 years of climate data for Canada (frequency of data = daily), and we want to identify the anomalies from the climate data.

import numpy as np
import pandas as pd
import torch
from sklearn.preprocessing import MinMaxScaler
import time
import datetime
import matplotlib.pyplot as plt
import seaborn as sns
import os

data_file = ""
MODEL_SELECTED = "deepant" # Possible Values ['deepant', 'lstmae']
for dirname, _, filenames in os.walk('/kaggle/input'):
    for filename in filenames:
        data_file = os.path.join(dirname, filename)

The modules are imported and file is loaded into Kaggle kernel’s environment.

def read_modulate_data(data_file):
        Data ingestion : Function to read and formulate the data
    data = pd.read_csv(data_file)
    data.fillna(data.mean(), inplace=True)
    df = data.copy()
    data.set_index("LOCAL_DATE", inplace=True)
    data.index = pd.to_datetime(data.index)
    return data, df

#pytorch #anomaly-detection #deep-learning #timeseries #outliers #deep learning

What is GEEK

Buddha Community

 Unsupervised Anomaly Detection for Time Series
Lenora  Hauck

Lenora Hauck


Unsupervised Anomaly Detection on Time Series

To understand the normal behaviour of any flow on time axis and detect anomaly situations is one of the prominent fields in data driven studies. These studies are mostly conducted in unsupervised manner, since labelling the data in real life projects is a very tough process in terms of requiring a deep retrospective analyses if you already don’t have label information. Keep in mind that outlier detection and anomaly detection are used interchangeably most of the time.

There is not a magical silver bullet that performs well in all anomaly detection use cases. In this writing, I touch on fundamental methodologies which are mainly utilized while detecting anomalies on time series in an unsupervised way, and mention about simple working principles of them. In this sense, this writing can be thought as an overview about anomaly detection on time series including real life experiences.

Image for post

Photo by Jack Nagz on Unsplash

Probability Based Approaches

Using Z-score is one of the most straightforward methodology. Z-score basically stands for the number of standart deviation that sample value is below or above the mean of the distribution. It assumes that each features fits a normal distribution, and calculating the z-score of each features of a sample give an insight in order to detect anomalies. Samples which have much features whose values are located far from the means are likely to be an anomaly.

While estimating the z-scores, you should take into account the several factors that affect the pattern to get more robust inferences. Let me give you an example, you aim detecting anomalies in traffic values on devices in telco domain. Hour information, weekday information, device information(if multiple device exist in dataset) are likely to shape the pattern of traffic values. For this reason, z-score should be estimated by considering each device, hour and weekday for this example. For instance, if you expect 2.5 mbps average traffic on device A at 8 p.m. at weekends, you should take into consideration that value while making a decision for corresponding device and time information.

#outlier-detection #time-series-analysis #time-series-forecasting #python #anomaly-detection

Real-Time Time Series Anomaly Detection

As much as it has become easier over the years to collect vast amounts of data across different sources, companies need to ensure that the data they’re gathering can bring value. To aid insight collection from the data, machine learning and analytics have become trending tools. Since these domains require real-time insights, an abundance of unwelcome data can create real issues.

Before decisions are made, and critically, before actions are taken, we must ask: are there anomalies in our data that could skew the results of the algorithmic analysis? If anomalies do exist, it is critical that we automatically detect and mitigate their influence. This ensures that we get the most accurate results possible before taking action.

In this post, we explore different anomaly detection approaches that can scale on a big data source in real-time. The tsmoothie package can help us to carry out this task. Tsmoothie is a python library for time series smoothing and outlier detection that can handle multiple series in a vectorized way. It’s useful because it can provide the techniques we needed to monitor sensors over time.

#time-series-analysis #editors-pick #anomaly-detection #data-science #machine-learning

Michael  Hamill

Michael Hamill


These Tips Will Help You Step Up Anomaly Detection Using ML

In this article, you will learn a couple of Machine Learning-Based Approaches for Anomaly Detection and then show how to apply one of these approaches to solve a specific use case for anomaly detection (Credit Fraud detection) in part two.

A common need when you analyzing real-world data-sets is determining which data point stand out as being different from all other data points. Such data points are known as anomalies, and the goal of anomaly detection (also known as outlier detection) is to determine all such data points in a data-driven fashion. Anomalies can be caused by errors in the data but sometimes are indicative of a new, previously unknown, underlying process.

#machine-learning #machine-learning-algorithms #anomaly-detection #detecting-data-anomalies #data-anomalies #machine-learning-use-cases #artificial-intelligence #fraud-detection

Time Series Basics with Pandas

In my last post, I mentioned multiple selecting and filtering  in Pandas library. I will talk about time series basics with Pandas in this post. Time series data in different fields such as finance and economy is an important data structure. The measured or observed values over time are in a time series structure. Pandas is very useful for time series analysis. There are tools that we can easily analyze.

In this article, I will explain the following topics.

  • What is the time series?
  • What are time series data structures?
  • How to create a time series?
  • What are the important methods used in time series?

Before starting the topic, our Medium page includes posts on data science, artificial intelligence, machine learning, and deep learning. Please don’t forget to follow us on Medium 🌱 to see these posts and the latest posts.

Let’s get started.

#what-is-time-series #pandas #time-series-python #timeseries #time-series-data

What is Time Series Forecasting?

In this article, we will be discussing an algorithm that helps us analyze past trends and lets us focus on what is to unfold next so this algorithm is time series forecasting.

What is Time Series Analysis?

In this analysis, you have one variable -TIME. A time series is a set of observations taken at a specified time usually equal in intervals. It is used to predict future value based on previously observed data points.

Here some examples where time series is used.

  1. Business forecasting
  2. Understand the past behavior
  3. Plan future
  4. Evaluate current accomplishments.

Components of time series :

  1. Trend: Let’s understand by example, let’s say in a new construction area someone open hardware store now while construction is going on people will buy hardware. but after completing construction buyers of hardware will be reduced. So for some times selling goes high and then low its called uptrend and downtrend.
  2. **Seasonality: **Every year chocolate sell goes high during the end of the year due to Christmas. This same pattern happens every year while in the trend that is not the case. Seasonality is repeating same pattern at same intervals.
  3. Irregularity: It is also called noise. When something unusual happens that affects the regularity, for example, there is a natural disaster once in many years lets say it is flooded so people buying medicine more in that period. This what no one predicted and you don’t know how many numbers of sales going to happen.
  4. Cyclic: It is basically repeating up and down movements so this means it can go more than one year so it doesn’t have fix pattern and it can happen any time and it is much harder to predict.

Stationarity of a time series:

A series is said to be “strictly stationary” if the marginal distribution of Y at time t[p(Yt)] is the same as at any other point in time. This implies that the mean, variance, and covariance of the series Yt are time-invariant.

However, a series said to be “weakly stationary” or “covariance stationary” if mean and variance are constant and covariance of two-point Cov(Y1, Y1+k)=Cov(Y2, Y2+k)=const, which depends only on lag k but do not depend on time explicitly.

#machine-learning #time-series-model #machine-learning-ai #time-series-forecasting #time-series-analysis