forked from apache/systemds
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SYSTEMDS-3150] Outlier Detection via DBSCAN
- This commit introduces dbscanApply() method to find the cluster membership of unseen (test) data. Closes apache#1497.
- Loading branch information
1 parent
ac807f4
commit 47533e2
Showing
8 changed files
with
313 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#------------------------------------------------------------- | ||
# | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
# | ||
#------------------------------------------------------------- | ||
# | ||
# Implements the outlier detection/prediction algorithm using a DBScan model | ||
# | ||
# INPUT PARAMETERS: | ||
# ---------------------------------------------------------------------------- | ||
# NAME TYPE DEFAULT MEANING | ||
# ---------------------------------------------------------------------------- | ||
# Xtest Matrix[Double] --- The input Matrix to do outlier detection on. | ||
# clusterModel Matrix[Double] --- Model of clusters to predict outliers against. | ||
# eps Double 0.5 Maximum distance between two points for one to be considered reachable for the other. | ||
|
||
# OUTPUT PARAMETERS: | ||
# ---------------------------------------------------------------------------- | ||
# NAME TYPE DEFAULT MEANING | ||
# ---------------------------------------------------------------------------- | ||
# outlierPoints Matrix[Double] --- Predicted outliers | ||
|
||
|
||
m_dbscanApply = function (Matrix[Double] Xtest, Matrix[Double] clusterModel, Double eps = 0.5) | ||
return (Matrix[double] outlierPoints) | ||
{ | ||
num_features_Xtest = ncol(Xtest); | ||
num_rows_Xtest = nrow(Xtest); | ||
num_features_model = ncol(clusterModel); | ||
num_rows_model = nrow(clusterModel); | ||
|
||
if(num_features_Xtest != num_features_model) {stop("DBSCAN Outlier: Stopping due to invalid inputs: features need to match");} | ||
if(eps < 0) { stop("DBSCAN Outlier: Stopping due to invalid inputs: Epsilon (eps) should be greater than 0"); } | ||
if(num_rows_model <= 0) { stop("DBSCAN Outlier: Stopping due to invalid inputs: Model is empty"); } | ||
|
||
X = rbind(clusterModel, Xtest); | ||
neighbors = dist(X); | ||
neighbors = replace(target = neighbors, pattern = 0, replacement = 2.225e-307); | ||
neighbors = neighbors - diag(diag(neighbors)); | ||
Xtest_dists = neighbors[(num_rows_model+1):nrow(X), 1:num_rows_model]; | ||
withinEps = ((Xtest_dists <= eps) * (0 < Xtest_dists)); | ||
outlierPoints = rowSums(withinEps) >= 1; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
src/test/java/org/apache/sysds/test/functions/builtin/part1/BuiltinDbscanApplyTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package org.apache.sysds.test.functions.builtin.part1; | ||
import org.apache.sysds.common.Types.ExecMode; | ||
import org.apache.sysds.common.Types.ExecType; | ||
import org.apache.sysds.runtime.matrix.data.MatrixValue.CellIndex; | ||
import org.apache.sysds.test.AutomatedTestBase; | ||
import org.apache.sysds.test.TestConfiguration; | ||
import org.apache.sysds.test.TestUtils; | ||
import org.junit.Test; | ||
import java.util.HashMap; | ||
|
||
public class BuiltinDbscanApplyTest extends AutomatedTestBase | ||
{ | ||
private final static String TEST_NAME = "dbscanApply"; | ||
private final static String TEST_DIR = "functions/builtin/"; | ||
private static final String TEST_CLASS_DIR = TEST_DIR + BuiltinDbscanApplyTest.class.getSimpleName() + "/"; | ||
|
||
private final static double eps = 1e-9; | ||
private final static int rows = 1700; | ||
private final static int cols = 3; | ||
private final static int min = -10; | ||
private final static int max = 10; | ||
|
||
private final static int minPts = 5; | ||
|
||
@Override | ||
public void setUp() { | ||
addTestConfiguration(TEST_NAME,new TestConfiguration(TEST_CLASS_DIR, TEST_NAME,new String[]{"B"})); | ||
} | ||
|
||
@Test | ||
public void testDBSCANOutlierDefault0CP() { | ||
runOutlierByDBSCAN(true, 6, 18, 1, ExecType.CP); | ||
} | ||
|
||
@Test | ||
public void testDBSCANOutlierDefault0SP() { | ||
runOutlierByDBSCAN(true, 6, 18, 1, ExecType.SPARK); | ||
} | ||
|
||
@Test | ||
public void testDBSCANOutlierDefault1CP() { | ||
runOutlierByDBSCAN(true, 5, 15, 1, ExecType.CP); | ||
} | ||
|
||
@Test | ||
public void testDBSCANOutlierDefault1SP() { | ||
runOutlierByDBSCAN(true, 5, 15, 1, ExecType.SPARK); | ||
} | ||
|
||
@Test | ||
public void testDBSCANOutlierDefault2CP() { | ||
runOutlierByDBSCAN(true, 12, 77, 1, ExecType.CP); | ||
} | ||
|
||
@Test | ||
public void testDBSCANOutlierDefault2SP() { | ||
runOutlierByDBSCAN(true, 12, 77, 1, ExecType.SPARK); | ||
} | ||
|
||
private void runOutlierByDBSCAN(boolean defaultProb, int seedA, int seedB, double epsDB, ExecType instType) | ||
{ | ||
ExecMode platformOld = setExecMode(instType); | ||
|
||
try | ||
{ | ||
loadTestConfiguration(getTestConfiguration(TEST_NAME)); | ||
String HOME = SCRIPT_DIR + TEST_DIR; | ||
|
||
fullDMLScriptName = HOME + TEST_NAME + ".dml"; | ||
programArgs = new String[]{"-explain","-nvargs", | ||
"X=" + input("A"), "Y=" + input("B"),"Z=" + output("C"), "eps=" + epsDB, "minPts=" + minPts}; | ||
fullRScriptName = HOME + TEST_NAME + ".R"; | ||
rCmd = getRCmd(inputDir(), inputDir(), Double.toString(epsDB), Integer.toString(minPts), expectedDir()); | ||
|
||
//generate actual dataset | ||
double[][] A = getNonZeroRandomMatrix(rows, cols, min, max, seedA); | ||
writeInputMatrixWithMTD("A", A, true); | ||
double[][] B = getNonZeroRandomMatrix(rows, cols, min, max, seedB); | ||
writeInputMatrixWithMTD("B", B, true); | ||
|
||
runTest(true, false, null, -1); | ||
runRScript(true); | ||
|
||
//compare matrices | ||
HashMap<CellIndex, Double> dmlfile = readDMLMatrixFromOutputDir("C"); | ||
HashMap<CellIndex, Double> rfile = readRMatrixFromExpectedDir("C"); | ||
|
||
TestUtils.compareMatrices(dmlfile, rfile, eps, "Stat-DML", "Stat-R"); | ||
} | ||
finally { | ||
rtplatform = platformOld; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#------------------------------------------------------------- | ||
# | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
# | ||
#------------------------------------------------------------- | ||
|
||
args<-commandArgs(TRUE) | ||
library("Matrix") | ||
library("dbscan") | ||
|
||
X = as.matrix(readMM(paste(args[1], "A.mtx", sep=""))); | ||
Y = as.matrix(readMM(paste(args[2], "B.mtx", sep=""))); | ||
eps = as.double(args[3]); | ||
minPts = as.integer(args[4]); | ||
dbModel = dbscan(X, eps, minPts); | ||
|
||
cleanMatr = matrix(, nrow = nrow(X), ncol = 3) | ||
for(i in 1:nrow(X)) { | ||
if(dbModel$cluster[i] > 0) { | ||
cleanMatr[i,] = X[i,] | ||
} | ||
} | ||
|
||
cleanMatr = cleanMatr[rowSums(is.na(cleanMatr)) != ncol(cleanMatr),] | ||
|
||
dbModelClean = dbscan(cleanMatr, eps, minPts); | ||
|
||
Z = predict(dbModelClean, Y, data = cleanMatr); | ||
Z[Z > 0] = 1; | ||
writeMM(as(Z, "CsparseMatrix"), paste(args[5], "C", sep="")); |
Oops, something went wrong.