Example usage for weka.core Instances attribute

List of usage examples for weka.core Instances attribute

Introduction

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

Prototype

publicAttribute attribute(String name) 

Source Link

Document

Returns an attribute given its name.

Usage

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

License:Open Source License

/**
 * {@inheritDoc}//from   w  ww .  j a  va 2  s. c om
 */
@Override
public void update0(final Instances dataSet) throws Exception {
    if (this.mp != null)
        this.panel.remove(this.mp);

    if (this.cl != null)
        this.slider.removeChangeListener(cl);
    //if (this.cl!=null) this.slider.removeChangeListener(cl);

    this.cl = new ChangeListener() {
        @Override
        public void stateChanged(final ChangeEvent e) {
            if (!slider.getValueIsAdjusting()) {
                dtFactory = new J48DecisionTreeFactory(slider.getValue() / 100d, false);
                update(dataSet);
            }
        }
    };
    this.slider.addChangeListener(cl);

    final double frameWidth = this.panel.getSize().getWidth() * 0.95d;
    final double frameHeight = this.panel.getSize().getHeight() * 0.95d;

    final ListOrderedMap<JComponent, Integer> mapPanels = new ListOrderedMap<JComponent, Integer>();

    final String oldSelected;
    if (this.attrSelectionCombo.getSelectedItem() == null) {
        oldSelected = dataSet.classAttribute().name();
    } else {
        final Attribute oldAttr = dataSet.attribute(this.attrSelectionCombo.getSelectedItem().toString());
        if (oldAttr != null) {
            oldSelected = oldAttr.name();
        } else {
            oldSelected = dataSet.classAttribute().name();
        }
    }
    final int idx = dataSet.attribute(oldSelected).index();
    final Set<Object> presentValues = WekaDataStatsUtil.getNominalRepartition(dataSet, idx).keySet();
    for (final Object o : presentValues) {
        final Instances part = WekaDataProcessingUtil.filterDataSetOnNominalValue(dataSet, idx, o.toString());
        final DecisionTree dti = dtFactory.buildDecisionTree(part);

        final int ratio = 100 * part.numInstances() / dataSet.numInstances();
        final GraphView myGraph = DecisionTreeToGraphViewHelper.buildGraphView(dti, eventPublisher,
                commandDispatcher);
        myGraph.hideSharedLabel();
        myGraph.addMetaInfo("size=" + dti.getSize(), "");
        myGraph.addMetaInfo("depth=" + dti.getDepth(), "");
        myGraph.addMetaInfo("err=" + FormatterUtil.DECIMAL_FORMAT.format(100d * dti.getErrorRate()) + "%", "");

        final JButton openInEditorButton = new JButton("Edit");
        openInEditorButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                GraphUtil.importDecisionTreeInEditor(dtFactory, part, applicationContext, eventPublisher,
                        commandDispatcher);
            }
        });
        myGraph.addMetaInfoComponent(openInEditorButton);

        myGraph.fitGraphToSubPanel(frameWidth - 10 * presentValues.size(), frameHeight - 10, ratio);
        mapPanels.put((JComponent) myGraph, ratio);

    }
    this.mp = new MultiPanel(mapPanels, (int) frameWidth, (int) frameHeight,
            this.withWeightCheckBox.isSelected());

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

    if (this.attrSelectionCombo.getActionListeners().length > 0) {
        this.attrSelectionCombo.removeActionListener(attrSelectionComboListener);
    }
    if (this.withWeightCheckBox.getActionListeners().length > 0) {
        this.withWeightCheckBox.removeActionListener(attrSelectionComboListener);
    }

    this.attrSelectionCombo.removeAllItems();
    for (final Attribute attr : WekaDataStatsUtil.getNominalAttributesList(dataSet)) {
        this.attrSelectionCombo.addItem(attr.name());
    }
    this.attrSelectionCombo.setSelectedItem(oldSelected);

    this.attrSelectionComboListener = new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            update(dataSet);
        }
    };
    this.attrSelectionCombo.addActionListener(attrSelectionComboListener);
    this.withWeightCheckBox.addActionListener(attrSelectionComboListener);

}

From source file:lu.lippmann.cdb.datasetview.tasks.CompareAttributesTask.java

License:Open Source License

/**
 * {@inheritDoc}//from   ww w. j a  va2s .  c  om
 */
