Anomaly Detection, a short tutorial using Python__Python

來源:互聯網
上載者:User

Anomaly detection is the problem of identifying data points that don't conform to expected (normal) behaviour. Unexpected data points are also known as outliers and exceptions etc. Anomaly detection has crucial significance in the wide variety of domains as it provides critical and actionable information. For example, an anomaly in MRI image scan could be an indication of the malignant tumour or anomalous reading from production plant sensor may indicate faulty component.

Simply, anomaly detection is the task of defining a boundary around normal data points so that they can be distinguishable from outliers. But several different factors make this notion of defining normality very challenging. E.g. normal behaviour usually evolve in certain domains and the notion that is considered normal in the present could change in future. Moreover, defining the normal region which separates outliers from normal data points is not straightforward in itself.

In this tutorial, we will implement anomaly detection algorithm (in Python) to detect outliers in computer servers. This algorithm is dissuced by Andrew Ng in his course of Machine Learning on Coursera. To keep things simple we will use two features 1) throughput in mb/s and 2) latency in ms of response for each server. The Gaussian model will be used to learn an underlying pattern of the dataset with the hope that our features follow the gaussian distribution. After that, we will find data points with very low probabilities of being normal and hence can be considered outliers. For training set, we will first learn the gaussian distribution of each feature for which mean and variance of features are required. Numpy provides the method to calculate both mean and variance (covariance matrix) efficiently. Similarly, Scipy library provide method to estimate gaussian distribution.

Let's get started! By first importing requried libraries and defining functions for reading data, mean normalizing features and estimating gaussian distribution.

import matplotlib.pyplot as pltimport numpy as np%matplotlib inlinefrom numpy import genfromtxtfrom scipy.stats import multivariate_normalfrom sklearn.metrics import f1_score
def read_dataset(filePath,delimiter=','):    return genfromtxt(filePath, delimiter=delimiter)def feature_normalize(dataset):    mu = np.mean(dataset,axis=0)    sigma = np.std(dataset,axis=0)    return (dataset - mu)/sigmadef estimateGaussian(dataset):    mu = np.mean(dataset, axis=0)    sigma = np.cov(dataset.T)    return mu, sigma    def multivariateGaussian(dataset,mu,sigma):    p = multivariate_normal(mean=mu, cov=sigma)    return p.pdf(dataset)

Next, define a function to find the optimal value for threshold (epsilon) that can be used to differentiate between normal and anomalous data points. For learning the optimal value of epsilon we will try different values in a range of learned probabilities on a cross-validation set. The f-score will be calculated for predicted anomalies based on the ground truth data available. The epsilon value with highest f-score will be selected as threshold i.e. the probabilities that lie below the selected threshold will be considered anomalous.

def selectThresholdByCV(probs,gt):    best_epsilon = 0    best_f1 = 0    f = 0    stepsize = (max(probs) - min(probs)) / 1000;    epsilons = np.arange(min(probs),max(probs),stepsize)    for epsilon in np.nditer(epsilons):        predictions = (probs < epsilon)        f = f1_score(gt, predictions, average = "binary")        if f > best_f1:            best_f1 = f            best_epsilon = epsilon    return best_f1, best_epsilon

We have all the required pieces, next let's call above defined functions to find anomalies in the dataset. Also, as we are dealing with only two features here, plotting helps us visualize the anomalous data points.

tr_data = read_dataset('tr_server_data.csv') cv_data = read_dataset('cv_server_data.csv') gt_data = read_dataset('gt_server_data.csv')n_training_samples = tr_data.shape[0]n_dim = tr_data.shape[1]plt.figure()plt.xlabel("Latency (ms)")plt.ylabel("Throughput (mb/s)")plt.plot(tr_data[:,0],tr_data[:,1],"bx")plt.show()

mu, sigma = estimateGaussian(tr_data)p = multivariateGaussian(tr_data,mu,sigma)p_cv = multivariateGaussian(cv_data,mu,sigma)fscore, ep = selectThresholdByCV(p_cv,gt_data)outliers = np.asarray(np.where(p < ep))plt.figure() plt.xlabel("Latency (ms)") plt.ylabel("Throughput (mb/s)") plt.plot(tr_data[:,0],tr_data[:,1],"bx") plt.plot(tr_data[outliers,0],tr_data[outliers,1],"ro") plt.show()

We implemented a very simple anomaly detection algorithm. To gain more in-depth knowledge, please consult following resource:Chandola, Varun, Arindam Banerjee, and Vipin Kumar. "Anomaly detection: A survey." ACM computing surveys (CSUR) 41.3 (2009): 15.

The complete code (Python notebook) and the dataset is available at the following link.

Last Updated: 24/1/2017 http://aqibsaeed.github.io/2016-07-17-anomaly-detection/

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.