🔳

4.3 Anomaly Detection

Anomalies

Anomaly detection is used to identify deviations or rare/suspicious events because they deviate from standard behaviors or patterns. These events are also called outliers and lie

Techniques for Anomaly Detection

  1. Statistical Methods: Statistical techniques like mean, standard deviation, and probability distributions to detect anomalies.
  1. Machine Learning Methods:
    1. Isolation Forest: Uses decision trees to isolate anomalies by recursively partitioning the data.
    2. Local Outlier Factor (LOF): Compares the local density of a point to its neighbors. Points with significantly lower density are considered anomalies.
    3. One-Class Support Vector Machine (SVM): A regular support vector machine algorithm tries to find a hyperplane that best separates the two classes of data points. In an SVM that has one class of data points, the task is to predict a hypersphere that separates the cluster of data points from the anomalies.
 

Practical Application of Anomaly Detection

  • Cybersecurity: Identifying unusual network traffic indicating potential attacks.
  • Finance: Detecting fraudulent transactions or abnormal market behaviors.
  • Healthcare: Monitoring patient data to spot irregularities that may indicate health issues.
  • Manufacturing: Predicting equipment failures by monitoring sensor data.

Practice Problem

We have a list of scores for a test. An anomaly is defined as any score that is an outlier from the average score. Write a function to identify them and print them.
 

Instructions:

  1. Input: A list of integers representing scores.
  1. Output: Print each score that qualifies as an anomaly.
  1. Criteria: An anomaly is a score that differs from the average score by more than a specified threshold value.

Example:

Input:
[85, 90, 82, 88, 92, 95, 75, 98, 80, 70]
Output:
75 98 70

Explanation:

  • Average score:
  • Threshold: ±10 points from the average (75.5 to 95.5)
  • Anomalies: Scores 75, 98, and 70 fall outside this range.
 

Code:

Click to see the code:
import java.util.ArrayList; import java.util.List; public class AnomalyDetection { public static void main(String[] args) { int[] scores = {85, 90, 82, 88, 92, 95, 75, 98, 80, 70}; detectAnomalies(scores); } public static void detectAnomalies(int[] scores) { // Calculate average score double sum = 0; for (int score : scores) { sum += score; } double average = sum / scores.length; // Define threshold (e.g., ±10 points) double threshold = 10.0; // Detect anomalies List<Integer> anomalies = new ArrayList<>(); for (int score : scores) { if (Math.abs(score - average) > threshold) { anomalies.add(score); } } // Print anomalies System.out.println("Anomalies detected:"); for (int anomaly : anomalies) { System.out.println(anomaly); } } }
 

Explanation:

  • detectAnomalies method: Calculates the average score from the input array, defines a threshold, and identifies scores that deviate from the average by more than the threshold. Anomalies are stored in a list and printed at the end.

About Me

Create a new class called AboutMe. In the main method, print your name. On the same line, print any message. On the next line(s), print a stanza from your favorite poem. Make sure it's formatted correctly. And of course, be sure to have good programming style!

Previous Section

 
⚖️
Copyright © 2024 Code 4 Tomorrow. All rights reserved. The code in this course is licensed under the MIT License. If you would like to use content from any of our courses, you must obtain our explicit written permission and provide credit. Please contact classes@code4tomorrow.org for inquiries.