forked from CSC3100/Temporal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.java
52 lines (39 loc) · 1.24 KB
/
Main.java
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
import weka.clusterers.*;
import weka.core.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.LinkedList;
public class Main {
public static void main(String args[]) throws Exception {
// Create a linked list of points
LinkedList<Point> points = new LinkedList<Point>();
points.add(new Point(50, 10));
points.add(new Point(10, 20));
points.add(new Point(20, 20));
points.add(new Point(25, 10));
// Create attributes
ArrayList<Attribute> attributes = new ArrayList<Attribute>();
attributes.add(new Attribute("x"));
attributes.add(new Attribute("y"));
// Create instances
Instances data = new Instances("Example", attributes, points.size());
for (Point point : points) {
double[] values = new double[2];
values[0] = point.getX();
values[1] = point.getY();
data.add(new DenseInstance(1.0, values));
}
// Print the instances
System.out.println(data);
// models
SimpleKMeans model1 = new SimpleKMeans();
model1.setNumClusters(2);
model1.buildClusterer(data);
System.out.println(model1);
// show results
for (int i = 0; i < data.numInstances(); i++) {
int clsLabel = model1.clusterInstance(data.instance(i));
System.out.printf("Instance %d -> Cluster %d \n", i, clsLabel);
}
}
}