Example usage for weka.clusterers Clusterer buildClusterer

List of usage examples for weka.clusterers Clusterer buildClusterer

Introduction

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

Prototype

void buildClusterer(Instances data) throws Exception;

Source Link

Document

Generates a clusterer.

Usage

From source file:guineu.modules.dataanalysis.clustering.hierarchical.HierarClusterer.java

License:Open Source License

public String getHierarchicalCluster(Instances dataset) {
    Clusterer clusterer = new HierarchicalClusterer();
    String[] options = new String[5];
    LinkType link = parameters.getParameter(HierarClustererParameters.linkType).getValue();
    DistanceType distanceType = parameters.getParameter(HierarClustererParameters.distanceType).getValue();
    options[0] = "-L";
    options[1] = link.name();//  w w w. ja va 2 s  . c o  m
    options[2] = "-A";
    switch (distanceType) {
    case EUCLIDIAN:
        options[3] = "weka.core.EuclideanDistance";
        break;
    case CHEBYSHEV:
        options[3] = "weka.core.ChebyshevDistance";
        break;
    case MANHATTAN:
        options[3] = "weka.core.ManhattanDistance";
        break;
    case MINKOWSKI:
        options[3] = "weka.core.MinkowskiDistance";
        break;
    }

    options[4] = "-P";
    try {
        ((HierarchicalClusterer) clusterer).setOptions(options);
        clusterer.buildClusterer(dataset);
        return ((HierarchicalClusterer) clusterer).graph();
    } catch (Exception ex) {
        Logger.getLogger(HierarClusterer.class.getName()).log(Level.SEVERE, null, ex);
        return null;
    }
}

From source file:guineu.modules.dataanalysis.clustering.simplekmeans.SimpleKMeansClusterer.java

License:Open Source License

public List<Integer> getClusterGroups(Instances dataset) {
    List<Integer> clusters = new ArrayList<Integer>();
    String[] options = new String[2];
    Clusterer clusterer = new SimpleKMeans();

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

    try {/*from  w  w w.j a  va2  s.  com*/
        ((SimpleKMeans) clusterer).setOptions(options);
        clusterer.buildClusterer(dataset);
        Enumeration e = dataset.enumerateInstances();
        while (e.hasMoreElements()) {
            clusters.add(clusterer.clusterInstance((Instance) e.nextElement()));
        }
        this.numberOfGroups = clusterer.numberOfClusters();
    } catch (Exception ex) {
        Logger.getLogger(SimpleKMeansClusterer.class.getName()).log(Level.SEVERE, null, ex);
    }
    return clusters;
}

From source file:intensityclustering.IntensityClustering.java

/**
 * Draws the 2D Histogram Plot in the IntensityClustering. X-Axsis is
 * intensity value of chanel 2 image (where the stained nuclei are). Y-axis
 * are relative frequencies of present nuclei.
 *
 * @param tss The TMAspots whose nuclei are considered (both gold-standard
 * and estimated nuclei).// ww w. j a  va2  s  .  c om
 * @param doAlsoClustering If true, the TMApoints are also clustered
 * according to the histogram.
 */
