weka.clusterers.EM Java Examples

The following examples show how to use weka.clusterers.EM. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: WekaClassesToClusterTest.java    From Java-Data-Science-Cookbook with MIT License 6 votes vote down vote up
public void generateClassToCluster(){
	Remove filter = new Remove();
	filter.setAttributeIndices("" + (weather.classIndex() + 1));
	try {
		filter.setInputFormat(weather);
		Instances dataClusterer = Filter.useFilter(weather, filter);
		clusterer = new EM();
		clusterer.buildClusterer(dataClusterer);
		ClusterEvaluation eval = new ClusterEvaluation();
		eval.setClusterer(clusterer);
		eval.evaluateClusterer(weather);

		System.out.println(eval.clusterResultsToString());
	} catch (Exception e) {
	}
}
 
Example #2
Source File: TMAPoints.java    From orbit-image-analysis with GNU General Public License v3.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
private HashMap<Integer, Integer> sortAndpPrintCluster(EM clusterer) {
    List<double[]> vals = new ArrayList<double[]>(clusterer.getNumClusters());
    for (int c = 0; c < clusterer.getNumClusters(); c++) {
        vals.add(new double[]{clusterer.getClusterModelsNumericAtts()[c][0][0],   // mean
                clusterer.getClusterModelsNumericAtts()[c][0][1],  // var
                clusterer.getClusterModelsNumericAtts()[c][0][2], // prior
                clusterer.getClusterPriors()[c], // priors normalized
                c // cluster number
        });
    }
    Collections.sort(vals, new Comparator() {
        public int compare(Object o1, Object o2) {
            double[] d1 = (double[]) o1;
            double[] d2 = (double[]) o2;
            return (int) Math.signum(d1[0] - d2[0]);
        }
    });
    for (int c = 0; c < vals.size(); c++) {
        logger.trace("Cluster " + c + ":" + vals.get(c)[0] + " Var:" + vals.get(c)[1] + " Z:" + vals.get(c)[2] + " Prior:" + vals.get(c)[3] + " Cluster:" + vals.get(c)[4]);
    }
    return mergeClusters(vals);
}
 
Example #3
Source File: TMAPoints.java    From orbit-image-analysis with GNU General Public License v3.0 6 votes vote down vote up
private int guessNumClusters(EM clusterer, Instances instances, int start, int end) throws Exception {
    ClusterEvaluation eval = new ClusterEvaluation();
    int bestNum = start;
    double best = Double.POSITIVE_INFINITY;
    double bic;
    for (int c = start; c <= end; c++) {
        clusterer.setNumClusters(c);
        clusterer.buildClusterer(instances);
        eval.setClusterer(clusterer);
        eval.evaluateClusterer(instances);
        bic = bic(eval.getLogLikelihood(), c, instances.numInstances());
        logger.trace("numCluster " + c + " -> BIC: " + bic);
        if (bic < best) {
            best = bic;
            bestNum = c;
            logger.trace("bestNum: " + bestNum);
        }
    }
    return bestNum;
}
 
Example #4
Source File: Cluster.java    From chuidiang-ejemplos with GNU Lesser General Public License v3.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    Instances data = GenerateTestVessels.getData();
    data.setClassIndex(-1); // No class index.

    Remove rm = new Remove();
    rm.setAttributeIndices("1");
    rm.setInputFormat(data);
    data = Filter.useFilter(data,rm);
    System.out.println(data);


    EM cw = new EM();

    cw.buildClusterer(data);
    System.out.println(cw);

    System.out.println(cw.clusterInstance(data.firstInstance()));
}
 
Example #5
Source File: EMClusterer.java    From mzmine3 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public ClusteringResult performClustering(Instances dataset, ParameterSet parameters) {

  List<Integer> clusters = new ArrayList<Integer>();
  String[] options = new String[2];
  EM clusterer = new EM();

  int numberOfIterations =
      parameters.getParameter(EMClustererParameters.numberOfIterations).getValue();
  options[0] = "-I";
  options[1] = String.valueOf(numberOfIterations);

  try {
    clusterer.setOptions(options);
    clusterer.buildClusterer(dataset);
    Enumeration<?> e = dataset.enumerateInstances();
    while (e.hasMoreElements()) {
      clusters.add(clusterer.clusterInstance((Instance) e.nextElement()));
    }
    ClusteringResult result = new ClusteringResult(clusters, null, clusterer.numberOfClusters(),
        parameters.getParameter(EMClustererParameters.visualization).getValue());
    return result;

  } catch (Exception ex) {
    logger.log(Level.SEVERE, null, ex);
    return null;
  }
}
 
Example #6
Source File: EMClusterer.java    From mzmine2 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public ClusteringResult performClustering(Instances dataset, ParameterSet parameters) {

  List<Integer> clusters = new ArrayList<Integer>();
  String[] options = new String[2];
  EM clusterer = new EM();

  int numberOfIterations =
      parameters.getParameter(EMClustererParameters.numberOfIterations).getValue();
  options[0] = "-I";
  options[1] = String.valueOf(numberOfIterations);

  try {
    clusterer.setOptions(options);
    clusterer.buildClusterer(dataset);
    Enumeration<?> e = dataset.enumerateInstances();
    while (e.hasMoreElements()) {
      clusters.add(clusterer.clusterInstance((Instance) e.nextElement()));
    }
    ClusteringResult result = new ClusteringResult(clusters, null, clusterer.numberOfClusters(),
        parameters.getParameter(EMClustererParameters.visualization).getValue());
    return result;

  } catch (Exception ex) {
    logger.log(Level.SEVERE, null, ex);
    return null;
  }
}
 
Example #7
Source File: ClusteringTask.java    From Machine-Learning-in-Java with MIT License 4 votes vote down vote up
public static void main(String args[]) throws Exception{
	
	//load data
	Instances data = new Instances(new BufferedReader(new FileReader("data/bank-data.arff")));
	
	// new instance of clusterer
	EM model = new EM();
	// build the clusterer
	model.buildClusterer(data);
	System.out.println(model);
	
	double logLikelihood = ClusterEvaluation.crossValidateModel(model, data, 10, new Random(1));
	System.out.println(logLikelihood);


}