@Override
Instances process0(final Instances dataSet) throws Exception {

    final String s1 = (String) JOptionPane.showInputDialog(null, "Select an attribute:\n",
            "Attribute selection", JOptionPane.PLAIN_MESSAGE, null,
            WekaDataStatsUtil.getNominalAttributesNames(dataSet).toArray(), "");

    final Attribute attr1 = dataSet.attribute(s1);
    final WekaClusteringResult wcr1 = WekaMachineLearningUtil
            .computeClustersResultFromNominalAttributeValues(dataSet, attr1.index());

    final String s2 = (String) JOptionPane.showInputDialog(null, "Select an attribute:\n",
            "Attribute selection", JOptionPane.PLAIN_MESSAGE, null,
            WekaDataStatsUtil.getNominalAttributesNames(dataSet).toArray(), "");

    final Attribute attr2 = dataSet.attribute(s2);
    final WekaClusteringResult wcr2 = WekaMachineLearningUtil
            .computeClustersResultFromNominalAttributeValues(dataSet, attr2.index());

    JOptionPane.showMessageDialog(null, new WekaClusteringResultsComparison(wcr1, wcr2));

    return null;
}

From source file:lu.lippmann.cdb.datasetview.tasks.DuplicateAttributeTask.java

License:Open Source License

/**
 * {@inheritDoc}/*from www.  ja v  a  2s  .  c om*/
 */
@Override
Instances process0(final Instances dataSet) throws Exception {

    final String s = (String) JOptionPane.showInputDialog(null, "Select an attribute:\n", "Attribute selection",
            JOptionPane.PLAIN_MESSAGE, null, WekaDataStatsUtil.getAttributeNames(dataSet).toArray(), "");

    if (s != null)
        return WekaDataProcessingUtil.buildDataSetWithDuplicateAttribute(dataSet, dataSet.attribute(s).index());
    else
        return dataSet;
}

From source file:lu.lippmann.cdb.datasetview.tasks.FillShortGapsInTimeSeriesByInterpolationTask.java

License:Open Source License

/**
 * {@inheritDoc}/*from ww  w  .j a  v  a2 s  . c  o m*/
 */
@Override
Instances process0(final Instances dataSet) throws Exception {
    final int dateIdx = WekaDataStatsUtil.getFirstDateAttributeIdx(dataSet);
    if (dateIdx == -1) {
        throw new Exception("No date attribute in this dataset!");
    }

    Instances newds = dataSet;
    for (int i = 0; i < dataSet.numAttributes(); i++) {
        System.out.println("Fill gaps for " + i);

        if (i == dateIdx)
            continue;
        final java.util.List<double[]> gaps = WekaTimeSeriesUtil.findGaps(newds, i);
        for (final double[] dd : gaps) {
            if (dd[3] > 1)
                continue;
            newds = WekaTimeSeriesUtil.fillGapWithInterpolation(newds, newds.attribute(i), (int) dd[2],
                    (int) dd[3], 1);
        }
    }

    return newds;
}

From source file:lu.lippmann.cdb.datasetview.tasks.SetAttributeAsTimestampTask.java

License:Open Source License

/**
 * {@inheritDoc}/* w  ww  .  ja v  a2s.  c o m*/
 */
@Override
Instances process0(final Instances dataSet) throws Exception {
    final String s = (String) JOptionPane.showInputDialog(null, "Select an attribute:\n", "Attribute selection",
            JOptionPane.PLAIN_MESSAGE, null, WekaDataStatsUtil.getNumericAttributesNames(dataSet).toArray(),
            "");

    if (s != null) {
        final Instances newds = new Instances(dataSet);
        newds.insertAttributeAt(new Attribute("date", "dd-MM-yyyy HH:mm"), newds.numAttributes());

        final int sidx = newds.attribute(s).index();

        for (int i = 0; i < newds.numInstances(); i++) {
            newds.instance(i).setValue(newds.numAttributes() - 1, newds.instance(i).value(sidx));
        }

        return newds;
    } else
        return dataSet;
}

From source file:lu.lippmann.cdb.datasetview.tasks.SortAttributesTask.java

License:Open Source License

/**
 * {@inheritDoc}/*  w w  w.  jav a2 s. c o  m*/
 */