void drawNucleiIntensities2D(List<TMAspot> tss, boolean doAlsoClustering) {
    // draw the plot
    Plot2DPanel plot;
    if (((java.awt.BorderLayout) (jPanel9.getLayout()))
            .getLayoutComponent(java.awt.BorderLayout.CENTER) != null) {
        plot = (Plot2DPanel) ((java.awt.BorderLayout) (jPanel9.getLayout()))
                .getLayoutComponent(java.awt.BorderLayout.CENTER);
        plot.removeAllPlots();
        plot.removeAllPlotables();
    } else {
        plot = new Plot2DPanel(PlotPanel.SOUTH);
        plot.setAxisLabels("Intensity", "Frequency");
        plot.plotCanvas.setBackground(jPanel9.getBackground());
        plot.plotLegend.setBackground(jPanel9.getBackground());
        plot.plotToolBar.setBackground(plot.plotCanvas.getBackground());
    }
    if (((java.awt.BorderLayout) (jPanel9.getLayout()))
            .getLayoutComponent(java.awt.BorderLayout.CENTER) == null) {
        jPanel9.add(plot, java.awt.BorderLayout.CENTER);
        jPanel15.setBackground(plot.plotCanvas.getBackground());
        jPanel15.setVisible(true);
        validate();
        pack();
    }

    if (tss.size() > 0) {
        try {
            this.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));

            List<Integer> intensities = new ArrayList<>();
            int intensity;
            int min = Integer.parseInt(jTextField1.getText());
            int max = Integer.parseInt(jTextField16.getText());
            for (TMAspot ts : tss) {
                //TODO: GET THE CHANNEL 2 Image
                //BufferedImage img = ts.getBufferedImage(TMAspot.SHOW_CHANNEL2_IMAGE, false);
                BufferedImage img = ts.getBufferedImage(false);
                // img can be null if color deconvolution has not been performed, yet.
                if (img != null) {
                    List<TMApoint> tps = ts.getPoints();
                    for (TMALabel tp : tps) {
                        intensity = TMAspot.getAverageColorAtPoint(img, tp.x, tp.y, ts.getParam_r(), false)
                                .getRed();
                        if (intensity >= min && intensity <= max) {
                            intensities.add(intensity);
                        }
                    }
                }
            }

            double[] intensities_array = new double[intensities.size()];

            for (int i = 0; i < intensities.size(); i++) {
                intensities_array[i] = intensities.get(i);
            }

            int nbins = jSlider7.getValue();
            if (intensities_array.length > 0) {
                plot.addHistogramPlot("TMA points", intensities_array, 0, 256, nbins);
            } //else {
              //  JOptionPane.showMessageDialog(this, "No TMA points have been found.", "No TMA points found.", JOptionPane.WARNING_MESSAGE);
              //}

            //// Cluster Points according to histograms
            if (doAlsoClustering) {
                // Find Clusters
                int n = getParam_nClusters();

                // Create ARFF Data
                FastVector atts;
                Instances data;
                int i;

                // 1. create arff data format
                atts = new FastVector(1);
                for (i = 0; i < 1; i++) {
                    atts.addElement(new Attribute(Integer.toString(i)));
                }

                // 2. create Instances object
                data = new Instances("TMA points", atts, tmarker.getNumberNuclei(tss));

                // 3. fill with data
                for (i = 0; i < intensities_array.length; i++) {
                    // add the instance
                    Instance inst = new Instance(1.0, new double[] { intensities_array[i] });
                    inst.setDataset(data);
                    data.add(inst);
                }

                // 4. set data class index (last attribute is the class)
                //data.setClassIndex(data.numAttributes() - 1); // not for weka 3.5.X
                if (tmarker.DEBUG > 4) {
                    java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.INFO,
                            data.toString());
                }

                Clusterer clusterer = getClusterer();
                String[] options = getClustererOptions();

                if (tmarker.DEBUG > 3) {
                    if (options.length > 0) {
                        String info = "Clusterer should have options:\n";
                        for (String o : options) {
                            info += o + " ";
                        }
                        info += "\n";
                        java.util.logging.Logger.getLogger(getClass().getName())
                                .log(java.util.logging.Level.INFO, info);
                    }
                }

                clusterer.setOptions(options); // set the clusterer options
                clusterer.buildClusterer(data); // build the clusterer

                // order the clusters according to the brightness
                // The most bright cluster should be 0, then 1, then 2,...
                ArrayList<ArrayList<Double>> values = new ArrayList<>();
                for (i = 0; i < n; i++) {
                    values.add(new ArrayList<Double>());
                }
                int z;
                double value;
                for (i = 0; i < data.numInstances(); i++) {
                    z = clusterer.clusterInstance(data.instance(i));
                    value = data.instance(i).value(0);
                    values.get(z).add(value);
                }
                double[] means = new double[n];
                double[] stds = new double[n];
                for (i = 0; i < n; i++) {
                    means[i] = Misc.mean(values.get(i).toArray(new Double[values.get(i).size()]));
                    stds[i] = Misc.std(values.get(i).toArray(new Double[values.get(i).size()]));
                }
                int[] ordering = Misc.orderArray(means, true);

                for (i = 0; i < n; i++) {
                    int ind = Misc.IndexOf(ordering, i);
                    plot.addPlotable(new Line(getParam_ColorOfClassK(i),
                            new double[] { means[ind], plot.plotCanvas.base.roundXmin[1] },
                            new double[] { means[ind], plot.plotCanvas.base.roundXmax[1] }, 2 * stds[ind]));
                    plot.addPlot(Plot2DPanel.LINE, "Staining " + i, getParam_ColorOfClassK(i),
                            new double[][] { new double[] { means[ind], plot.plotCanvas.base.roundXmin[1] },
                                    new double[] { means[ind], plot.plotCanvas.base.roundXmax[1] } });
                }

                String clusterInfo = "";
                for (String o : clusterer.getOptions()) {
                    clusterInfo += o + " ";
                }
                clusterInfo += "\n\n";
                clusterInfo += clusterer.toString().trim();
                if (getParam_AutomaticClustererString().equalsIgnoreCase("Hierarchical")) {
                    try {
                        clusterInfo += ((HierarchicalClusterer) clusterer).graph();
                        HierarchyVisualizer a = new HierarchyVisualizer(
                                ((HierarchicalClusterer) clusterer).graph());
                        a.setSize(800, 600);
                        if (clusterVisualizer == null) {
                            clusterVisualizer = new JFrame("Hierarchical Clusterer Dendrogram");
                            clusterVisualizer.setIconImage(getIconImage());
                            clusterVisualizer.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                            clusterVisualizer.setSize(800, 600);
                        }
                        Container contentPane = clusterVisualizer.getContentPane();
                        contentPane.removeAll();
                        contentPane.add(a);
                    } catch (Exception e) {
                        clusterVisualizer = null;
                    }
                }
                jTextArea1.setText(clusterInfo);

                if (tmarker.DEBUG > 3) {
                    String info = "Clusterer has options\n";
                    for (String o : clusterer.getOptions()) {
                        info += o + " ";
                    }
                    info += "\n";
                    info += clusterer.toString() + "\n";
                    // info += (clusterer).globalInfo() + "\n";
                    info += "\n";
                    info += clusterInfo + "\n";
                    java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.INFO,
                            info);
                }

                // cluster all TMAspots and assign the corresponding class to them
                // Cluster the points
                List<List<Integer>> clustered_points = new ArrayList<>();
                for (i = 0; i < n; i++) {
                    clustered_points.add(new ArrayList<Integer>());
                }

                int k;
                for (TMAspot ts : tss) {
                    //TODO: GET THE CHANNEL 2 IMAGE
                    //BufferedImage img = ts.getBufferedImage(TMAspot.SHOW_CHANNEL2_IMAGE, false);
                    BufferedImage img = ts.getBufferedImage(false);
                    List<TMApoint> tps = ts.getPoints();
                    for (TMApoint tp : tps) {
                        intensity = TMAspot.getAverageColorAtPoint(img, tp.x, tp.y, ts.getParam_r(), false)
                                .getRed();

                        // add the instance
                        Instance inst = new Instance(1.0, new double[] { intensity });
                        inst.setDataset(data);
                        k = ordering[clusterer.clusterInstance(inst)];

                        // store the color for later visualization
                        clustered_points.get(k).add(intensity);

                        // set the staining of the TMApoint
                        switch (k) {
                        case 0:
                            tp.setStaining(TMALabel.STAINING_0);
                            break;
                        case 1:
                            tp.setStaining(TMALabel.STAINING_1);
                            break;
                        case 2:
                            tp.setStaining(TMALabel.STAINING_2);
                            break;
                        default:
                            tp.setStaining(TMALabel.STAINING_3);
                            break;
                        }
                    }
                    ts.dispStainingInfo();
                    if (manager.getVisibleTMAspot() == ts) {
                        manager.repaintVisibleTMAspot();
                    }
                }

                // Write the description
                String description = "Nuclei clustered with " + getParam_AutomaticClustererString();
                if (getParam_AutomaticClustererString().equalsIgnoreCase("Hierarchical")) {
                    description += " (" + getParam_HierarchicalClusteringMethod() + ")";
                }
                description += ", n=" + getParam_nClusters() + ", channel 2 intensity.";
                jLabel42.setText(description);
                jLabel41.setText(" ");

            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            this.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
        }
    }
}

