Weka Clustering Instance - Java Machine Learning AI

Java examples for Machine Learning AI:weka

Description

Weka Clustering Instance

Demo Code



import java.io.BufferedReader;
import java.io.FileReader;
import weka.clusterers.EM;
import weka.core.Instances;
import weka.core.Utils;
import weka.core.converters.ConverterUtils.DataSource;

public class WekaClusteringInstance {
    public static void main(String[] args) throws Exception {
        DataSource source = new DataSource(
                "bank-train.arff");
        Instances instancesTrain = source.getDataSet();

        Instances instancesTest = new Instances(new BufferedReader(
                new FileReader(
                        "bank-test.arff")));
        instancesTest.setClassIndex(instancesTest.numAttributes() - 1);

        String[] options = Utils.splitOptions("-I 100");
        EM cluster = new EM();
        cluster.setOptions(options);//w w w  .  ja  v  a 2 s  . co m
        cluster.buildClusterer(instancesTrain);

            int clsRes = cluster.clusterInstance(instancesTest.instance(i));

            double[] allLabel = cluster
                    .distributionForInstance(instancesTest.instance(i));

            for (double d : allLabel) {
                System.out.print(d + "---");
            }
            System.out.println();
        }

    }
}

Related Tutorials