forked from memoiry/LightML.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spectralCluster.jl
48 lines (37 loc) · 957 Bytes
/
spectralCluster.jl
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
function spec_clustering(data,k)
w = computing_similarity(data)
d = diagm(vec(sum(w,1)))
l = d-w
temp = eig(l)
temp = temp[2]
e_map = temp[:,1:(k-1)] #seems that the largest k eigenvalue works?
#e_map = temp[:,(end-k+1):end]
model = Kmeans(k=k)
train!(model,e_map)
predict!(model)
return model
end
function computing_similarity(data)
n_sample = size(data,1)
w = zeros(n_sample,n_sample)
for i = 1:n_sample
for j = 1:n_sample
w[i,j] = count_sim(data[i,:],data[j,:])
end
end
return w
end
function count_sim(x::Vector,y::Vector;
types="Gaussian",
gamma = 1)
if types == "Gaussian"
return exp(-gamma*norm(x-y)^2)
end
end
function test_spec_cluster()
X, y = make_blo()
clu = length(unique(y))
model = spec_clustering(X,clu)
predictions = model.clusters
plot_in_2d(model)
end