From source file:intensityclustering.IntensityClustering.java

/**
 * Clusters the TMApoints on given TMAspots according to their staining
 * intensity (color). All parameters (e.g. clusterer and parameters) are
 * selected by the user. Features are simple color features.
 *
 * @param tss The TMAspots of which all nuclei (gold-standard and estimated)
 * are clustered according to color.//  www  . jav  a2  s  . c o  m
 */
private void clusterPointsAutomaticallyColorSpace(List<TMAspot> tss) {
    if (tss.size() > 0) {
        try {
            this.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));

            int n = getParam_nClusters();

            // Create ARFF Data
            FastVector atts;
            Instances data;
            int i;

            // 1. create arff data format
            atts = new FastVector(3);
            for (i = 0; i < 3; i++) {
                atts.addElement(new Attribute(Integer.toString(i)));
            }

            // 2. create Instances object
            data = new Instances("TMA points", atts, tmarker.getNumberNuclei(tss));

            // 3. fill with data
            BufferedImage img;
            Color c;
            float[] features = new float[3];
            String colorSpace = getParam_ColorSpace();
            for (TMAspot ts : tss) {
                img = ts.getBufferedImage();
                List<TMApoint> tps = ts.getPoints();
                for (TMApoint tp : tps) {
                    Color2Feature(TMAspot.getAverageColorAtPoint(img, tp.x, tp.y, ts.getParam_r(), false),
                            colorSpace, features);

                    // add the instance
                    Instance inst = new Instance(1.0, new double[] { features[0], features[1], features[2] });
                    inst.setDataset(data);
                    data.add(inst);
                }
            }

            // 4. set data class index (last attribute is the class)
            //data.setClassIndex(data.numAttributes() - 1); // not for weka 3.5.X
            if (tmarker.DEBUG > 4) {
                java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.INFO,
                        data.toString());
            }

            Clusterer clusterer = getClusterer();
            String[] options = getClustererOptions();
            if (false && colorSpace.equalsIgnoreCase("hsb")) {
                String[] newoptions = new String[options.length + 2];
                System.arraycopy(options, 0, newoptions, 0, options.length);
                newoptions[options.length] = "-A";
                newoptions[options.length + 1] = "weka.core.MyHSBDistance";
                options = newoptions;
            }

            if (tmarker.DEBUG > 3) {
                if (options.length > 0) {
                    String info = "Clusterer should have options\n";
                    for (String o : options) {
                        info += o + " ";
                    }
                    info += "\n";
                    java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.INFO,
                            info);
                }
            }

            clusterer.setOptions(options); // set the clusterer options
            clusterer.buildClusterer(data); // build the clusterer

            // order the clusters according to the brightness
            // The most bright cluster should be 0, then 1, then 2,...
            ArrayList<ArrayList<Double>> values = new ArrayList<>();
            for (i = 0; i < clusterer.numberOfClusters(); i++) {
                values.add(new ArrayList<Double>());
            }
            int z;
            double value;
            for (i = 0; i < data.numInstances(); i++) {
                z = clusterer.clusterInstance(data.instance(i));
                value = getParam_ColorSpace().equalsIgnoreCase("hsb") ? data.instance(i).value(2)
                        : Misc.RGBToGray(data.instance(i).value(0), data.instance(i).value(1),
                                data.instance(i).value(2));
                values.get(z).add(value);
            }
            double[] means = new double[clusterer.numberOfClusters()];
            for (i = 0; i < clusterer.numberOfClusters(); i++) {
                means[i] = Misc.mean(values.get(i).toArray(new Double[values.get(i).size()]));
            }
            int[] ordering = Misc.orderArray(means, !getParam_ColorSpace().equalsIgnoreCase("rtp"));

            String clusterInfo = "";
            for (String o : clusterer.getOptions()) {
                clusterInfo += o + " ";
            }
            clusterInfo += "\n\n";
            clusterInfo += clusterer.toString().trim();
            if (getParam_AutomaticClustererString().equalsIgnoreCase("Hierarchical")) {
                try {
                    clusterInfo += ((HierarchicalClusterer) clusterer).graph();
                    HierarchyVisualizer a = new HierarchyVisualizer(
                            ((HierarchicalClusterer) clusterer).graph());
                    a.setSize(800, 600);
                    if (clusterVisualizer == null) {
                        clusterVisualizer = new JFrame("Hierarchical Clusterer Dendrogram");
                        clusterVisualizer.setIconImage(getIconImage());
                        clusterVisualizer.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                        clusterVisualizer.setSize(800, 600);
                    }
                    Container contentPane = clusterVisualizer.getContentPane();
                    contentPane.removeAll();
                    contentPane.add(a);
                } catch (Exception e) {
                    clusterVisualizer = null;
                }
            }
            jTextArea1.setText(clusterInfo);

            if (tmarker.DEBUG > 3) {
                String info = "Clusterer has options\n";
                for (String o : clusterer.getOptions()) {
                    info += o + " ";
                }
                info += "\n";
                info += clusterer.toString() + "\n";
                // info += (clusterer).globalInfo() + "\n";
                info += "\n";
                info += clusterInfo + "\n";
                java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.INFO,
                        info);
            }

            // cluster all TMAspots and assign the corresponding class to them
            // Cluster the points
            List<List<Color>> clustered_points = new ArrayList<>();
            for (i = 0; i < clusterer.numberOfClusters(); i++) {
                clustered_points.add(new ArrayList<Color>());
            }

            int k;
            for (TMAspot ts : tss) {
                img = ts.getBufferedImage();
                List<TMApoint> tps = ts.getPoints();
                for (TMApoint tp : tps) {
                    c = TMAspot.getAverageColorAtPoint(img, tp.x, tp.y, ts.getParam_r(), false);
                    Color2Feature(c, colorSpace, features);

                    // add the instance
                    Instance inst = new Instance(1.0, new double[] { features[0], features[1], features[2] });
                    inst.setDataset(data);
                    k = ordering[clusterer.clusterInstance(inst)];

                    // store the color for later visualization
                    clustered_points.get(k).add(c);

                    // set the staining of the TMApoint
                    switch (k) {
                    case 0:
                        tp.setStaining(TMALabel.STAINING_0);
                        break;
                    case 1:
                        tp.setStaining(TMALabel.STAINING_1);
                        break;
                    case 2:
                        tp.setStaining(TMALabel.STAINING_2);
                        break;
                    default:
                        tp.setStaining(TMALabel.STAINING_3);
                        break;
                    }
                }
                ts.dispStainingInfo();
                if (manager.getVisibleTMAspot() == ts) {
                    manager.repaintVisibleTMAspot();
                }
            }

            // draw the points
            Plot3DPanel plot;
            if (((java.awt.BorderLayout) (jPanel2.getLayout()))
                    .getLayoutComponent(java.awt.BorderLayout.CENTER) != null) {
                plot = (Plot3DPanel) ((java.awt.BorderLayout) (jPanel2.getLayout()))
                        .getLayoutComponent(java.awt.BorderLayout.CENTER);
                plot.removeAllPlots();
            } else {
                plot = new Plot3DPanel();
                plot.plotCanvas.setBackground(jPanel2.getBackground());
                plot.addLegend(PlotPanel.SOUTH);
                plot.plotLegend.setBackground(jPanel2.getBackground());
            }
            if (colorSpace.equalsIgnoreCase("hsb")) {
                plot.setAxisLabels("Hue", "Saturation", "Brightness");
            } else if (colorSpace.equalsIgnoreCase("rtp")) {
                plot.setAxisLabels("R", "Theta", "Phi");
            } else {
                plot.setAxisLabels("Red", "Green", "Blue");
            }

            for (i = 0; i < clusterer.numberOfClusters(); i++) {
                double[] xs = new double[clustered_points.get(i).size()];
                double[] ys = new double[clustered_points.get(i).size()];
                double[] zs = new double[clustered_points.get(i).size()];
                for (int j = 0; j < clustered_points.get(i).size(); j++) {
                    Color2Feature(clustered_points.get(i).get(j), colorSpace, features);
                    xs[j] = features[0];
                    ys[j] = features[1];
                    zs[j] = features[2];
                }
                if (xs.length > 0) {
                    c = getParam_ColorOfClassK(i);
                    plot.addScatterPlot("Staining " + i, c, xs, ys, zs);
                }
            }

            // Write the description
            String description = "Nuclei clustered with " + getParam_AutomaticClustererString();
            if (getParam_AutomaticClustererString().equalsIgnoreCase("Hierarchical")) {
                description += " (" + getParam_HierarchicalClusteringMethod() + ")";
            }
            description += ", n=" + getParam_nClusters() + ", color space " + getParam_ColorSpace() + ".";
            jLabel41.setText(description);
            jLabel42.setText(" ");

            if (((java.awt.BorderLayout) (jPanel2.getLayout()))
                    .getLayoutComponent(java.awt.BorderLayout.CENTER) == null) {
                jPanel2.add(plot, java.awt.BorderLayout.CENTER);
                validate();
                pack();
            }
        } catch (Exception | OutOfMemoryError e) {
            java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE, null,
                    e);
            JOptionPane.showMessageDialog(this,
                    "The clustering could not be performed.\n\n" + "A possible reasons is:\n"
                            + "- Not enough memory (too many points), \n\n"
                            + "You might want to try a different clustering method or less TMAspots.\n\n"
                            + "The error message is: \n" + e.getMessage(),
                    "Error at Nucleus clustering", JOptionPane.WARNING_MESSAGE);
        } finally {
            this.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
        }
    }
}

