Example usage for weka.core Instances classIndex

List of usage examples for weka.core Instances classIndex

Introduction

In this page you can find the example usage for weka.core Instances classIndex.

Prototype


publicint classIndex() 

Source Link

Document

Returns the class attribute's index.

Usage

From source file:Learning.WekaWrapper.java

public double[] evaluate(String fn) throws Exception {

    ConverterUtils.DataSource source = new ConverterUtils.DataSource(fn);

    Instances data = source.getDataSet();

    // setting class attribute if the data format does not provide this information
    // For example, the XRFF format saves the class attribute information as well
    if (data.classIndex() == -1) {
        data.setClassIndex(data.numAttributes() - 1);
    }/*  w  w w .j a  v a 2  s  . c  o m*/

    NumericToNominal nmf = new NumericToNominal();
    nmf.setInputFormat(data);
    data = Filter.useFilter(data, nmf);

    tree = new J48(); // new instance of tree

    String[] options = new String[1];

    options[0] = "-C 0.25 -M 2";
    tree.setOptions(options);
    tree.buildClassifier(data); // build classifier

    // eval
    eval = new Evaluation(data);
    eval.crossValidateModel(tree, data, 5, new Random(1));

    // System.out.println("corr: " + eval.pctCorrect());
    // System.out.println("inco: " + eval.pctIncorrect());
    // System.out.println(eval.toSummaryString());
    // System.out.println(eval.toMatrixString());
    //  System.out.println(eval.toClassDetailsString());
    double[] results = new double[2];
    results[0] = eval.pctCorrect();
    results[1] = eval.pctIncorrect();
    return results;
}

From source file:lector.Analizador.java

public static void clasificador() {

    BufferedReader reader1;/*from w  ww  .  j a  v  a 2  s  . c om*/
    BufferedReader reader2;
    try {
        reader1 = new BufferedReader(new FileReader("/Users/danieltapia/Google Drive/EPN/MAESTRIA/MSW128 BI/"
                + "proyecto/compartida/DataSetAnalisisSentimientos.arff"));

        reader2 = new BufferedReader(new FileReader("/Users/danieltapia/Google Drive/EPN/MAESTRIA/MSW128 BI/"
                + "proyecto/compartida/DataSetAnalisisSentimientos_inc.arff"));
        Instances train = new Instances(reader1);
        train.setClassIndex(train.numAttributes() - 1);
        System.out.println(train.classIndex() + " " + train.numAttributes());

        Instances test = new Instances(reader2);
        test.setClassIndex(train.numAttributes() - 1);
        System.out.println(test.classIndex() + " " + test.numAttributes());

        NaiveBayes model = new NaiveBayes();
        model.buildClassifier(train);

        //classify
        Instances labeled = new Instances(test);

        for (int i = 0; i < test.numInstances(); i++) {
            double clsLabel = model.classifyInstance(test.instance(i));
            labeled.instance(i).setClassValue(clsLabel);
        }

        // https://youtu.be/JY_x5zKTfyo?list=PLJbE6j2EG1pZnBhOg3_Rb63WLCprtyJag
        Evaluation eval_train = new Evaluation(test);
        eval_train.evaluateModel(model, test);

        reader1.close();
        reader2.close();

        //System.out.println(eval_train.toSummaryString("\nResults\n======\n", false));
        String[] options = new String[4];
        options[0] = "-t"; //name of training file
        options[1] = "/Users/danieltapia/Google Drive/EPN/MAESTRIA/MSW128 BI/proyecto/"
                + "compartida/DataSetAnalisisSentimientos.arff";
        options[2] = "-T";
        options[3] = "/Users/danieltapia/Google Drive/EPN/MAESTRIA/MSW128 BI/proyecto/"
                + "compartida/DataSetAnalisisSentimientos_inc.arff";
        System.out.println(Evaluation.evaluateModel(model, options));

        try ( // print classification results to file
                BufferedWriter writer = new BufferedWriter(
                        new FileWriter("/Users/danieltapia/Google Drive/EPN/MAESTRIA/MSW128 BI/"
                                + "proyecto/compartida/DataSetAnalisisSentimientos_labeled.arff"))) {
            writer.write(labeled.toString());
        }

    } catch (Exception e) {
    }
}

From source file:LogReg.Logistic.java

License:Open Source License