@Override
Instances process0(final Instances dataSet) throws Exception {

    final String s = (String) JOptionPane.showInputDialog(null, "Select an attribute:\n", "Attribute selection",
            JOptionPane.PLAIN_MESSAGE, null, WekaDataStatsUtil.getAttributeNames(dataSet).toArray(), "");

    if (s != null)
        return WekaDataProcessingUtil.buildDataSetSortedByAttribute(dataSet, dataSet.attribute(s).index());
    else
        return dataSet;
}

From source file:lu.lippmann.cdb.datasetview.tasks.SortInstancesTask.java

License:Open Source License

/**
 * {@inheritDoc}//from w  ww.  j a  v  a 2s .c o  m
 */
@Override
Instances process0(final Instances dataSet) throws Exception {

    final String s = (String) JOptionPane.showInputDialog(null, "Select an attribute:\n", "Attribute selection",
            JOptionPane.PLAIN_MESSAGE, null, WekaDataStatsUtil.getAttributeNames(dataSet).toArray(), "");

    if (s != null)
        dataSet.sort(dataSet.attribute(s));
    return dataSet;
}

From source file:lu.lippmann.cdb.datasetview.tasks.SortTimeSeriesTask.java

License:Open Source License

/**
 * {@inheritDoc}/*  w  w w . j av  a  2 s  . c  om*/
 */
@Override
Instances process0(final Instances dataSet) throws Exception {
    if (WekaDataStatsUtil.getFirstDateAttributeIdx(dataSet) == -1) {
        throw new Exception("Only usable with time series!");
    }

    final String s = (String) JOptionPane.showInputDialog(null, "Select a time serie:\n",
            "Time serie selection", JOptionPane.PLAIN_MESSAGE, null,
            WekaDataStatsUtil.getAttributeNames(dataSet).toArray(), "");

    if (s != null)
        return WekaDataProcessingUtil.buildDataSetSortedByAttribute(dataSet, dataSet.attribute(s).index());
    else
        return dataSet;
}

From source file:lu.lippmann.cdb.datasetview.tasks.SupervisedFeatureSelectionWithDecisionTreeTask.java

License:Open Source License

/**
 * {@inheritDoc}/*from   w  w w  .j  a  v  a2s  .  com*/
 */
@Override
Instances process0(final Instances dataSet) throws Exception {
    if (dataSet.classIndex() == -1)
        throw new Exception("Need a selected class!");

    final DecisionTree dt = dtFactory.buildCachedDecisionTree(dataSet);
    final Set<Integer> attrToKeep = new HashSet<Integer>();

    for (final CNode cn : dt.getGraphWithOperations().getVertices()) {
        final Attribute attribute = dataSet.attribute(cn.getName());
        if (attribute != null)
            attrToKeep.add(attribute.index());
    }
    attrToKeep.add(dataSet.classIndex());

    final int size = attrToKeep.size();
    final int[] array = new int[size];
    int i = 0;
    final Iterator<Integer> iter = attrToKeep.iterator();
    while (iter.hasNext()) {
        array[i] = iter.next().intValue();
        i++;
    }

    return WekaDataProcessingUtil.buildFilteredByAttributesDataSet(dataSet, array);
}

From source file:lu.lippmann.cdb.datasetview.tasks.UnsupervisedFeatureSelectionTask.java

License:Open Source License

/**
 * {@inheritDoc}// ww w  .  ja va  2 s.c  o  m
 */
@Override
Instances process0(final Instances dataSet) throws Exception {
    final int k;
    if (this.ratio == -1)
        k = getFeaturesCountFromInput(null, dataSet.numAttributes());
    else
        k = (int) Math.round(this.ratio * dataSet.numAttributes());

    final List<Integer> attrToKeep = WekaMachineLearningUtil.computeUnsupervisedFeaturesSelection(dataSet, k);
    if (!attrToKeep.contains(dataSet.classIndex()))
        attrToKeep.add(dataSet.classIndex());
    final int[] array = ArraysUtil.transform(attrToKeep);

    System.out.println("unsupervised fs -> before=" + dataSet.numAttributes() + " after=" + array.length);

    final Instances newds = WekaDataProcessingUtil.buildFilteredByAttributesDataSet(dataSet, array);
    final Attribute clsAttr = newds.attribute(dataSet.classAttribute().name());
    System.out.println(clsAttr + " " + dataSet.classAttribute().name());
    newds.setClass(clsAttr);
    return newds;
}