Multi-variate Outlier Detection using Normal Distribution

Java implementation of multi-variate outlier detection using normal distribution

License

License

MIT
Categories

Categories

Java Languages
GroupId

GroupId

com.github.chen0040
ArtifactId

ArtifactId

java-outliers
Last Version

Last Version

1.0.1
Release Date

Release Date

Type

Type

jar
Description

Description

Multi-variate Outlier Detection using Normal Distribution
Java implementation of multi-variate outlier detection using normal distribution
Project URL

Project URL

https://github.com/chen0040/java-outliers
Source Code Management

Source Code Management

https://github.com/chen0040/java-outliers

Download java-outliers

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.chen0040/java-outliers/ -->
<dependency>
    <groupId>com.github.chen0040</groupId>
    <artifactId>java-outliers</artifactId>
    <version>1.0.1</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.chen0040/java-outliers/
implementation 'com.github.chen0040:java-outliers:1.0.1'
// https://jarcasting.com/artifacts/com.github.chen0040/java-outliers/
implementation ("com.github.chen0040:java-outliers:1.0.1")
'com.github.chen0040:java-outliers:jar:1.0.1'
<dependency org="com.github.chen0040" name="java-outliers" rev="1.0.1">
  <artifact name="java-outliers" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.chen0040', module='java-outliers', version='1.0.1')
)
libraryDependencies += "com.github.chen0040" % "java-outliers" % "1.0.1"
[com.github.chen0040/java-outliers "1.0.1"]

Dependencies

compile (5)

Group / Artifact Type Version
org.slf4j : slf4j-api jar 1.7.20
org.slf4j : slf4j-log4j12 jar 1.7.20
org.apache.commons : commons-math3 jar 3.2
com.github.chen0040 : java-data-frame jar 1.0.9
gov.nist.math : jama jar 1.0.3

provided (1)

Group / Artifact Type Version
org.projectlombok : lombok jar 1.16.6

test (10)

Group / Artifact Type Version
org.testng : testng jar 6.9.10
org.hamcrest : hamcrest-core jar 1.3
org.hamcrest : hamcrest-library jar 1.3
org.assertj : assertj-core jar 3.5.2
org.powermock : powermock-core jar 1.6.5
org.powermock : powermock-api-mockito jar 1.6.5
org.powermock : powermock-module-junit4 jar 1.6.5
org.powermock : powermock-module-testng jar 1.6.5
org.mockito : mockito-core jar 2.0.2-beta
org.mockito : mockito-all jar 2.0.2-beta

Project Modules

There are no modules declared in this project.

java-outliers

Package provide java implementation of outlier detection using normal distribution for multi-variate datasets

Install

Add the following dependency to your POM file:

<dependency>
  <groupId>com.github.chen0040</groupId>
  <artifactId>java-outliers</artifactId>
  <version>1.0.1</version>
</dependency>

Usage

The anomaly detection algorithms takes data that is prepared and stored in a data frame (Please refers to this link on how to create a data frame from file or from scratch)

The MultiVariateNormalOutliers can be trained using unsupervised learning.

To create and train the MultiVariateNormalOutliers, run the following code:

MultiVariateNormalOutliers method = new MultiVariateNormalOutliers();
method.fit(trainingData);

To test the trained method on new data, run:

boolean outlier = method.isAnomaly(dataRow);

Complete sample code for LOF

The problem that we will be using as demo as the following anomaly detection problem:

scki-learn example for one-class

Below is the sample code which illustrates how to use MultiVariateNormalOutliers to detect outliers in the above problem:

DataQuery.DataFrameQueryBuilder schema = DataQuery.blank()
      .newInput("c1")
      .newInput("c2")
      .newOutput("anomaly")
      .end();

Sampler.DataSampleBuilder negativeSampler = new Sampler()
      .forColumn("c1").generate((name, index) -> randn() * 0.3 + (index % 2 == 0 ? -2 : 2))
      .forColumn("c2").generate((name, index) -> randn() * 0.3 + (index % 2 == 0 ? -2 : 2))
      .forColumn("anomaly").generate((name, index) -> 0.0)
      .end();

Sampler.DataSampleBuilder positiveSampler = new Sampler()
      .forColumn("c1").generate((name, index) -> rand(-4, 4))
      .forColumn("c2").generate((name, index) -> rand(-4, 4))
      .forColumn("anomaly").generate((name, index) -> 1.0)
      .end();

DataFrame trainingData = schema.build();

trainingData = negativeSampler.sample(trainingData, 200);
trainingData = positiveSampler.sample(trainingData, 200);

System.out.println(trainingData.head(10));

DataFrame crossValidationData = schema.build();

crossValidationData = negativeSampler.sample(crossValidationData, 40);
crossValidationData = positiveSampler.sample(crossValidationData, 40);

MultiVariateNormalOutliers method = new MultiVariateNormalOutliers();
method.fit(trainingData);

BinaryClassifierEvaluator evaluator = new BinaryClassifierEvaluator();

for(int i = 0; i < crossValidationData.rowCount(); ++i){
 boolean predicted = method.isAnomaly(crossValidationData.row(i));
 boolean actual = crossValidationData.row(i).target() > 0.5;
 evaluator.evaluate(actual, predicted);
 logger.info("predicted: {}\texpected: {}", predicted, actual);
}

evaluator.report();

Versions

Version
1.0.1