/**
 * Builds the classifier/*from   www . j  a  v a2 s .co  m*/
 *
 * @param train the training data to be used for generating the
 * boosted classifier.
 * @throws Exception if the classifier could not be built successfully
 */
public void buildClassifier(Instances train) throws Exception {
    // can classifier handle the data?
    getCapabilities().testWithFail(train);

    // remove instances with missing class
    train = new Instances(train);
    train.deleteWithMissingClass();

    // Replace missing values   
    m_ReplaceMissingValues = new ReplaceMissingValues();
    m_ReplaceMissingValues.setInputFormat(train);
    train = Filter.useFilter(train, m_ReplaceMissingValues);

    // Remove useless attributes
    m_AttFilter = new RemoveUseless();
    m_AttFilter.setInputFormat(train);
    train = Filter.useFilter(train, m_AttFilter);

    // Transform attributes
    m_NominalToBinary = new NominalToBinary();
    m_NominalToBinary.setInputFormat(train);
    train = Filter.useFilter(train, m_NominalToBinary);

    // Save the structure for printing the model
    m_structure = new Instances(train, 0);

    // Extract data
    m_ClassIndex = train.classIndex();
    m_NumClasses = train.numClasses();

    int nK = m_NumClasses - 1; // Only K-1 class labels needed 
    int nR = m_NumPredictors = train.numAttributes() - 1;
    int nC = train.numInstances();

    m_Data = new double[nC][nR + 1]; // Data values
    int[] Y = new int[nC]; // Class labels
    double[] xMean = new double[nR + 1]; // Attribute means
    xSD = new double[nR + 1]; // Attribute stddev's
    double[] sY = new double[nK + 1]; // Number of classes
    double[] weights = new double[nC]; // Weights of instances
    double totWeights = 0; // Total weights of the instances
    m_Par = new double[nR + 1][nK]; // Optimized parameter values

    if (m_Debug) {
        System.out.println("Extracting data...");
    }

    for (int i = 0; i < nC; i++) {
        // initialize X[][]
        Instance current = train.instance(i);
        Y[i] = (int) current.classValue(); // Class value starts from 0
        weights[i] = current.weight(); // Dealing with weights
        totWeights += weights[i];

        m_Data[i][0] = 1;
        int j = 1;
        for (int k = 0; k <= nR; k++) {
            if (k != m_ClassIndex) {
                double x = current.value(k);
                m_Data[i][j] = x;
                xMean[j] += weights[i] * x;
                xSD[j] += weights[i] * x * x;
                j++;
            }
        }

        // Class count
        sY[Y[i]]++;
    }

    if ((totWeights <= 1) && (nC > 1))
        throw new Exception("Sum of weights of instances less than 1, please reweight!");

    xMean[0] = 0;
    xSD[0] = 1;
    for (int j = 1; j <= nR; j++) {
        xMean[j] = xMean[j] / totWeights;
        if (totWeights > 1)
            xSD[j] = Math.sqrt(Math.abs(xSD[j] - totWeights * xMean[j] * xMean[j]) / (totWeights - 1));
        else
            xSD[j] = 0;
    }

    if (m_Debug) {
        // Output stats about input data
        System.out.println("Descriptives...");
        for (int m = 0; m <= nK; m++)
            System.out.println(sY[m] + " cases have class " + m);
        System.out.println("\n Variable     Avg       SD    ");
        for (int j = 1; j <= nR; j++)
            System.out.println(Utils.doubleToString(j, 8, 4) + Utils.doubleToString(xMean[j], 10, 4)
                    + Utils.doubleToString(xSD[j], 10, 4));
    }

    // Normalise input data 
    for (int i = 0; i < nC; i++) {
        for (int j = 0; j <= nR; j++) {
            if (xSD[j] != 0) {
                m_Data[i][j] = (m_Data[i][j] - xMean[j]) / xSD[j];
            }
        }
    }

    if (m_Debug) {
        System.out.println("\nIteration History...");
    }

    double x[] = new double[(nR + 1) * nK];
    double[][] b = new double[2][x.length]; // Boundary constraints, N/A here

    // Initialize
    for (int p = 0; p < nK; p++) {
        int offset = p * (nR + 1);
        x[offset] = Math.log(sY[p] + 1.0) - Math.log(sY[nK] + 1.0); // Null model
        b[0][offset] = Double.NaN;
        b[1][offset] = Double.NaN;
        for (int q = 1; q <= nR; q++) {
            x[offset + q] = 0.0;
            b[0][offset + q] = Double.NaN;
            b[1][offset + q] = Double.NaN;
        }
    }

    OptEng opt = new OptEng();
    opt.setDebug(m_Debug);
    opt.setWeights(weights);
    opt.setClassLabels(Y);

    if (m_MaxIts == -1) { // Search until convergence
        x = opt.findArgmin(x, b);
        while (x == null) {
            x = opt.getVarbValues();
            if (m_Debug)
                System.out.println("200 iterations finished, not enough!");
            x = opt.findArgmin(x, b);
        }
        if (m_Debug)
            System.out.println(" -------------<Converged>--------------");
    } else {
        opt.setMaxIteration(m_MaxIts);
        x = opt.findArgmin(x, b);
        if (x == null) // Not enough, but use the current value
            x = opt.getVarbValues();
    }

    m_LL = -opt.getMinFunction(); // Log-likelihood

    // Don't need data matrix anymore
    m_Data = null;

    // Convert coefficients back to non-normalized attribute units
    for (int i = 0; i < nK; i++) {
        m_Par[0][i] = x[i * (nR + 1)];
        for (int j = 1; j <= nR; j++) {
            m_Par[j][i] = x[i * (nR + 1) + j];
            if (xSD[j] != 0) {
                m_Par[j][i] /= xSD[j];
                m_Par[0][i] -= m_Par[j][i] * xMean[j];
            }
        }
    }
}

