Example usage for weka.clusterers SimpleKMeans buildClusterer

List of usage examples for weka.clusterers SimpleKMeans buildClusterer

Introduction

In this page you can find the example usage for weka.clusterers SimpleKMeans buildClusterer.

Prototype

@Override
public void buildClusterer(Instances data) throws Exception 

Source Link

Document

Generates a clusterer.

Usage

From source file:qoala.arff.java

public void SimpleKmeans(int numberOfCLuster) throws Exception {

    Instances train = new Instances(dataSet);

    SimpleKMeans skm = new SimpleKMeans();
    skm.setPreserveInstancesOrder(true);
    skm.setNumClusters(numberOfCLuster);
    skm.buildClusterer(train);
    skm.setSeed(10);/*  ww  w .  j  ava2  s.  c o  m*/
    int[] ClusterSize = skm.getClusterSizes();

    ClusterEvaluation eval = new ClusterEvaluation();
    eval.setClusterer(skm);
    eval.evaluateClusterer(train);

    System.out.println("Cluster Evaluation:" + eval.clusterResultsToString());

    int[] assignments = skm.getAssignments();

    System.out.println("# - cluster - distribution");

    for (int j = 0; j < skm.getNumClusters(); j++) {
        int i = 0;
        for (int clusterNum : assignments) {

            if (clusterNum == j)

                System.out.println("Instance " + i + " -> Cluster number: " + clusterNum);

            i++;
        }
    }
}

From source file:rdfsystem.data.DataMining.java

public static String cluster(RdfManager manager) throws Exception {
    Instances ins = transformData(manager, false);
    SimpleKMeans cls = new SimpleKMeans();
    String[] options = "-N 5".split(" ");
    cls.setOptions(options);//from  w ww  .  j a  v a 2s  . co m
    cls.buildClusterer(ins);
    ClusterEvaluation eval = new ClusterEvaluation();
    eval.setClusterer(cls);
    eval.evaluateClusterer(ins);
    return eval.clusterResultsToString();
}

From source file:soccer.core.ASimplePractice.java

public void evaluate() throws IOException, Exception {
    Instances data = loader.getInstances();
    SimpleKMeans cluster = new SimpleKMeans();
    cluster.setNumClusters(4);//from  w  ww.  j a v a  2  s. com
    cluster.buildClusterer(data);
    ClusterEvaluation eval = new ClusterEvaluation();
    eval.setClusterer(cluster);
    eval.evaluateClusterer(data);
    System.out.println(eval.clusterResultsToString());
}

From source file:soccer.core.classifiers.BookKeeperConsistencyClassifier.java

public static void main(String[] args) throws Exception {
    BookKeeperConsistency bkc = new BookKeeperConsistency();
    Instances data = bkc.getInstances();

    RemoveWithValues rwv = new RemoveWithValues();
    rwv.setOptions(new String[] { "-C", "4", "-S", "6", "-V" });
    rwv.setInputFormat(data);//w w  w  .  j a  v a 2  s . c om
    data = Filter.useFilter(data, rwv);
    RemoveWithValues rwv1 = new RemoveWithValues();
    rwv1.setOptions(new String[] { "-C", "6", "-S", "6", "-V" });
    rwv1.setInputFormat(data);
    data = Filter.useFilter(data, rwv1);

    //        Normalize nm = new Normalize();
    //        nm.setOptions(new String[]{
    //            "-S", "100"
    //        });
    //        nm.setInputFormat(data);
    //        data = Filter.useFilter(data, nm);

    Remove rm = new Remove();
    rm.setOptions(new String[] { "-R", "2-last" });
    rm.setInputFormat(data);
    Instances newData = Filter.useFilter(data, rm);

    SimpleKMeans cluster = new SimpleKMeans();
    cluster.setOptions(new String[] { "-N", "2", "-A", "weka.core.ManhattanDistance" });

    cluster.buildClusterer(newData);
    ClusterEvaluation eval = new ClusterEvaluation();
    eval.setClusterer(cluster);
    eval.evaluateClusterer(newData);
    System.out.println(eval.clusterResultsToString());
    //        for (int i = 0; i < newData.size(); i++) {
    //            Instance instance = newData.get(i);
    //            if (cluster.clusterInstance(instance) == 0) {
    //                System.out.println(data.get(i).toString());
    //            }
    //        }
}