From source file:lu.lippmann.cdb.lab.beta.util.WekaUtil2.java

License:Open Source License

/**
 * //from  w  w  w . j  ava  2 s  .co  m
 * @param wekaClusterer
 * @param instances
 * @return
 * @throws Exception
 */
public static List<IndexedInstance> computeClusters(final Clusterer wekaClusterer, final Instances instances)
        throws Exception {
    final Instances ii = new Instances(instances);
    ii.setClassIndex(-1);

    wekaClusterer.buildClusterer(ii);

    final ClusterEvaluation eval = new ClusterEvaluation();
    eval.setClusterer(wekaClusterer);
    eval.evaluateClusterer(ii);

    final int clustersCount = eval.getNumClusters();
    final List<IndexedInstance> clustersList = new ArrayList<IndexedInstance>(clustersCount);

    //Initialize instances
    for (int k = 0; k < clustersCount; k++) {
        clustersList.add(new IndexedInstance(new Instances(instances, 0), new HashMap<Integer, Integer>()));
    }

    final double[] ass = eval.getClusterAssignments();
    if (ass.length != ii.numInstances())
        throw new IllegalStateException();
    for (int i = 0; i < ass.length; i++) {
        IndexedInstance idxi = clustersList.get((int) ass[i]);
        idxi.getInstances().add(instances.instance(i));
        int pos = idxi.getInstances().size() - 1;
        idxi.getMapOrigIndex().put(pos, i);
    }

    return clustersList;
}

