|
1 | | -# SVDD |
2 | | - Python code for abnormal detection or fault detection using Support Vector Data Description (SVDD) |
| 1 | +# Support Vector Data Description (SVDD) |
| 2 | + |
| 3 | +Python Code for abnormal detection or fault detection using SVDD. |
| 4 | + |
| 5 | +Version 1.0, 1-DEC-2019 |
| 6 | + |
| 7 | +Email: iqiukp@outlook.com |
| 8 | + |
| 9 | +------------------------------------------------------------------- |
| 10 | + |
| 11 | +## Main features |
| 12 | + |
| 13 | +* SVDD model for training dataset containing only positive training data. (SVDD) |
| 14 | +* SVDD model for training dataset containing both positive training data and negative training data. (nSVDD) |
| 15 | +* Multiple kinds of kernel functions. |
| 16 | +* Visualization module including ROC curve plotting, test result plotting, and decision boundary. |
| 17 | + |
| 18 | +------------------------------------------------------------------- |
| 19 | + |
| 20 | +## Requirements |
| 21 | + |
| 22 | +* matplotlib |
| 23 | +* cvxopt |
| 24 | +* scipy |
| 25 | +* numpy |
| 26 | +* scikit_learn |
| 27 | + |
| 28 | +------------------------------------------------------------------- |
| 29 | + |
| 30 | +## About SVDD model |
| 31 | + |
| 32 | +Two types of SVDD models are built according to the following references: |
| 33 | + |
| 34 | +[1] Tax D M J, Duin R P W. Support vector data description[J]. Machine learning, 2004, 54(1): 45-66. |
| 35 | + |
| 36 | +------------------------------------------------------------------- |
| 37 | + |
| 38 | +## A simple application for decision boundary (using differnent kernel functions) |
| 39 | + |
| 40 | +``` |
| 41 | +
|
| 42 | +# -*- coding: utf-8 -*- |
| 43 | +
|
| 44 | +import sys |
| 45 | +sys.path.append("..") |
| 46 | +from src.svdd import SVDD |
| 47 | +from src.visualize import Visualization as draw |
| 48 | +from data import PrepareData as load |
| 49 | +
|
| 50 | +# load banana-shape data |
| 51 | +trainData, testData, trainLabel, testLabel = load.banana() |
| 52 | +
|
| 53 | +
|
| 54 | +# kernel list |
| 55 | +kernelList = {"1": {"type": 'gauss', "width": 1/24}, |
| 56 | + "2": {"type": 'linear', "offset": 0}, |
| 57 | + "3": {"type": 'ploy', "degree": 2, "offset": 0}, |
| 58 | + "4": {"type": 'tanh', "gamma": 1e-4, "offset": 0}, |
| 59 | + "5": {"type": 'lapl', "width": 1/12} |
| 60 | + } |
| 61 | +
|
| 62 | +
|
| 63 | +for i in range(len(kernelList)): |
| 64 | +
|
| 65 | + # set SVDD parameters |
| 66 | + parameters = {"positive penalty": 0.9, |
| 67 | + "negative penalty": 0.8, |
| 68 | + "kernel": kernelList.get(str(i+1)), |
| 69 | + "option": {"display": 'on'}} |
| 70 | + |
| 71 | + # construct an SVDD model |
| 72 | + svdd = SVDD(parameters) |
| 73 | + |
| 74 | + # train SVDD model |
| 75 | + svdd.train(trainData, trainLabel) |
| 76 | + |
| 77 | + # test SVDD model |
| 78 | + distance, accuracy = svdd.test(testData, testLabel) |
| 79 | + |
| 80 | + # visualize the results |
| 81 | + # draw.testResult(svdd, distance) |
| 82 | + # draw.testROC(testLabel, distance) |
| 83 | + draw.boundary(svdd, trainData, trainLabel) |
| 84 | +
|
| 85 | +``` |
| 86 | + |
| 87 | +* gaussian kernel function |
| 88 | + |
| 89 | +<p align="middle"> |
| 90 | + <img src="https://github.com/iqiukp/SVDD/blob/master/imgs/kernel_gauss.png" width="720"> |
| 91 | +</p> |
| 92 | + |
| 93 | +* linear kernel function |
| 94 | + |
| 95 | +<p align="middle"> |
| 96 | + <img src="https://github.com/iqiukp/SVDD/blob/master/imgs/kernel_linear.png" width="720"> |
| 97 | +</p> |
| 98 | + |
| 99 | +* polynomial kernel function |
| 100 | + |
| 101 | +<p align="middle"> |
| 102 | + <img src="https://github.com/iqiukp/SVDD/blob/master/imgs/kernel_ploy.png" width="720"> |
| 103 | +</p> |
| 104 | + |
| 105 | +* sigmoid kernel function |
| 106 | + |
| 107 | +<p align="middle"> |
| 108 | + <img src="https://github.com/iqiukp/SVDD/blob/master/imgs/kernel_tanh.png" width="720"> |
| 109 | +</p> |
| 110 | + |
| 111 | +* laplacian kernel function |
| 112 | + |
| 113 | +<p align="middle"> |
| 114 | + <img src="https://github.com/iqiukp/SVDD/blob/master/imgs/kernel_lapl.png" width="720"> |
| 115 | +</p> |
| 116 | + |
| 117 | + |
| 118 | +## A simple application for abnormal detection or fault detection |
| 119 | + |
| 120 | +``` |
| 121 | +
|
| 122 | +# -*- coding: utf-8 -*- |
| 123 | +
|
| 124 | +import sys |
| 125 | +sys.path.append("..") |
| 126 | +from src.svdd import SVDD |
| 127 | +from src.visualize import Visualization as draw |
| 128 | +from data import PrepareData as load |
| 129 | +
|
| 130 | +# load banana-shape data |
| 131 | +trainData, testData, trainLabel, testLabel = load.iris() |
| 132 | +
|
| 133 | +# set SVDD parameters |
| 134 | +parameters = {"positive penalty": 0.9, |
| 135 | + "negative penalty": 0.8, |
| 136 | + "kernel": {"type": 'gauss', "width": 1/24}, |
| 137 | + "option": {"display": 'on'}} |
| 138 | +
|
| 139 | +
|
| 140 | +# construct an SVDD model |
| 141 | +svdd = SVDD(parameters) |
| 142 | +
|
| 143 | +# train SVDD model |
| 144 | +svdd.train(trainData, trainLabel) |
| 145 | +
|
| 146 | +
|
| 147 | +# test SVDD model |
| 148 | +distance, accuracy = svdd.test(testData, testLabel) |
| 149 | +
|
| 150 | +# visualize the results |
| 151 | +draw.testResult(svdd, distance) |
| 152 | +draw.testROC(testLabel, distance) |
| 153 | +
|
| 154 | +``` |
| 155 | + |
| 156 | +* test result |
| 157 | + |
| 158 | +<p align="middle"> |
| 159 | + <img src="https://github.com/iqiukp/SVDD/blob/master/imgs/hybrid_result.png" width="720"> |
| 160 | +</p> |
| 161 | + |
| 162 | +* ROC curve |
| 163 | + |
| 164 | +<p align="middle"> |
| 165 | + <img src="https://github.com/iqiukp/SVDD/blob/master/imgs/hybrid_roc.png" width="480"> |
| 166 | +</p> |
| 167 | + |
0 commit comments