From source file:soccer.core.models.PlayerModel.java

public static void main(String[] args) throws Exception {
    PlayerModel pm = new PlayerModel();
    Instances data = pm.buildInstance();
    SimpleKMeans cluster = new SimpleKMeans();
    cluster.setNumClusters(4);//w w  w .j ava 2s .c  o m
    cluster.buildClusterer(data);
    ClusterEvaluation eval = new ClusterEvaluation();
    eval.setClusterer(cluster);
    eval.evaluateClusterer(data);
    System.out.println(eval.clusterResultsToString());
}

From source file:swm.project.mappings.UserToUserCluster.java

private void clusterUserHistoryWithKmeans() throws FileNotFoundException, IOException, Exception {
    Reader reader;/*from w  ww .j  av a  2 s.com*/
    userToUserClusterHistory = new HashMap<>();
    userClustersToUsersHistory = new HashMap<>();
    reader = new FileReader(MappingConstants.USER_MOVIE_CLUSTERS);
    Instances instanceValues = new Instances(reader);
    SimpleKMeans kmeans = new SimpleKMeans();

    kmeans.setNumClusters(20);
    kmeans.setPreserveInstancesOrder(true);
    kmeans.setDistanceFunction(new EuclideanDistance());
    kmeans.buildClusterer(instanceValues);

    int[] assignments = kmeans.getAssignments();
    int userid = 0;
    for (int clusterNo : assignments) {
        int user = (int) instanceValues.get(userid).value(0);
        userToUserClusterHistory.put(user, clusterNo);
        ArrayList<Integer> users = new ArrayList<>();
        if (userClustersToUsersHistory.containsKey(clusterNo)) {
            users = userClustersToUsersHistory.get(clusterNo);
            users.add(user);
        } else {
            users.add(user);
            userClustersToUsersHistory.put(clusterNo, users);
        }
        userid++;

    }
}

From source file:tr.gov.ulakbim.jDenetX.experiments.wrappers.EvalActiveBoostingID.java

License:Open Source License

public Instances clusteredInstances(Instances data) {
    if (data == null) {
        throw new NullPointerException("Data is null at clusteredInstances method");
    }/*from w ww .  j  a  v  a 2  s.c  o m*/
    Instances sampled_data = data;
    for (int i = 0; i < sampled_data.numInstances(); i++) {
        sampled_data.remove(i);
    }

    SimpleKMeans sKmeans = new SimpleKMeans();
    data.setClassIndex(data.numAttributes() - 1);
    Remove filter = new Remove();
    filter.setAttributeIndices("" + (data.classIndex() + 1));
    List assignments = new ArrayList();

    try {
        filter.setInputFormat(data);
        Instances dataClusterer = Filter.useFilter(data, filter);
        String[] options = new String[3];
        options[0] = "-I"; // max. iterations
        options[1] = "500";
        options[2] = "-O";
        sKmeans.setNumClusters(data.numClasses());
        sKmeans.setOptions(options);
        sKmeans.buildClusterer(dataClusterer);
        System.out.println("Kmeans\n:" + sKmeans);
        System.out.println(Arrays.toString(sKmeans.getAssignments()));
        assignments = Arrays.asList(sKmeans.getAssignments());
    } catch (Exception e) {
        e.printStackTrace();
    }
    System.out.println("Assignments\n: " + assignments);
    ClusterEvaluation eval = new ClusterEvaluation();
    eval.setClusterer(sKmeans);
    try {
        eval.evaluateClusterer(data);
    } catch (Exception e) {
        e.printStackTrace();
    }
    int classesToClustersMap[] = eval.getClassesToClusters();
    for (int i = 0; i < classesToClustersMap.length; i++) {
        if (assignments.get(i).equals(((Integer) classesToClustersMap[(int) data.get(i).classValue()]))) {
            ((Instances) sampled_data).add(data.get(i));
        }
    }
    return ((Instances) sampled_data);
}

From source file:wekimini.InputGenerator.java

public void selectKmClusters(int numClusters) throws Exception {
    buildDataset();/*from  w w  w  .java 2s . c  om*/

    SimpleKMeans km = new SimpleKMeans();
    km.setNumClusters(numClusters);
    km.buildClusterer(dataset);

    clusters = km.getClusterCentroids();

    addKmClustersToTraining();
}