-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtrial-kmeans.rb
More file actions
35 lines (28 loc) · 798 Bytes
/
trial-kmeans.rb
File metadata and controls
35 lines (28 loc) · 798 Bytes
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
# Weka scripting from jruby
# Written by Peter Lane, 2009.
# Run with: java -jar jruby.jar trial-kmeans.rb FILENAME.arff
# NOTE: this requires weka.jar to be in the execution folder. A symbolic link
# will work fine.
#
require "java"
require "weka"
java_import "java.io.FileReader"
java_import "weka.clusterers.SimpleKMeans"
java_import "weka.core.Instances"
# load data file
file = FileReader.new ARGV[0]
data = Instances.new file
# create the model
kmeans = SimpleKMeans.new
kmeans.buildClusterer data
# print out the built model
print kmeans
# Display the cluster for each instance
data.numInstances.times do |i|
cluster = "UNKNOWN"
begin
cluster = kmeans.clusterInstance(data.instance(i))
rescue java.lang.Exception
end
puts "#{data.instance(i)},#{cluster}"
end