forked from shubhomoydas/ad_examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspectral_outlier.py
More file actions
executable file
·175 lines (134 loc) · 5.78 KB
/
spectral_outlier.py
File metadata and controls
executable file
·175 lines (134 loc) · 5.78 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import numpy.random as rnd
from sklearn import manifold
from sklearn.ensemble import IsolationForest
from common.gen_samples import *
"""
pythonw -m ad.spectral_outlier
"""
def euclidean_dist(x1, x2):
dist = np.sqrt(np.sum((x1 - x2) ** 2))
return dist
class LabelDiffusion(object):
"""
IMPORTANT: The results from Python's Scikit-Learn MDS API are significantly
different (and sub-optimal) from R. Strongly recommend R's isoMDS for the last
step of converting pair-wise distances to 2D coordinates.
"""
def __init__(self, n_neighbors=10, k2=0.5, alpha=0.99,
n_components=2, eigen_solver='auto',
tol=0., max_iter=None, n_jobs=1, metric=True):
self.n_neighbors = n_neighbors
self.k2 = k2
self.alpha = alpha
self.n_components = n_components
self.eigen_solver = eigen_solver
self.tol = tol
self.max_iter = max_iter
self.n_jobs = n_jobs
self.metric = metric
self.alphas_ = None
self.lambdas_ = None
def fit_transform(self, x_in):
n = nrow(x_in)
x = normalize_and_center_by_feature_range(x_in)
dists = np.zeros(shape=(n, n), dtype=float)
for i in range(n):
for j in range(i, n):
dists[i, j] = euclidean_dist(x[i, :], x[j, :])
dists[j, i] = dists[i, j]
logger.debug(dists[0, 0:10])
neighbors = np.zeros(shape=(n, self.n_neighbors), dtype=int)
for i in range(n):
neighbors[i, :] = np.argsort(dists[i, :])[0:self.n_neighbors]
logger.debug(neighbors[0, 0:10])
W = np.zeros(shape=(n, n))
for i in range(n):
for j in neighbors[i, :]:
# diagonal elements of W will be zeros
if i != j:
W[i, j] = np.exp(-(dists[i, j] ** 2) / self.k2)
W[j, i] = W[i, j]
D = W.sum(axis=1)
# logger.debug(str(list(D[0:10])))
iDroot = np.diag(np.sqrt(D) ** (-1))
S = iDroot.dot(W.dot(iDroot))
# logger.debug("S: %s" % str(list(S[0, 0:10])))
B = np.eye(n) - self.alpha * S
# logger.debug("B: %s" % str(list(B[0, 0:10])))
A = np.linalg.inv(B)
tdA = np.diag(np.sqrt(np.diag(A)) ** (-1))
A = tdA.dot(A.dot(tdA))
# logger.debug("A: %s" % str(list(A[0, 0:10])))
d = 1 - A
# logger.debug("d: %s" % str(list(d[0, 0:10])))
# logger.debug("min(d): %f, max(d): %f" % (np.min(d), np.max(d)))
mds = manifold.MDS(self.n_components,
metric=self.metric, dissimilarity='precomputed')
# using abs below because some zeros are represented as -0; other values are positive.
embedding = mds.fit_transform(np.abs(d))
return embedding
if __name__ == "__main__":
logger = logging.getLogger(__name__)
args = get_command_args(debug=True, debug_args=["--debug",
"--plot",
"--log_file=temp/spectral_outlier.log"])
# print "log file: %s" % args.log_file
configure_logger(args)
# sample_type = "4_"
# sample_type = "donut_"
sample_type = "face_"
rnd.seed(42)
x, y = get_demo_samples(sample_type)
n = x.shape[0]
xx = yy = x_grid = Z = scores = None
if args.plot:
plot_sample(x, y, pdfpath="temp/spectral_%ssamples.pdf" % sample_type)
n_neighbors = 10
n_components = 2
method = "standard" # ['standard', 'ltsa', 'hessian', 'modified']
# embed_type = "se"
# embed_type = "tsne"
# embed_type = "isomap"
# embed_type = "mds"
# embed_type = "lle_%s" % method
embed_type = "diffusion"
if embed_type == "se":
embed = manifold.SpectralEmbedding(n_components=n_components, n_neighbors=n_neighbors)
elif embed_type == "tsne":
embed = manifold.TSNE(n_components=n_components, init='pca', random_state=0)
elif embed_type.startswith("lle_"):
embed = manifold.LocallyLinearEmbedding(n_neighbors=n_neighbors, n_components=n_components,
eigen_solver='auto', method=method)
elif embed_type == "isomap":
embed = manifold.Isomap(n_neighbors=n_neighbors, n_components=n_components)
elif embed_type == "mds":
embed = manifold.MDS(n_components=n_components)
elif embed_type == "diffusion":
embed = LabelDiffusion(n_neighbors=n_neighbors, n_components=n_components, metric=True)
else:
raise ValueError("invalid embed type %s" % embed_type)
x_tr = embed.fit_transform(x)
logger.debug(x_tr)
if args.plot:
plot_sample(x_tr, y, pdfpath="temp/spectral_%s%s.pdf" % (sample_type, embed_type))
ad_type = 'ifor'
outliers_fraction = 0.1
ad = IsolationForest(max_samples=256, contamination=outliers_fraction, random_state=None)
ad.fit(x_tr)
scores = -ad.decision_function(x_tr)
top_anoms = np.argsort(-scores)[np.arange(10)]
if args.plot:
# to plot probability contours
xx, yy = np.meshgrid(np.linspace(np.min(x_tr[:, 0]), np.max(x_tr[:, 0]), 50),
np.linspace(np.min(x_tr[:, 1]), np.max(x_tr[:, 1]), 50))
x_grid = np.c_[xx.ravel(), yy.ravel()]
Z = -ad.decision_function(x_grid)
Z = Z.reshape(xx.shape)
pdfpath = "temp/spectral_%scontours_%s_%s.pdf" % (sample_type, ad_type, embed_type)
dp = DataPlotter(pdfpath=pdfpath, rows=1, cols=1)
pl = dp.get_next_plot()
pl.contourf(xx, yy, Z, 20, cmap=plt.cm.get_cmap('jet'))
dp.plot_points(x_tr, pl, labels=y, lbl_color_map={0: "grey", 1: "red"}, s=25)
pl.scatter(x_tr[top_anoms, 0], x_tr[top_anoms, 1], marker='o', s=35,
edgecolors='red', facecolors='none')
dp.close()