From source file:lu.lippmann.cdb.common.gui.dataset.InstancesLoaderDialogFactory.java

License:Open Source License

private static Instances showDialog(final Component parent, final boolean setClass) throws Exception {
    final Preferences prefs = Preferences.userRoot().node("CadralDecisionBuild");
    final String path = prefs.get(REG_KEY, WekaDataAccessUtil.DEFAULT_SAMPLE_DIR);

    final JFileChooser fc = new JFileChooser();
    fc.setCurrentDirectory(new File(path));
    final int returnVal = fc.showOpenDialog(parent);
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        final File file = fc.getSelectedFile();
        if (file != null) {
            prefs.put(REG_KEY, file.getPath());
            final Instances ds = WekaDataAccessUtil.loadInstancesFromARFFOrCSVFile(file);
            final Attribute defaultClassAttr = ds.classIndex() >= 0 ? ds.classAttribute() : ds.attribute(0);
            ds.setClassIndex(-1);//  ww  w  . jav a2 s  . com
            ds.setRelationName(file.getPath());
            final List<String> attributesNames = new ArrayList<String>();
            final Enumeration<?> e = ds.enumerateAttributes();
            while (e.hasMoreElements()) {
                final Attribute attr = (Attribute) e.nextElement();
                attributesNames.add(attr.name());
            }

            if (setClass) {
                final String s = (String) JOptionPane.showInputDialog(parent,
                        "Select the class attribute for '" + file.getName() + "' (default:'"
                                + defaultClassAttr.name() + "'): ",
                        "Class selection", JOptionPane.QUESTION_MESSAGE, null, // icon
                        attributesNames.toArray(), attributesNames.get(attributesNames.size() - 1));
                if (s != null) {
                    ds.setClass(ds.attribute(s));
                } else {
                    //Otherwise no class defined and CACHE attributeClass => No class index defined after cancel + retry
                    ds.setClass(defaultClassAttr);
                    return null;
                }
            } else {
                ds.setClass(defaultClassAttr);
            }
            return ds;
        } else
            throw new Exception();
    } else
        return null;
}

From source file:lu.lippmann.cdb.datasetview.DatasetView.java

License:Open Source License

public DatasetView setDataSet(final Instances pdataSet) {
    if (pdataSet.classIndex() != -1 && !pdataSet.classAttribute().isNominal())
        pdataSet.setClassIndex(-1);//  w  w w  . jav  a2 s .com

    if (this.initialDataSet == null) {
        this.initialDataSet = pdataSet;
        this.initialCompleteness = new CompletenessComputer(this.initialDataSet);
        this.dataCompletenessProgressBar.setMaximum(pdataSet.numInstances() * pdataSet.numAttributes());
        reinitDataCompleteness();
    }

    this.dataSet = pdataSet;

    if (!filtered)
        this.notFilteredDataSet = pdataSet;

    updateClassSelectionMenu();
    this.supervisedTransformPane.setVisible(pdataSet.classIndex() != -1);

    for (final TabView tv : tabViews) {
        tv.update(dataSet);
    }

    try {
        updateFiltersPane(dataSet);
    } catch (Exception e) {
        eventPublisher.publish(new ErrorOccuredEvent("Error when updating filters", e));
    }

    updateTooltipShowingDatasetDimensions();

    return this;
}

