Example usage for weka.clusterers SimpleKMeans setNumClusters

List of usage examples for weka.clusterers SimpleKMeans setNumClusters

Introduction

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

Prototype

@Override
public void setNumClusters(int n) throws Exception 

Source Link

Document

set the number of clusters to generate.

Usage

From source file:org.montp2.m1decol.ter.clustering.KMeansClustering.java

License:Open Source License

public Clusterer computeClustering(String inPath, String outPath, Properties propertiesCluster)
        throws Exception {
    Instances inputInstances = WekaUtils.loadARFF(inPath);

    EuclideanDistance euclideanDistance = new EuclideanDistance();
    euclideanDistance.setAttributeIndices("first-last");
    euclideanDistance.setDontNormalize(false);
    euclideanDistance.setInvertSelection(false);

    SimpleKMeans kmeans = new SimpleKMeans();
    kmeans.setPreserveInstancesOrder(/*from   w  w w.  jav  a  2  s.  c om*/
            Boolean.valueOf(propertiesCluster.getProperty(ClusterProperties.Kmeans.PERSERVE_INSTANCE)));
    kmeans.setDontReplaceMissingValues(Boolean
            .valueOf(propertiesCluster.getProperty(ClusterProperties.Kmeans.DONT_REPLACE_MISSING_VALUES)));
    kmeans.setDisplayStdDevs(
            Boolean.valueOf(propertiesCluster.getProperty(ClusterProperties.Kmeans.DISPLAY_STD_DEVS)));
    kmeans.setMaxIterations(
            Integer.valueOf(propertiesCluster.getProperty(ClusterProperties.Kmeans.MAX_ITERATIONS)));
    kmeans.setNumClusters(
            Integer.valueOf(propertiesCluster.getProperty(ClusterProperties.Kmeans.NUM_CLUSTERS)));
    kmeans.setSeed(10);
    //kmeans.setSeed(
    //      Integer.valueOf(propertiesCluster.getProperty(ClusterProperties.Kmeans.SEED)));
    kmeans.setDistanceFunction(euclideanDistance);
    kmeans.buildClusterer(inputInstances);

    WekaUtils.saveModel(kmeans, outPath);

    /*
    *
    * Pour obtenir les pourcentages de les clusters
    * ClusterEvaluation eval = new ClusterEvaluation();
    * eval.setClusterer(kmeans);
    * eval.evaluateClusterer(inputInstances);
    * System.out.println(eval.clusterResultsToString());
    *
    * */

    return kmeans;
}

From source file:probcog.clustering.multidim.KMeansClusterer.java

License:Open Source License

public KMeansClusterer(SimpleKMeans clusterer, int dimensions, int k) throws Exception {
    super(clusterer, dimensions);
    clusterer.setNumClusters(k);
}

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);//from   w w  w  .j a  va2s .  c o m
    skm.setSeed(10);
    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:soccer.core.ASimplePractice.java

public void evaluate() throws IOException, Exception {
    Instances data = loader.getInstances();
    SimpleKMeans cluster = new SimpleKMeans();
    cluster.setNumClusters(4);
    cluster.buildClusterer(data);//from   w w w  . ja v a2s .c  o m
    ClusterEvaluation eval = new ClusterEvaluation();
    eval.setClusterer(cluster);
    eval.evaluateClusterer(data);
    System.out.println(eval.clusterResultsToString());
}

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);
    cluster.buildClusterer(data);//  ww w.jav  a  2 s  .  co m
    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  www. jav a 2 s  . c o m
    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");
    }// w ww .  j av 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:tubes2.Main.java

/**
 * @param args the command line arguments
 *//* w  w  w .  jav  a2  s .c  om*/
public static void main(String[] args) throws IOException, Exception {
    // TODO code application logic here
    String filename = "weather";

    //Masih belum mengerti tipe .csv yang dapat dibaca seperti apa
    //CsvToArff convert = new CsvToArff(filename+".csv");

    //LOAD FILE
    BufferedReader datafile = readDataFile("data/" + filename + ".arff");
    Instances data = new Instances(datafile);

    ArrayList<Integer> numericIdx = new ArrayList<Integer>();
    for (int i = 0; i < data.numAttributes(); i++) {
        if (data.attribute(i).isNumeric()) {
            numericIdx.add(i);
        }
    }
    System.out.println();
    System.out.println("\n----SEBELUM NORMALISASI-----");
    System.out.println(data);
    normalizeData(data, numericIdx);
    System.out.println("\n----SETELAH NORMALISASI-----");
    System.out.println(data);

    //END OF LOAD FILE

    SimpleKMeans simpleK = new SimpleKMeans();
    simpleK.setNumClusters(4);
    Clusterer[] clusterers = { simpleK, new myKMeans(4), new myAgnes(4) };

    boolean first = true;
    for (Clusterer clusterer : clusterers) {
        try {
            clusterer.buildClusterer(data);
            System.out.println("\n\n----HASIL CLUSTERING-----");
            System.out.println(clusterer);
        } catch (Exception ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

From source file:wekimini.InputGenerator.java

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

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

    clusters = km.getClusterCentroids();

    addKmClustersToTraining();
}