Anomaly Detection: From Industry Insights to Python Scripts
Saeed
By Saeed Mirshekari

August 24, 2023

Anomaly Detection Demystified: Unveiling Industry Applications with Python

Hey there, curious minds! Ever wondered how industries keep a vigilant eye on their operations to spot those sneaky outliers that don't quite fit the norm? Well, you're about to embark on an exciting journey into the world of anomaly detection – a superpower that helps industries from finance to healthcare stay on top of their game. Buckle up as we dive into the magic of spotting those anomalies and even cook up some Python scripts to simulate solutions. Let's roll!

Anomaly Detection Unveiled

Picture this: You're analyzing a dataset, and suddenly you stumble upon a data point that's way off the charts, defying all patterns. That's an anomaly – an outlier that doesn't follow the crowd. Anomaly detection is like playing detective with data. It's the art of identifying these oddballs, helping industries catch fraud, prevent failures, and make smarter decisions.

Finance: Unmasking Financial Fraud

Ah, the financial realm – a goldmine for anomalies. Imagine a cybercriminal making off with your hard-earned money through a fraudulent transaction. Here's where anomaly detection steps in. By analyzing transaction patterns, a bank can build models that raise red flags when a transaction seems fishy.

import numpy as np
from sklearn.ensemble import IsolationForest

# Simulating transaction data (legitimate and fraudulent)
legitimate_transactions = np.random.normal(100, 20, 800).reshape(-1, 1)
fraudulent_transactions = np.random.normal(500, 100, 50).reshape(-1, 1)
transactions = np.vstack((legitimate_transactions, fraudulent_transactions))

# Training the Isolation Forest model
model = IsolationForest(contamination=0.05)
model.fit(transactions)

# Predicting anomalies
predictions = model.predict(transactions)

Healthcare: Detecting Disease Outbreaks

Healthcare, meet data science! Anomaly detection here isn't about finding odd medical conditions; it's about spotting unusual patterns that might indicate a disease outbreak. By analyzing patient symptoms and health data, public health agencies can identify spikes that hint at a potential epidemic.

import pandas as pd
from sklearn.cluster import DBSCAN

# Simulating health data (symptoms, age, location)
data = {'Symptoms': [4, 5, 3, 2, 7, 8, 6, 2, 4, 6],
        'Age': [30, 45, 22, 28, 60, 73, 35, 29, 42, 32]}
df = pd.DataFrame(data)

# Clustering with DBSCAN
model = DBSCAN(eps=3, min_samples=2)
df['Cluster'] = model.fit_predict(df)

# Identifying anomalies
anomalies = df[df['Cluster'] == -1]

Manufacturing: Preventing Equipment Failures

In manufacturing, a single malfunctioning machine can spell disaster. Anomaly detection helps industries predict equipment failures by monitoring sensor data. If a machine starts spewing out data that's way off the historical patterns, it's time to take action before things go haywire.

import numpy as np
from sklearn.ensemble import RandomForestRegressor

# Simulating sensor data (time, temperature, pressure)
time = np.linspace(0, 10, 100).reshape(-1, 1)
temperature = np.random.normal(40, 5, 100).reshape(-1, 1)
pressure = np.random.normal(30, 3, 100).reshape(-1, 1)
sensor_data = np.hstack((time, temperature, pressure))

# Training a Random Forest Regressor
model = RandomForestRegressor(n_estimators=50)
model.fit(time, temperature)

# Predicting temperatures and spotting anomalies
predicted_temperature = model.predict(time)
residuals = temperature - predicted_temperature
anomalies = np.where(np.abs(residuals) > 2 * np.std(residuals))[0]

E-Commerce: Recommending Your Next Buy

Ever notice how Amazon suggests products that feel eerily spot-on? Anomaly detection plays a part here too. By analyzing your browsing and purchasing history, e-commerce giants can spot unusual behavior and recommend products that align with your preferences.

import pandas as pd
from sklearn.ensemble import IsolationForest

# Simulating user behavior data (time spent, items viewed)
data = {'TimeSpent': [10, 15, 8, 12, 20, 25, 5, 7, 18, 30],
        'ItemsViewed': [5, 7, 3, 4, 8, 10, 2, 3, 6, 12]}
