-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathagnes.rb
200 lines (187 loc) · 5.26 KB
/
agnes.rb
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# *****************************************************************
# NOTE: Please place the training and test CSV files in the same folder as the file
# NOTE: ruby agnes.rb to run the program
# *****************************************************************
require_relative './silhouette_coefficient'
@distance_matrix = []
@clusters = []
# Internal: Initialize and populate distances between each point in the dataset
# This matrix is a must, else the program will choke trying to calculate
# the distance measure each and everytime
#
# @distance_matrix instance variable is being populated and is used across the
# scope of this algorithm
#
# Return: nil
#
# Examples
# populate_distance_map
# => nil
def populate_distance_map
@distance_matrix = @clusters.length.times.map { |x| [] }
@clusters.each do |x|
@clusters.each do |y|
distance = euclidian_measure(x.first.last, y.first.last)
@distance_matrix[x.first.first][y.first.first] = distance
@distance_matrix[y.first.first][x.first.first] = distance
end
end
end
# Internal: Merge two clusters at indices index1 and index2 to form a new
# combined cluster at index1
#
# Return: nil
#
# Examples
# agnes
# => nil
def agnes
@clusters = (0..data.length-1).map { |x| [data[x]]}
populate_distance_map
cluster_iteration = data.length
while cluster_iteration > 20 # Dendogram cutoff after 20 clusters are formed
distance = Float::INFINITY
min = Float::INFINITY
mergable_cluster_index1 = nil
mergable_cluster_index2 = nil
@clusters.each_with_index do |x, i|
@clusters.each_with_index do |y, j|
unless i == j
distance = single_link_distance(x, y)
if distance < min
min = distance
mergable_cluster_index1 = i
mergable_cluster_index2 = j
end
end
end
end
@clusters = merge_clusters(mergable_cluster_index1, mergable_cluster_index2)
cluster_iteration -= 1
end
@clusters
end
# Internal: Merge two clusters at indices index1 and index2 to form a new
# combined cluster at index1
#
# Return: nil
#
# Examples
# merge_clusters(2, 10)
# => nil
def merge_clusters(index1, index2)
@clusters.each_with_index do |x, index|
if index == index2
@clusters[index2].each do |c|
@clusters[index1].push(c)
end
@clusters[index2] = nil
end
end
@clusters = @clusters.select{ |x| x != nil }
end
# Internal: Distance between nearest points across two clusters c1 c2
#
# Return: Float
#
# Examples
# single_link_distance([[1.0, 2.0, 3.0, 4.0]], [[0.0, 1.0, 2.0, 3.0]])
# => 2
def single_link_distance(c1, c2)
min = Float::INFINITY
distance = 0
c1.each do |x|
c2.each do |y|
distance = @distance_matrix[x.first][y.first]
min = distance if distance < min
end
end
min
end
# Internal: Manhattan distance calculator
#
# Return: Float
#
# Examples
# manhattan_measure([1.0, 2.0, 3.0, 4.0], [0.0, 1.0, 2.0, 3.0])
# => 4
def manhattan_measure(p1, p2)
size = [p1.size, p2.size].max
(0..size-1).inject(0.0) { |sum, i| sum + ( ( (p1[i] || 0) - (p2[i] || 0) ).abs) }
end
# Internal: Euclidian distance calculator
#
# Return: Float
#
# Examples
# euclidian_measure([1.0, 2.0, 3.0, 4.0], [0.0, 1.0, 2.0, 3.0])
# => 2
def euclidian_measure(p1, p2)
size = [p1.size, p2.size].max
sum = (0..size-1).inject(0.0) { |sum, i| sum + ( ( (p1[i] || 0) - (p2[i] || 0) ) ** 2 ) }
sum ** 0.5
end
# Internal: Memoization method to prevent redundant data preprocess
#
# Return: Array
#
# Examples
# data
def data
# This is a common memoization technique used in Ruby
@data ||= preprocess_data
end
# Internal: Read data from an input CSV file
#
# Return: Array
#
# Examples
# preprocess_data("votes-train.csv")
# => [[1, [features]], [2, [features]], [3, [features]]]
def preprocess_data
classified_data = []
index = 0
File.open("iris.data", "r") do |f|
f.each_line do |line|
next if line.chomp.empty?
partition = line.partition(",")
classified_data << [index, partition.last.chomp.split(",").map(&:to_f)]
index += 1
end
end
classified_data
end
def clusterer
@clusters = []
agnes.each_with_index do |x, index|
print "CLUSTER #{index}: "
p x.length
@clusters[index] = x.map { |k| k.last }
end
@clusters.each_with_index do |x, index|
print "CLUSTER #{index+1}: "
print "#{x.length}(size) "
p "Silhouette Coefficient: #{SilhouetteCoefficient.silhouette_coefficient(x, @clusters)}"
end
end
#OUTPUT
# CLUSTER 0: 2308 Silhouette: 0.637483458953335
# CLUSTER 1: 1 Silhouette: 1.0
# CLUSTER 2: 1 Silhouette: 1.0
# CLUSTER 3: 1 Silhouette: 1.0
# CLUSTER 4: 1 Silhouette: 1.0
# CLUSTER 5: 4 Silhouette: 0.7843654469225305
# CLUSTER 6: 1 Silhouette: 1.0
# CLUSTER 7: 3 Silhouette: 0.7454163850685841
# CLUSTER 8: 1 Silhouette: 1.0
# CLUSTER 9: 1 Silhouette: 1.0
# CLUSTER 10: 1 Silhouette: 1.0
# CLUSTER 11: 1 Silhouette: 1.0
# CLUSTER 12: 1 Silhouette: 1.0
# CLUSTER 13: 1 Silhouette: 1.0
# CLUSTER 14: 2 Silhouette: 0.944062951161708
# CLUSTER 15: 2 Silhouette: 0.8494818155755182
# CLUSTER 16: 1 Silhouette: 1.0
# CLUSTER 17: 1 Silhouette: 1.0
# CLUSTER 18: 1 Silhouette: 1.0
# CLUSTER 19: 1 Silhouette: 1.0