From source file:lu.lippmann.cdb.datasetview.tabs.AbstractTabView.java

License:Open Source License

/**
 * {@inheritDoc}/*from w  w w. ja v  a2 s  .co m*/
 */
@Override
public final void update(final Instances dataset) {
    if (this.parentTabbedPane != null) {
        boolean enabled = true;
        if (needsClassAttribute())
            enabled &= (dataset.classIndex() != -1);
        if (needsDateAttribute())
            enabled &= (WekaDataStatsUtil.getFirstDateAttributeIdx(dataset) != -1);

        this.parentTabbedPane.setEnabledAt(this.posInParentTabbedPane, enabled);
        if (!enabled && isTabSelected())
            this.parentTabbedPane.setSelectedIndex(0);
        if (!enabled)
            return;
    }

    if (isSlow()) {
        final Runnable r = new Runnable() {
            @Override
            public void run() {
                processUpdate(new Instances(dataset)); // duplication to avoid strange problem
            }
        };
        if (UPDATE_SLOWTABS_ONBACKGROUND || isTabSelected()) {
            this.executorService.execute(r);
        } else {
            this.updateToExecuteWhenTabIsSelected = r;
        }
    } else {
        processUpdate(dataset);
    }
}

From source file:lu.lippmann.cdb.datasetview.tabs.AttributesSummaryTabView.java

License:Open Source License

/**
 * {@inheritDoc}/*from www .ja  v a 2 s. co m*/
 */
@Override
public void update0(final Instances dataSet) throws Exception {
    final int numAttr = dataSet.numAttributes();

    final JTabbedPane tabbedPane = new JTabbedPane();

    this.as.setGridWidth(Math.min(3, numAttr));
    this.as.setColoringIndex(dataSet.classIndex());
    this.as.setInstances(dataSet);
    tabbedPane.addTab("All", this.as);

    for (int i = 0; i < numAttr; i++) {
        final Instances nds = WekaDataProcessingUtil.buildFilteredByAttributesDataSet(dataSet, new int[] { i });
        final AttributeSummarizer as0 = new AttributeSummarizer();
        as0.setGridWidth(1);
        as0.setColoringIndex(nds.classIndex());
        as0.setInstances(nds);
        tabbedPane.addTab(dataSet.attribute(i).name(), as0);
    }

    this.jxp.removeAll();
    this.jxp.add(tabbedPane, BorderLayout.CENTER);
    this.jxp.repaint();
}

From source file:lu.lippmann.cdb.datasetview.tabs.MDSTabView.java

License:Open Source License

/**
 * {@inheritDoc}/*from  w  w  w . ja  v  a2  s  .c  om*/
 */