From source file:myclusterer.WekaCode.java

public static Clusterer buildClusterer(Instances dataSet, int clusterType) throws Exception {
    Clusterer clusterer = null;
    if (clusterType == SimpleKMeans) {
        SimpleKMeans kmeans = new SimpleKMeans();
        Scanner scan = new Scanner(System.in);
        System.out.print("Masukkan jumlah cluster: ");
        int K = scan.nextInt();
        kmeans.setNumClusters(K);//from   w  ww.  j  ava  2s . c o  m
        clusterer = kmeans;
        clusterer.buildClusterer(dataSet);
    } else if (clusterType == HierarchicalClusterer) {
        HierarchicalClusterer hierarchical = new HierarchicalClusterer();
        Scanner scan = new Scanner(System.in);
        System.out.print("Masukkan jumlah cluster: ");
        int K = scan.nextInt();
        hierarchical.setNumClusters(K);
        clusterer = hierarchical;
        clusterer.buildClusterer(dataSet);
    } else if (clusterType == MyKMeans) {
        MyKMeans kmeans = new MyKMeans();
        Scanner scan = new Scanner(System.in);
        System.out.print("Masukkan jumlah cluster: ");
        int K = scan.nextInt();
        kmeans.setNumClusters(K);
        clusterer = kmeans;
        clusterer.buildClusterer(dataSet);
    } else if (clusterType == MyAgnes) {
        MyAgnes agnes = new MyAgnes();
        Scanner scan = new Scanner(System.in);
        System.out.print("Masukkan jumlah cluster: ");
        int K = scan.nextInt();
        agnes.setNumClusters(K);
        clusterer = agnes;
        clusterer.buildClusterer(dataSet);
    }
    return clusterer;
}

