-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhclustering.jl
160 lines (112 loc) · 4.14 KB
/
hclustering.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
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
#=
hclustering
Copyright © 2014 Mark Wells <[email protected]>
Distributed under terms of the MIT license.
=#
include("helperFuncs.jl")
using PyCall
@pyimport scipy.spatial.distance as dist
@pyimport scipy.cluster.hierarchy as hier
@pyimport sklearn.neighbors as neighbors
@pyimport sklearn.cluster as cluster
@pyimport sklearn.metrics as metrics
using PyPlot
function performCluster(features,kVal,con_matrix)
estimator = cluster.AgglomerativeClustering(n_clusters=kVal,connectivity=con_matrix)
predictedLabels = estimator[:fit_predict](features)
return predictedLabels
end
function evaluateCluster(features,predictedLabels)
# println("sample size: ",size(features)[1])
sample = sqrt(size(features)[1])
sampleSize = int(sample)
# println("sample size for silhouette: ",sampleSize, typeof(sampleSize))
score = metrics.silhouette_score(features,predictedLabels,sample_size=sampleSize)
return score
end
function testVariousK(features,con_matrix)
## Want a range from from 10 to 30 inclusive
kList = range(10,1,21)
scores = Array(Float64,length(kList))
## Perform clustering on a variety of k values
maxscore = 0.0
bestK = 0
for i = 1:length(kList)
try
labels = performCluster(features,kList[i],con_matrix)
score = evaluateCluster(features,labels)
scores[i] = score
## Keep track of the best silhouette score
## and the associated k value
if score > maxscore
maxscore = score
println("Current max score: ", maxscore)
bestK = kList[i]
end
end
end
println("Now writing silhouette scores file")
sil_scores_file_name = "/home/CREATIVE_STATION/Astro-ML-in-Julia/sil_scores.csv"
writecsv(sil_scores_file_name,scores)
println("Now writing kList file")
kList_file_name = "/home/CREATIVE_STATION/Astro-ML-in-Julia/kList.csv"
writecsv(kList_file_name,kList)
return bestK
end
## Collects the KIDs that belong to each cluster and writes them out to a file
## Each line in the resulting file is one cluster's KIDs
function clusterMembership(kids,labels)
lenProc = length(kids)
println("lenProc: ", lenProc)
end
## Plot the feature space given two features
## The function parameter indices are column numbers
## From 1 to 9, the number of features
function plotFeatures(features,feat_1_ind::Int64,feat_2_ind::Int64)
x = features[:,feat_1_ind]
y = features[:,feat_2_ind]
clf()
scatter(x,y)
show()
end
function test_for_to()
normFeatsFile = "norm_cross_ref_feats_plus_kid.csv"
kids,feats = getFeatures(normFeatsFile)
feats_1 = int(linspace(1,15,15))
feats_2 = int(linspace(1,15,15))
combArr = hcat(feats_1,feats_2)
plotFeatures(feats,4,10)
end
function clusteringDriver()
println("begun process")
normFeatsFile = "norm_cross_ref_feats_plus_kid.csv"
kids,feats = getFeatures(normFeatsFile)
nkids = size(kids)[1]
n = int(sqrt(nkids))
# n = int(nkids/50)
# n = nkids
println("The overall sample size is: ", n)
inds = randperm(nkids)[1:n]
sample = feats[inds,:]
sample_kids = kids[inds,:]
# println("sample_kids: ", sample_kids)
## Include connectivity constraints to only merge the nearest neighbors
numNeighbors = 30
connectivity_matrix = neighbors.kneighbors_graph(sample,numNeighbors)
## Test to see which k value is the best
bestK = testVariousK(sample,connectivity_matrix)
println("best k: ", bestK)
## Get the labels of the best k
labels = performCluster(sample,bestK,connectivity_matrix)
dist_matrix = dist.pdist(sample,"euclidean")
link_matrix = hier.linkage(dist_matrix)
color_thres = 0.9*maximum(link_matrix[:,2])
R = hier.dendrogram(link_matrix,show_leaf_counts=true,p=50,
truncate_mode="level",distance_sort="ascending",leaf_font_size=13)
xlabel("Number of Members",fontsize=15)
ylabel("Normalized Distance Between Children Nodes",fontsize=15)
title("Dendrogram of Hierarchical Clustering Results",fontsize=17)
createBarGraph(labels)
end
#clusteringDriver()
test_for_to()