@Override
public void update0(final Instances dataSet) throws Exception {
    this.jxp.removeAll();

    if (this.distComboListener != null)
        distCombo.removeActionListener(this.distComboListener);
    this.distComboListener = new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            if (!currentDist.equals(distCombo.getSelectedItem()))
                update(dataSet);
            currentDist = distCombo.getSelectedItem();

            final MDSDistancesEnum mde = MDSDistancesEnum.valueOf(currentDist.toString());
            boolean showDistanceParameters = (mde.equals(MDSDistancesEnum.MINKOWSKI));
            distanceParameters.setVisible(showDistanceParameters);
            distanceParametersLabel.setVisible(showDistanceParameters);
        }
    };
    this.distCombo.addActionListener(this.distComboListener);

    if (this.distanceParametersListener != null)
        distanceParameters.removeActionListener(this.distanceParametersListener);
    this.distanceParameters.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            if (!currentParameter.equals(distanceParameters.getText()))
                update(dataSet);
            currentParameter = distanceParameters.getText();
        }
    });
    this.distanceParameters.addActionListener(this.distanceParametersListener);

    if (this.shihListener != null)
        shihCheckbox.removeActionListener(this.shihListener);
    this.shihListener = new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            update(dataSet);
        }
    };
    this.shihCheckbox.addActionListener(this.shihListener);
    this.shihCheckbox.setEnabled(!WekaDataStatsUtil.areAllAttributesNominal(dataSet));

    if (this.ignoreListener != null)
        ignoreClassCheckbox.removeActionListener(this.ignoreListener);
    this.ignoreListener = new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            update(dataSet);
        }
    };
    this.ignoreClassCheckbox.addActionListener(this.ignoreListener);
    this.ignoreClassCheckbox.setEnabled(dataSet.classIndex() != -1);

    if (this.maxInstancesListener != null)
        maxInstances.removeKeyListener(this.maxInstancesListener);
    this.maxInstancesListener = new KeyAdapter() {
        @Override
        public void keyPressed(KeyEvent e) {
            final int cCode = e.getKeyCode();
            if (cCode == KeyEvent.VK_ENTER) {
                update(dataSet);
                e.consume();
            }
        }
    };
    this.maxInstances.addKeyListener(maxInstancesListener);

    if (this.normalizeListener != null)
        normalizeCheckbox.removeActionListener(this.normalizeListener);
    this.normalizeListener = new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            update(dataSet);
        }
    };
    this.normalizeCheckbox.addActionListener(this.normalizeListener);

    //TODO : use proper layout ...
    final JXPanel northPanel = new JXPanel();
    northPanel.setLayout(new GridBagLayout());
    final GridBagConstraints gbc = new GridBagConstraints();
    gbc.gridx = 0;
    gbc.gridy = 0;
    gbc.gridwidth = 2;
    gbc.weightx = 1;
    gbc.fill = GridBagConstraints.BOTH;
    northPanel.add(this.distCombo, gbc);
    gbc.weightx = 0;
    gbc.gridwidth = 1;
    gbc.gridy = 1;
    northPanel.add(this.distanceParametersLabel, gbc);
    gbc.gridx = 1;
    northPanel.add(this.distanceParameters, gbc);

    this.jxp.add(northPanel, BorderLayout.NORTH);

    final MDSDistancesEnum mde = MDSDistancesEnum.valueOf(distCombo.getSelectedItem().toString());
    final String strOrder = distanceParameters.getText();
    if (mde.equals(MDSDistancesEnum.MINKOWSKI)) {
        mde.setParameters(new String[] { strOrder });
    }
    Instances usedDataSet = dataSet;
    if (shihCheckbox.isSelected()) {
        //Modify instance using SHIH Algorithm
        final Shih2010 shih = new Shih2010(dataSet);
        usedDataSet = shih.getModifiedInstances();
    }

    this.kmeansButton = new JButton("K-means");
    this.maxKField = new JTextField("10");

    //Create whole panel
    final JXPanel southPanel = new JXPanel();
    southPanel.add(shihCheckbox);
    southPanel.add(ignoreClassCheckbox);
    southPanel.add(normalizeCheckbox);
    southPanel.add(maxInstances);
    southPanel.add(new JLabel("Maximum K"));
    southPanel.add(maxKField);
    southPanel.add(kmeansButton);
    this.jxp.add(southPanel, BorderLayout.SOUTH);

    //Compute MDS
    final MDSResult mdsResult = ClassicMDS.doMDS(usedDataSet, mde, 2, Integer.valueOf(maxInstances.getText()),
            ignoreClassCheckbox.isSelected(), normalizeCheckbox.isSelected());

    final JXPanel mdsView = MDSViewBuilder.buildMDSViewFromDataSet(dataSet, mdsResult,
            Integer.valueOf(maxInstances.getText()), new Listener<Instances>() {
                @Override
                public void onAction(final Instances parameter) {
                    pushDataChange(new DataChange(parameter, TabView.DataChangeTypeEnum.Selection));
                }
            });
    this.jxp.add(mdsView, BorderLayout.CENTER);

    this.kmeansButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            try {
                //List of coordinates (x,y) of collapsed instances
                final Instances coordsInstances = mdsResult.buildInstancesFromMatrix();
                //FIXME dangerous : K-means on ordered collapsedInstance coordinates
                final KmeansImproved km = new KmeansImproved(coordsInstances,
                        Integer.valueOf(maxKField.getText()));
                final double[] ass = km.getClusteredInstances();
                int usedK = km.getUsedKmeans().getNumClusters();
                final StringBuilder labels = new StringBuilder();
                for (int i = 0; i < usedK; i++) {
                    labels.append("cluster").append((i + 1));
                    if (i < usedK - 1)
                        labels.append(",");
                }

                //Build modified dataset
                String attributeName = "cluster_proj";
                while (dataSet.attribute(attributeName) != null)
                    attributeName += "_proj";
                final Add addFilter = new Add();
                addFilter.setAttributeIndex("last");
                addFilter.setAttributeName(attributeName);
                addFilter.setNominalLabels(labels.toString());
                addFilter.setInputFormat(dataSet);
                final Instances modDataset = Filter.useFilter(dataSet, addFilter);
                final int nbInstances = modDataset.numInstances();
                final int nbAttributes = modDataset.numAttributes();

                if (mdsResult.getCInstances().isCollapsed()) {
                    //
                    final KmeansResult kmr = mdsResult.getCInstances().getCentroidMap();
                    final List<Instances> clusters = kmr.getClusters();
                    int nbClusters = clusters.size();

                    //Build a map between any instance and it's cluster's centroid
                    final Map<ComparableInstance, Integer> mapCentroid = new HashMap<ComparableInstance, Integer>();
                    for (int i = 0; i < nbClusters; i++) {
                        final Instances cluster = clusters.get(i);
                        final int clusterSize = cluster.size();
                        for (int k = 0; k < clusterSize; k++) {
                            mapCentroid.put(new ComparableInstance(cluster.instance(k)), i);
                        }
                    }

                    //Use the previous map to add the additionnal feature for every element !
                    for (int i = 0; i < nbInstances; i++) {
                        final int centroidIndex = mapCentroid.get(new ComparableInstance(dataSet.instance(i)));
                        final String value = "cluster" + (int) (ass[centroidIndex] + 1);
                        modDataset.instance(i).setValue(nbAttributes - 1, value);
                    }
                } else {
                    for (int i = 0; i < nbInstances; i++) {
                        final String value = "cluster" + (int) (ass[i] + 1);
                        modDataset.instance(i).setValue(nbAttributes - 1, value);
                    }
                }
                pushDataChange(new DataChange(modDataset, TabView.DataChangeTypeEnum.Update));
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        }
    });

    this.jxp.repaint();
}

From source file:lu.lippmann.cdb.datasetview.tabs.ScatterPlotTabView.java

License:Open Source License

private void update0(final Instances dataSet, int xidx, int yidx, int coloridx, final boolean asSerie) {
    System.out.println(xidx + " " + yidx);

    this.panel.removeAll();

    if (xidx == -1)
        xidx = 0;/*from   w  w  w  . j  a va  2  s.co m*/
    if (yidx == -1)
        yidx = 1;
    if (coloridx == -1)
        coloridx = 0;

    final Object[] numericAttrNames = WekaDataStatsUtil.getNumericAttributesNames(dataSet).toArray();

    final JComboBox xCombo = new JComboBox(numericAttrNames);
    xCombo.setBorder(new TitledBorder("x"));
    xCombo.setSelectedIndex(xidx);
    final JComboBox yCombo = new JComboBox(numericAttrNames);
    yCombo.setBorder(new TitledBorder("y"));
    yCombo.setSelectedIndex(yidx);
    final JCheckBox jcb = new JCheckBox("Draw lines");
    jcb.setSelected(asSerie);
    final JComboBox colorCombo = new JComboBox(numericAttrNames);
    colorCombo.setBorder(new TitledBorder("color"));
    colorCombo.setSelectedIndex(coloridx);
    colorCombo.setVisible(dataSet.classIndex() < 0);

    xCombo.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            update0(dataSet, xCombo.getSelectedIndex(), yCombo.getSelectedIndex(),
                    colorCombo.getSelectedIndex(), jcb.isSelected());
        }
    });

    yCombo.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            update0(dataSet, xCombo.getSelectedIndex(), yCombo.getSelectedIndex(),
                    colorCombo.getSelectedIndex(), jcb.isSelected());
        }
    });

    colorCombo.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            update0(dataSet, xCombo.getSelectedIndex(), yCombo.getSelectedIndex(),
                    colorCombo.getSelectedIndex(), jcb.isSelected());
        }
    });

    jcb.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            update0(dataSet, xCombo.getSelectedIndex(), yCombo.getSelectedIndex(),
                    colorCombo.getSelectedIndex(), jcb.isSelected());
        }
    });

    final JXPanel comboPanel = new JXPanel();
    comboPanel.setLayout(new GridLayout(1, 0));
    comboPanel.add(xCombo);
    comboPanel.add(yCombo);
    comboPanel.add(colorCombo);
    comboPanel.add(jcb);
    this.panel.add(comboPanel, BorderLayout.NORTH);

    final java.util.List<Integer> numericAttrIdx = WekaDataStatsUtil.getNumericAttributesIndexes(dataSet);
    final ChartPanel scatterplotChartPanel = buildChartPanel(dataSet, numericAttrIdx.get(xidx),
            numericAttrIdx.get(yidx), numericAttrIdx.get(coloridx), asSerie);

    this.panel.add(scatterplotChartPanel, BorderLayout.CENTER);

    this.panel.repaint();
    this.panel.updateUI();
}

From source file:lu.lippmann.cdb.datasetview.tabs.ScatterPlotTabView.java

License:Open Source License

private static ChartPanel buildChartPanel(final Instances dataSet, final int xidx, final int yidx,
        final int coloridx, final boolean asSerie) {
    final XYSeriesCollection data = new XYSeriesCollection();
    final Map<Integer, List<Instance>> filteredInstances = new HashMap<Integer, List<Instance>>();
    final int classIndex = dataSet.classIndex();
    if (classIndex < 0) {
        final XYSeries series = new XYSeries("Serie", false);
        for (int i = 0; i < dataSet.numInstances(); i++) {
            series.add(dataSet.instance(i).value(xidx), dataSet.instance(i).value(yidx));

        }/* ww w.  j a  v a  2 s  .  co m*/
        data.addSeries(series);
    } else {
        final Set<String> pvs = WekaDataStatsUtil.getPresentValuesForNominalAttribute(dataSet, classIndex);
        int p = 0;
        for (final String pv : pvs) {
            final XYSeries series = new XYSeries(pv, false);
            for (int i = 0; i < dataSet.numInstances(); i++) {
                if (dataSet.instance(i).stringValue(classIndex).equals(pv)) {
                    if (!filteredInstances.containsKey(p)) {
                        filteredInstances.put(p, new ArrayList<Instance>());
                    }
                    filteredInstances.get(p).add(dataSet.instance(i));

                    series.add(dataSet.instance(i).value(xidx), dataSet.instance(i).value(yidx));
                }
            }
            data.addSeries(series);

            p++;
        }

    }

    final JFreeChart chart = ChartFactory.createScatterPlot("Scatter Plot", // chart title
            dataSet.attribute(xidx).name(), // x axis label
            dataSet.attribute(yidx).name(), // y axis label
            data, // data
            PlotOrientation.VERTICAL, true, // include legend
            true, // tooltips
            false // urls
    );

    final XYPlot xyPlot = (XYPlot) chart.getPlot();
    final XYToolTipGenerator gen = new XYToolTipGenerator() {
        @Override
        public String generateToolTip(final XYDataset dataset, final int series, final int item) {
            if (classIndex < 0) {
                return InstanceFormatter.htmlFormat(dataSet.instance(item), true);
            } else {
                return InstanceFormatter.htmlFormat(filteredInstances.get(series).get(item), true);
            }
        }
    };

    int nbSeries;
    if (classIndex < 0) {
        nbSeries = 1;
    } else {
        nbSeries = filteredInstances.keySet().size();
    }

    final XYItemRenderer renderer = new XYLineAndShapeRenderer(asSerie, true) {
        /** */
        private static final long serialVersionUID = 1L;

        @Override
        public Paint getItemPaint(final int row, final int col) {
            //System.out.println(row+" "+col);
            if (classIndex < 0) {
                final double v = dataSet.instance(col).value(coloridx);
                final double[] minmax = WekaDataStatsUtil.getMinMaxForAttributeAsArrayOfDoubles(dataSet,
                        coloridx);

                final double rated = (v - minmax[0]) / (minmax[1] - minmax[0]);
                System.out.println("rated -> " + rated + " min=" + minmax[0] + "max=" + minmax[1]);
                final int colorIdx = (int) Math.round((ColorHelper.YlGnBu_9_COLORS.length - 1) * rated);

                //System.out.println(minmax[0]+" "+minmax[1]+" "+v+" "+rated+" "+colorIdx);
                return ColorHelper.YlGnBu_9_COLORS[colorIdx];
            } else
                return super.getItemPaint(row, col);
        }
    };
    xyPlot.setRenderer(renderer);

    for (int i = 0; i < nbSeries; i++) {
        renderer.setSeriesToolTipGenerator(i, gen);
    }

    return new ChartPanel(chart);
}