From source file:org.mcennis.graphrat.algorithm.clustering.WekaClassifierClusterer.java

License:Open Source License

@Override
public void execute(Graph g) {

    ActorByMode mode = (ActorByMode) ActorQueryFactory.newInstance().create("ActorByMode");
    mode.buildQuery((String) parameter.get("GroundMode").get(), ".*", false);

    try {/*  www  .j  a  v  a2  s .c  om*/

        Clusterer clusterer = (Clusterer) ((Class) parameter.get("Clusterer").get()).newInstance();
        String[] options = ((String) parameter.get("Options").get()).split("\\s+");

        ((OptionHandler) clusterer).setOptions(options);

        Iterator<Actor> actor = AlgorithmMacros.filterActor(parameter, g, mode, null, null);
        Instances dataSet = null;
        while (actor.hasNext()) {
            Actor a = actor.next();
            Property property = a.getProperty(
                    AlgorithmMacros.getSourceID(parameter, g, (String) parameter.get("SourceProperty").get()));

            if (!property.getValue().isEmpty()) {

                Instance value = (Instance) property.getValue().get(0);

                if ((dataSet == null) && (value.dataset() != null)) {
                    FastVector attributes = new FastVector();
                    for (int i = 0; i < value.dataset().numAttributes(); ++i) {
                        attributes.addElement(value.dataset().attribute(i));
                    }
                    dataSet = new Instances("Clustering", attributes, 1000);
                } else if ((dataSet == null)) {
                    FastVector attributes = new FastVector();
                    for (int i = 0; i < value.numAttributes(); ++i) {
                        Attribute element = new Attribute(Integer.toString(i));
                        attributes.addElement(element);
                    }
                    dataSet = new Instances("Clustering", attributes, 1000);
                }
                dataSet.add(value);
            }

        }
        clusterer.buildClusterer(dataSet);
        actor = AlgorithmMacros.filterActor(parameter, g, mode, null, null);
        HashMap<Integer, Graph> clusters = new HashMap<Integer, Graph>();
        while (actor.hasNext()) {
            Actor a = actor.next();
            Property property = a.getProperty(
                    AlgorithmMacros.getSourceID(parameter, g, (String) parameter.get("SourceProperty").get()));
            if (!property.getValue().isEmpty()) {

                Instance instance = (Instance) property.getValue().get(0);
                int cluster = -1;

                try {

                    cluster = clusterer.clusterInstance(instance);
                    if (!clusters.containsKey(cluster)) {
                        Graph graph = GraphFactory.newInstance().create(AlgorithmMacros.getDestID(parameter, g,
                                (String) parameter.get("GraphID").get() + cluster), parameter);
                        clusters.put(cluster, graph);
                    }
                    clusters.get(cluster).add(a);
                } catch (Exception ex) {

                    Logger.getLogger(WekaClassifierClusterer.class.getName()).log(Level.SEVERE,
                            "ClusterInstance on clusterer failed", ex);

                }

                Property clusterProperty = PropertyFactory.newInstance().create("BasicProperty", AlgorithmMacros
                        .getDestID(parameter, g, (String) parameter.get("DestinationProperty").get()),
                        Integer.class);

                clusterProperty.add(new Integer(cluster));

                a.add(clusterProperty);

            }
        }

        Iterator<Graph> graphIt = clusters.values().iterator();
        while (graphIt.hasNext()) {
            LinkQuery query = (LinkQuery) parameter.get("LinkQuery").get();
            Graph graph = graphIt.next();
            Iterator<Link> link = query.executeIterator(g, graph.getActor(), graph.getActor(), null);
            while (link.hasNext()) {
                graph.add(link.next());
            }
            if ((Boolean) parameter.get("AddContext").get()) {
                TreeSet<Actor> actorSet = new TreeSet<Actor>();
                actorSet.addAll(graph.getActor());
                link = query.executeIterator(g, actorSet, null, null);
                while (link.hasNext()) {
                    Link l = link.next();
                    Actor d = l.getDestination();
                    if (graph.getActor(d.getMode(), d.getID()) == null) {
                        graph.add(d);
                    }
                    if (graph.getLink(l.getRelation(), l.getSource(), l.getDestination()) == null) {
                        graph.add(l);
                    }
                }

                link = query.executeIterator(g, null, actorSet, null);
                while (link.hasNext()) {
                    Link l = link.next();
                    Actor d = l.getSource();
                    if (graph.getActor(d.getMode(), d.getID()) == null) {
                        graph.add(d);
                    }
                    if (graph.getLink(l.getRelation(), l.getSource(), l.getDestination()) == null) {
                        graph.add(l);
                    }
                }
            }
        }

    } catch (InstantiationException ex) {

        Logger.getLogger(WekaClassifierClusterer.class.getName()).log(Level.SEVERE, null, ex);

    } catch (IllegalAccessException ex) {

        Logger.getLogger(WekaClassifierClusterer.class.getName()).log(Level.SEVERE, null, ex);

    } catch (Exception ex) {

        Logger.getLogger(WekaClassifierClusterer.class.getName()).log(Level.SEVERE, null, ex);

    }

}

