Skip to content

Instantly share code, notes, and snippets.

@hoheinzollern
Created August 2, 2012 09:07
Show Gist options
  • Save hoheinzollern/3235662 to your computer and use it in GitHub Desktop.
Save hoheinzollern/3235662 to your computer and use it in GitHub Desktop.
Clustering in Weka
import java.awt.Container;
import java.awt.GridLayout;
import java.util.ArrayList;
import javax.swing.JFrame;
import weka.clusterers.HierarchicalClusterer;
import weka.core.Attribute;
import weka.core.DenseInstance;
import weka.core.EuclideanDistance;
import weka.core.Instances;
import weka.gui.hierarchyvisualizer.HierarchyVisualizer;
public class WekaTest {
static HierarchicalClusterer clusterer;
static Instances data;
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
// Instantiate clusterer
clusterer = new HierarchicalClusterer();
clusterer.setOptions(new String[] {"-L", "COMPLETE"});
clusterer.setDebug(true);
clusterer.setNumClusters(2);
clusterer.setDistanceFunction(new EuclideanDistance());
clusterer.setDistanceIsBranchLength(true);
// Build dataset
ArrayList<Attribute> attributes = new ArrayList<Attribute>();
attributes.add(new Attribute("A"));
attributes.add(new Attribute("B"));
attributes.add(new Attribute("C"));
data = new Instances("Weka test", attributes, 3);
// Add data
data.add(new DenseInstance(1.0, new double[] { 1.0, 0.0, 1.0 }));
data.add(new DenseInstance(1.0, new double[] { 0.5, 0.0, 1.0 }));
data.add(new DenseInstance(1.0, new double[] { 0.0, 1.0, 0.0 }));
data.add(new DenseInstance(1.0, new double[] { 0.0, 1.0, 0.3 }));
// Cluster network
clusterer.buildClusterer(data);
// Print normal
clusterer.setPrintNewick(false);
System.out.println(clusterer.graph());
// Print Newick
clusterer.setPrintNewick(true);
System.out.println(clusterer.graph());
// Let's try to show this clustered data!
JFrame mainFrame = new JFrame("Weka Test");
mainFrame.setSize(600, 400);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container content = mainFrame.getContentPane();
content.setLayout(new GridLayout(1, 1));
HierarchyVisualizer visualizer = new HierarchyVisualizer(clusterer.graph());
content.add(visualizer);
mainFrame.setVisible(true);
}
}
@fichette
Copy link

Is there a way to visualize cluster assignments with kmeans cluster (get the graph)?

@kinjal20
Copy link

From my dataset I want to make 50 clusters then collect all centroids of clusters in .csv file? what changes would be in this code?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment