Example usage for weka.clusterers SimpleKMeans numberOfClusters

List of usage examples for weka.clusterers SimpleKMeans numberOfClusters

Introduction

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

Prototype

@Override
public int numberOfClusters() throws Exception 

Source Link

Document

Returns the number of clusters.

Usage

From source file:br.ufrn.ia.core.clustering.EMIaProject.java

License:Open Source License

private void EM_Init(Instances inst) throws Exception {
    int i, j, k;/*from ww w.  j  a v a 2  s .  c om*/

    // run k means 10 times and choose best solution
    SimpleKMeans bestK = null;
    double bestSqE = Double.MAX_VALUE;
    for (i = 0; i < 10; i++) {
        SimpleKMeans sk = new SimpleKMeans();
        sk.setSeed(m_rr.nextInt());
        sk.setNumClusters(m_num_clusters);
        sk.setDisplayStdDevs(true);
        sk.buildClusterer(inst);
        if (sk.getSquaredError() < bestSqE) {
            bestSqE = sk.getSquaredError();
            bestK = sk;
        }
    }

    // initialize with best k-means solution
    m_num_clusters = bestK.numberOfClusters();
    m_weights = new double[inst.numInstances()][m_num_clusters];
    m_model = new DiscreteEstimator[m_num_clusters][m_num_attribs];
    m_modelNormal = new double[m_num_clusters][m_num_attribs][3];
    m_priors = new double[m_num_clusters];
    Instances centers = bestK.getClusterCentroids();
    Instances stdD = bestK.getClusterStandardDevs();
    double[][][] nominalCounts = bestK.getClusterNominalCounts();
    double[] clusterSizes = bestK.getClusterSizes();

    for (i = 0; i < m_num_clusters; i++) {
        Instance center = centers.instance(i);
        for (j = 0; j < m_num_attribs; j++) {
            if (inst.attribute(j).isNominal()) {
                m_model[i][j] = new DiscreteEstimator(m_theInstances.attribute(j).numValues(), true);
                for (k = 0; k < inst.attribute(j).numValues(); k++) {
                    m_model[i][j].addValue(k, nominalCounts[i][j][k]);
                }
            } else {
                double minStdD = (m_minStdDevPerAtt != null) ? m_minStdDevPerAtt[j] : m_minStdDev;
                double mean = (center.isMissing(j)) ? inst.meanOrMode(j) : center.value(j);
                m_modelNormal[i][j][0] = mean;
                double stdv = (stdD.instance(i).isMissing(j))
                        ? ((m_maxValues[j] - m_minValues[j]) / (2 * m_num_clusters))
                        : stdD.instance(i).value(j);
                if (stdv < minStdD) {
                    stdv = inst.attributeStats(j).numericStats.stdDev;
                    if (Double.isInfinite(stdv)) {
                        stdv = minStdD;
                    }
                    if (stdv < minStdD) {
                        stdv = minStdD;
                    }
                }
                if (stdv <= 0) {
                    stdv = m_minStdDev;
                }

                m_modelNormal[i][j][1] = stdv;
                m_modelNormal[i][j][2] = 1.0;
            }
        }
    }

    for (j = 0; j < m_num_clusters; j++) {
        // m_priors[j] += 1.0;
        m_priors[j] = clusterSizes[j];
    }
    Utils.normalize(m_priors);
}

From source file:detplagiasi.KMeansClustering.java

KMeansClustering() {
    addd = Container.getAddress();
    try {// w w w  .  ja v a 2s . c  o  m
        ClusterEvaluation eval;
        Instances data;
        String[] options;
        SimpleKMeans cl;

        File he = getArffFile();
        data = new Instances(new BufferedReader(new FileReader(he)));
        System.out.println("-----KMeans Clustering-----");
        // normal
        try (BufferedWriter out = new BufferedWriter(new FileWriter(addd + "\\output.txt", true))) {
            out.write("\r\n--> normal\r\n");
            options = new String[2];
            options[0] = "-t";
            options[1] = he.getAbsolutePath();
            out.write("\r\n" + ClusterEvaluation.evaluateClusterer(new SimpleKMeans(), options) + "\r\n");
            out.write("\r\n");

            // manual call
            out.write("\n--> manual\r\n");
            cl = new SimpleKMeans();
            cl.setNumClusters(4);
            out.write("\r\n");
            cl.buildClusterer(data);
            getDataUji();
            System.out.println("jumlah kluster = " + cl.numberOfClusters());
            System.out.println("kluster = " + cl.clusterInstance(dataUji.instance(0)));
            noClusterUji = cl.clusterInstance(dataUji.instance(0));
            totalCluster = cl.numberOfClusters();
            for (int b = 0; b < dataTraining.numInstances(); b++) {
                System.out.print("file " + td.fileName[b] + " termasuk cluster ke ");
                System.out.println(cl.clusterInstance(dataTraining.instance(b)));
                array1[b] = td.fileName[b];
                array2[b] = cl.clusterInstance(dataTraining.instance(b));
                //simpan nilai instance ke dalam sebuah array int buat dikirim ke detplaggui
            }

            out.write("\r\n");

            eval = new ClusterEvaluation();
            eval.setClusterer(cl);
            eval.evaluateClusterer(new Instances(data));
            out.write("\r\n\n# of clusters: " + eval.getNumClusters());

        } catch (Exception e) {
            System.err.println(e.getMessage());
            System.out.println("error2 kmeans cluster");
        }

    } catch (IOException ex) {
        Logger.getLogger(Clustering.class.getName()).log(Level.SEVERE, null, ex);
        System.out.println("errorrrr null kmeans");
    }
}

From source file:net.sf.mzmine.modules.peaklistmethods.dataanalysis.clustering.simplekmeans.SimpleKMeansClusterer.java

License:Open Source License

@Override
public ClusteringResult performClustering(Instances dataset, ParameterSet parameters) {

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

    int numberOfGroups = parameters.getParameter(SimpleKMeansClustererParameters.numberOfGroups).getValue();
    options[0] = "-N";
    options[1] = String.valueOf(numberOfGroups);

    try {/*from  www .  j  a v  a2  s  .c om*/
        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;
    }
}