From source file:org.mcennis.graphrat.algorithm.clustering.WekaProbablisticClusterer.java

License:Open Source License

@Override

public void execute(Graph g) {
    ActorByMode mode = (ActorByMode) ActorQueryFactory.newInstance().create("ActorByMode");
    mode.buildQuery((String) parameter.get("GroundMode").get(), ".*", false);

    try {/*from w  w w  .  java  2 s.c  om*/

        Clusterer clusterer = (Clusterer) ((Class) parameter.get("Clusterer").get()).newInstance();
        String[] options = ((String) parameter.get("Options").get()).split("\\s+");

        ((OptionHandler) clusterer).setOptions(options);

        Iterator<Actor> actor = AlgorithmMacros.filterActor(parameter, g, mode, null, null);
        Instances dataSet = null;
        while (actor.hasNext()) {
            Actor a = actor.next();
            Property property = a.getProperty(
                    AlgorithmMacros.getSourceID(parameter, g, (String) parameter.get("SourceProperty").get()));

            if (!property.getValue().isEmpty()) {

                Instance value = (Instance) property.getValue().get(0);

                if ((dataSet == null) && (value.dataset() != null)) {
                    FastVector attributes = new FastVector();
                    for (int i = 0; i < value.dataset().numAttributes(); ++i) {
                        attributes.addElement(value.dataset().attribute(i));
                    }
                    dataSet = new Instances("Clustering", attributes, 1000);
                } else if ((dataSet == null)) {
                    FastVector attributes = new FastVector();
                    for (int i = 0; i < value.numAttributes(); ++i) {
                        Attribute element = new Attribute(Integer.toString(i));
                        attributes.addElement(element);
                    }
                    dataSet = new Instances("Clustering", attributes, 1000);
                }
                dataSet.add(value);
            }

        }
        clusterer.buildClusterer(dataSet);
        actor = AlgorithmMacros.filterActor(parameter, g, mode, null, null);
        HashMap<Integer, Graph> clusters = new HashMap<Integer, Graph>();
        while (actor.hasNext()) {
            Actor a = actor.next();
            Property property = a.getProperty(
                    AlgorithmMacros.getSourceID(parameter, g, (String) parameter.get("SourceProperty").get()));
            if (!property.getValue().isEmpty()) {

                Instance instance = (Instance) property.getValue().get(0);
                double[] cluster = new double[] {};

                try {

                    cluster = clusterer.distributionForInstance(instance);
                } catch (Exception ex) {

                    Logger.getLogger(WekaClassifierClusterer.class.getName()).log(Level.SEVERE,
                            "ClusterInstance on clusterer failed", ex);

                }

                Property clusterProperty = PropertyFactory.newInstance().create("BasicProperty",
                        AlgorithmMacros.getDestID(parameter, g,
                                (String) parameter.get("DestinationProperty").get()),
                        (new double[] {}).getClass());

                clusterProperty.add(cluster);

                a.add(clusterProperty);

            }
        }

    } catch (InstantiationException ex) {

        Logger.getLogger(WekaClassifierClusterer.class.getName()).log(Level.SEVERE, null, ex);

    } catch (IllegalAccessException ex) {

        Logger.getLogger(WekaClassifierClusterer.class.getName()).log(Level.SEVERE, null, ex);

    } catch (Exception ex) {

        Logger.getLogger(WekaClassifierClusterer.class.getName()).log(Level.SEVERE, null, ex);

    }

}

From source file:tubes2.Main.java

/**
 * @param args the command line arguments
 *///from www  .j  a va 2s  .  co  m
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);
        }
    }
}