forked from ymirsky/KitNET-py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
corClust.py
74 lines (68 loc) · 3.47 KB
/
corClust.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import numpy as np
from scipy.cluster.hierarchy import linkage, fcluster, to_tree
# A helper class for KitNET which performs a correlation-based incremental clustering of the dimensions in X
# n: the number of dimensions in the dataset
# For more information and citation, please see our NDSS'18 paper: Kitsune: An Ensemble of Autoencoders for Online Network Intrusion Detection
class corClust:
def __init__(self,n):
#parameter:
self.n = n
#varaibles
self.c = np.zeros(n) #linear num of features
self.c_r = np.zeros(n) #linear sum of feature residules
self.c_rs = np.zeros(n) #linear sum of feature residules
self.C = np.zeros((n,n)) #partial correlation matrix
self.N = 0 #number of updates performed
# x: a numpy vector of length n
def update(self,x):
self.N += 1
self.c += x
c_rt = x - self.c/self.N
self.c_r += c_rt
self.c_rs += c_rt**2
self.C += np.outer(c_rt,c_rt)
# creates the current correlation distance matrix between the features
def corrDist(self):
c_rs_sqrt = np.sqrt(self.c_rs)
C_rs_sqrt = np.outer(c_rs_sqrt,c_rs_sqrt)
C_rs_sqrt[C_rs_sqrt==0] = 1e-100 #this protects against dive by zero erros (occurs when a feature is a constant)
D = 1-self.C/C_rs_sqrt #the correlation distance matrix
D[D<0] = 0 #small negatives may appear due to the incremental fashion in which we update the mean. Therefore, we 'fix' them
return D
# clusters the features together, having no more than maxClust features per cluster
def cluster(self,maxClust):
D = self.corrDist()
Z = linkage(D[np.triu_indices(self.n, 1)]) # create a linkage matrix based on the distance matrix
if maxClust < 1:
maxClust = 1
if maxClust > self.n:
maxClust = self.n
map = self.__breakClust__(to_tree(Z),maxClust)
return map
# a recursive helper function which breaks down the dendrogram branches until all clusters have no more than maxClust elements
def __breakClust__(self,dendro,maxClust):
if dendro.count <= maxClust: #base case: we found a minimal cluster, so mark it
return [dendro.pre_order()] #return the origional ids of the features in this cluster
return self.__breakClust__(dendro.get_left(),maxClust) + self.__breakClust__(dendro.get_right(),maxClust)
# Copyright (c) 2017 Yisroel Mirsky
#
# MIT License
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.