df = pd.DataFrame(data)

# Training an Isolation Forest model
model = IsolationForest(contamination=0.1)
model.fit(df)

# Predicting anomalies
predictions = model.predict(df)

Wrapping Up

And there you have it, anomaly detection demystified across different industries! We've seen how it's not just about spotting oddballs, but about keeping industries secure, efficient, and smart. Armed with Python scripts, you're now ready to embark on your anomaly-hunting adventures. Remember, whether it's finance, healthcare, manufacturing, or e-commerce, anomalies are everywhere – waiting to be uncovered.

So, go forth, embrace the power of data, and let anomaly detection be your guiding light in the labyrinth of information. Happy data sleuthing, my fellow enthusiasts!

# Top Anomaly Detection Algorithms and How They Work

1. Isolation Forest

Imagine you're in a dense forest, and you're on a mission to spot a rare, isolated tree. The Isolation Forest algorithm works in a similar way – it's all about isolating anomalies from the majority.

How it Works: Isolation Forest builds a random forest of decision trees, where each tree tries to isolate a point by creating splits based on random features and values. The idea is that anomalies will require fewer splits to be isolated compared to normal data points. By counting the average number of splits required to isolate a point across all trees, the algorithm assigns an anomaly score. Lower scores indicate anomalies.

from sklearn.ensemble import IsolationForest

# Training the Isolation Forest model
model = IsolationForest(contamination=0.05)
model.fit(data)

# Predicting anomalies
anomaly_scores = model.decision_function(data)

2. DBSCAN (Density-Based Spatial Clustering of Applications with Noise)

Ever thought of identifying clusters of friends in a crowd? DBSCAN does that for data points, with a special twist – it doesn't force every point into a cluster, giving room for those loner anomalies.

How it Works: DBSCAN looks at the density of points around each data point. It forms clusters by connecting points that are densely packed, while classifying points with few neighbors as outliers. This allows DBSCAN to naturally discover irregularly shaped clusters and, at the same time, label anomalies as points with low densities.

from sklearn.cluster import DBSCAN

# Clustering with DBSCAN
model = DBSCAN(eps=3, min_samples=2)
labels = model.fit_predict(data)

# Identifying anomalies
anomalies = data[labels == -1]

3. One-Class SVM (Support Vector Machine)

Picture a superhero that can recognize its allies even if it has never seen its enemies. That's One-Class SVM – it learns what's normal and identifies anything that deviates from it.

How it Works: One-Class SVM trains on the "normal" data and tries to find a boundary that encloses most of the data points. It aims to maximize the margin around this boundary while allowing a certain amount of error (anomalies) within it. Data points outside the boundary are classified as anomalies.

from sklearn.svm import OneClassSVM

# Training One-Class SVM model
model = OneClassSVM(nu=0.05)
model.fit(data)

# Predicting anomalies
anomaly_labels = model.predict(data)
anomalies = data[anomaly_labels == -1]

Closing Thoughts

There you have it – a sneak peek into the inner workings of three top-notch anomaly detection algorithms: Isolation Forest, DBSCAN, and One-Class SVM. Just like detectives with different approaches to solving mysteries, these algorithms have their unique ways of uncovering anomalies lurking in your data. Armed with this knowledge, you're ready to put these algorithms to work and unleash your data-sleuthing prowess! 🕵️‍♀️

If you like our work, you will love our newsletter..💚

About O'Fallon Labs

In O'Fallon Labs we help recent graduates and professionals to get started and thrive in their Data Science careers via 1:1 mentoring and more.


Saeed

Saeed Mirshekari

Saeed is currently a Director of Data Science in Mastercard and the Founder & Director of OFallon Labs LLC. He is a former research scholar at LIGO team (Physics Nobel Prize of 2017).

leave a comment



Let's Talk One-on-one!

SCHEDULE FREE CALL

Looking for a Data Science expert to help you score your first or the next Data Science job? Or, are you a business owner wanting to bring value and scale your business through Data Analysis? Either way, you’re in the right place. Let’s talk about your priorities!