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:adams.flow.transformer.WekaAttributeSelection.java

License:Open Source License

/**
 * Executes the flow item.//from w w  w  .jav a 2  s  . co  m
 *
 * @return      null if everything is fine, otherwise error message
 */
@Override
protected String doExecute() {
    String result;
    Instances data;
    Instances reduced;
    Instances transformed;
    AttributeSelection eval;
    boolean crossValidate;
    int fold;
    Instances train;
    WekaAttributeSelectionContainer cont;
    SpreadSheet stats;
    int i;
    Row row;
    int[] selected;
    double[][] ranked;
    Range range;
    String rangeStr;
    boolean useReduced;

    result = null;

    try {
        if (m_InputToken.getPayload() instanceof Instances)
            data = (Instances) m_InputToken.getPayload();
        else
            data = (Instances) ((WekaTrainTestSetContainer) m_InputToken.getPayload())
                    .getValue(WekaTrainTestSetContainer.VALUE_TRAIN);

        if (result == null) {
            crossValidate = (m_Folds >= 2);

            // setup evaluation
            eval = new AttributeSelection();
            eval.setEvaluator(m_Evaluator);
            eval.setSearch(m_Search);
            eval.setFolds(m_Folds);
            eval.setSeed((int) m_Seed);
            eval.setXval(crossValidate);

            // select attributes
            if (crossValidate) {
                Random random = new Random(m_Seed);
                data = new Instances(data);
                data.randomize(random);
                if ((data.classIndex() > -1) && data.classAttribute().isNominal()) {
                    if (isLoggingEnabled())
                        getLogger().info("Stratifying instances...");
                    data.stratify(m_Folds);
                }
                for (fold = 0; fold < m_Folds; fold++) {
                    if (isLoggingEnabled())
                        getLogger().info("Creating splits for fold " + (fold + 1) + "...");
                    train = data.trainCV(m_Folds, fold, random);
                    if (isLoggingEnabled())
                        getLogger().info("Selecting attributes using all but fold " + (fold + 1) + "...");
                    eval.selectAttributesCVSplit(train);
                }
            } else {
                eval.SelectAttributes(data);
            }

            // generate reduced/transformed dataset
            reduced = null;
            transformed = null;
            if (!crossValidate) {
                reduced = eval.reduceDimensionality(data);
                if (m_Evaluator instanceof AttributeTransformer)
                    transformed = ((AttributeTransformer) m_Evaluator).transformedData(data);
            }

            // generated stats
            stats = null;
            if (!crossValidate) {
                stats = new DefaultSpreadSheet();
                row = stats.getHeaderRow();

                useReduced = false;
                if (m_Search instanceof RankedOutputSearch) {
                    i = reduced.numAttributes();
                    if (reduced.classIndex() > -1)
                        i--;
                    ranked = eval.rankedAttributes();
                    useReduced = (ranked.length == i);
                }

                if (useReduced) {
                    for (i = 0; i < reduced.numAttributes(); i++)
                        row.addCell("" + i).setContent(reduced.attribute(i).name());
                    row = stats.addRow();
                    for (i = 0; i < reduced.numAttributes(); i++)
                        row.addCell(i).setContent(0.0);
                } else {
                    for (i = 0; i < data.numAttributes(); i++)
                        row.addCell("" + i).setContent(data.attribute(i).name());
                    row = stats.addRow();
                    for (i = 0; i < data.numAttributes(); i++)
                        row.addCell(i).setContent(0.0);
                }

                if (m_Search instanceof RankedOutputSearch) {
                    ranked = eval.rankedAttributes();
                    for (i = 0; i < ranked.length; i++)
                        row.getCell((int) ranked[i][0]).setContent(ranked[i][1]);
                } else {
                    selected = eval.selectedAttributes();
                    for (i = 0; i < selected.length; i++)
                        row.getCell(selected[i]).setContent(1.0);
                }
            }

            // selected attributes
            rangeStr = null;
            if (!crossValidate) {
                range = new Range();
                range.setIndices(eval.selectedAttributes());
                rangeStr = range.getRange();
            }

            // setup container
            if (crossValidate)
                cont = new WekaAttributeSelectionContainer(data, reduced, transformed, eval, m_Seed, m_Folds);
            else
                cont = new WekaAttributeSelectionContainer(data, reduced, transformed, eval, stats, rangeStr);
            m_OutputToken = new Token(cont);
        }
    } catch (Exception e) {
        m_OutputToken = null;
        result = handleException("Failed to process data:", e);
    }

    return result;
}

From source file:adams.flow.transformer.WekaChooseAttributes.java

License:Open Source License

/**
 * Prompts the user to select attributes.
 * /* w ww  .  j a va 2 s  . c om*/
 * @param inst   the data to present
 * @param preSelected   the indices of the attributes to use by default
 * @return      the list of selected attributes to keep, null if cancelled
 */
protected List<Integer> selectAttributes(Instances inst, List<Integer> preSelected) {
    ArrayList<Integer> result;
    DefaultTableModel model;
    BaseTable table;
    String[][] names;
    int i;
    int n;
    ApprovalDialog dialog;
    JPanel panelAll;
    JPanel panelOptions;
    BaseCheckBox checkBoxInvert;
    BaseTextArea textMessage;
    Range range;
    int[][] segments;
    int numAtts;
    String msg;

    result = new ArrayList<>();

    msg = getVariables().expand(m_Message);
    numAtts = inst.numAttributes();
    if (inst.classIndex() > -1)
        numAtts--;
    names = new String[numAtts][1];
    n = 0;
    for (i = 0; i < inst.numAttributes(); i++) {
        if (inst.classIndex() == i)
            continue;
        names[n][0] = inst.attribute(i).name();
        n++;
    }
    model = new DefaultTableModel(names, new String[] { "Attribute" });

    range = new Range();
    range.setMax(numAtts);
    range.setIndices(Utils.toIntArray(preSelected));
    segments = range.getIntSegments();
    table = new BaseTable(model);
    table.setAutoResizeMode(BaseTable.AUTO_RESIZE_OFF);
    table.setOptimalColumnWidth();
    table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
    for (int[] segment : segments)
        table.getSelectionModel().addSelectionInterval(segment[0], segment[1]);

    panelAll = new JPanel(new BorderLayout());
    panelAll.add(new BaseScrollPane(table), BorderLayout.CENTER);
    if (msg.trim().length() > 0) {
        textMessage = new BaseTextArea(msg.split("\n").length + 1, 40);
        textMessage.setText(msg);
        panelAll.add(new BaseScrollPane(textMessage), BorderLayout.NORTH);
    }
    panelOptions = new JPanel(new FlowLayout(FlowLayout.LEFT));
    checkBoxInvert = new BaseCheckBox("Remove selected attributes rather than keep them");
    panelOptions.add(checkBoxInvert);
    panelAll.add(panelOptions, BorderLayout.SOUTH);
    dialog = new ApprovalDialog(null, ModalityType.DOCUMENT_MODAL);
    dialog.setTitle("Choose attributes");
    dialog.getContentPane().add(panelAll, BorderLayout.CENTER);
    registerWindow(dialog, dialog.getTitle());
    dialog.pack();
    dialog.setLocationRelativeTo(getActualParentComponent());
    dialog.setVisible(true);
    deregisterWindow(dialog);

    if (dialog.getOption() != ApprovalDialog.APPROVE_OPTION)
        return null;

    if (checkBoxInvert.isSelected()) {
        range.setIndices(table.getSelectedRows());
        range.setInverted(true);
        result.addAll(Utils.toList(range.getIntIndices()));
    } else {
        result.addAll(Utils.toList(table.getSelectedRows()));
    }

    return result;
}

From source file:adams.flow.transformer.WekaChooseAttributes.java

License:Open Source License

/**
 * Returns the pre-selected indices.// w  w w  .  j  av a2 s .  c  om
 * 
 * @param inst   the dataset to work on
 * @return      the indices
 */
protected List<Integer> getPreSelectedIndices(Instances inst) {
    List<Integer> result;
    int i;

    result = new ArrayList<>();
    for (i = 0; i < inst.numAttributes(); i++) {
        if (inst.classIndex() == i)
            continue;
        if (m_PreSelection.isMatch(inst.attribute(i).name()))
            result.add(i);
    }

    return result;
}

From source file:adams.flow.transformer.WekaClassSelector.java

License:Open Source License

/**
 * Executes the flow item./*from ww  w .  ja  va2s .c o m*/
 *
 * @return      null if everything is fine, otherwise error message
 */
@Override
protected String doExecute() {
    String result;
    Object o;
    weka.core.Instances inst;
    boolean isInstances;
    List<Attribute> atts;
    int i;

    result = null;

    // dataset
    o = m_InputToken.getPayload();
    isInstances = false;
    inst = null;
    if (o instanceof weka.core.Instances) {
        inst = (weka.core.Instances) o;
        inst = new weka.core.Instances(inst);
        isInstances = true;
    } else if (o instanceof adams.data.instance.Instance) {
        inst = ((adams.data.instance.Instance) o).getDatasetHeader();
    } else if (o instanceof weka.core.Instance) {
        inst = ((weka.core.Instance) o).dataset();
    } else {
        result = "Cannot handle object of type " + o.getClass().getName() + "!";
    }

    if (result == null) {
        if (m_Unset) {
            inst.setClassIndex(-1);
        } else {
            // determine the attributes that fit the regular expression
            atts = new ArrayList<Attribute>();
            for (i = 0; i < inst.numAttributes(); i++) {
                if (m_RegexName.isEmpty() || m_RegexName.isMatch(inst.attribute(i).name()))
                    atts.add(inst.attribute(i));
            }

            // class index
            m_ClassIndex.setMax(atts.size());
            if (m_Override || (inst.classIndex() == -1))
                inst.setClassIndex(atts.get(m_ClassIndex.getIntIndex()).index());
        }

        // output
        if (isInstances)
            m_OutputToken = new Token(inst);
        else
            m_OutputToken = new Token(o);

        updateProvenance(m_OutputToken);
    }

    return result;
}

From source file:adams.flow.transformer.WekaInstancesInfo.java

License:Open Source License

/**
 * Executes the flow item./*from   ww  w  .  j av a 2  s. c o m*/
 *
 * @return      null if everything is fine, otherwise error message
 */
@Override
protected String doExecute() {
    String result;
    Instances inst;
    int index;
    int labelIndex;
    double[] dist;
    Enumeration enm;
    int i;

    result = null;

    if (m_InputToken.getPayload() instanceof Instance)
        inst = ((Instance) m_InputToken.getPayload()).dataset();
    else
        inst = (Instances) m_InputToken.getPayload();
    m_AttributeIndex.setData(inst);
    index = m_AttributeIndex.getIntIndex();

    m_Queue.clear();

    switch (m_Type) {
    case FULL:
        m_Queue.add(inst.toSummaryString());
        break;

    case FULL_ATTRIBUTE:
        m_Queue.add(getAttributeStats(inst, index));
        break;

    case FULL_CLASS:
        if (inst.classIndex() > -1)
            m_Queue.add(getAttributeStats(inst, inst.classIndex()));
        break;

    case HEADER:
        m_Queue.add(new Instances(inst, 0).toString());
        break;

    case RELATION_NAME:
        m_Queue.add(inst.relationName());
        break;

    case ATTRIBUTE_NAME:
        if (index != -1)
            m_Queue.add(inst.attribute(index).name());
        break;

    case ATTRIBUTE_NAMES:
        for (i = 0; i < inst.numAttributes(); i++)
            m_Queue.add(inst.attribute(i).name());
        break;

    case LABELS:
        if (index != -1) {
            enm = inst.attribute(index).enumerateValues();
            while (enm.hasMoreElements())
                m_Queue.add(enm.nextElement());
        }
        break;

    case CLASS_LABELS:
        if (inst.classIndex() > -1) {
            enm = inst.classAttribute().enumerateValues();
            while (enm.hasMoreElements())
                m_Queue.add(enm.nextElement());
        }
        break;

    case LABEL_COUNT:
        if (index > -1) {
            m_LabelIndex.setData(inst.attribute(index));
            labelIndex = m_LabelIndex.getIntIndex();
            m_Queue.add(inst.attributeStats(index).nominalCounts[labelIndex]);
        }
        break;

    case LABEL_COUNTS:
        if (index > -1)
            m_Queue.add(StatUtils.toNumberArray(inst.attributeStats(index).nominalCounts));
        break;

    case LABEL_DISTRIBUTION:
        if (index > -1) {
            dist = new double[inst.attributeStats(index).nominalCounts.length];
            for (i = 0; i < dist.length; i++)
                dist[i] = inst.attributeStats(index).nominalCounts[i];
            Utils.normalize(dist);
            m_Queue.add(StatUtils.toNumberArray(dist));
        }
        break;

    case CLASS_LABEL_COUNT:
        if (inst.classIndex() > -1) {
            m_LabelIndex.setData(inst.classAttribute());
            labelIndex = m_LabelIndex.getIntIndex();
            m_Queue.add(inst.attributeStats(inst.classIndex()).nominalCounts[labelIndex]);
        }
        break;

    case CLASS_LABEL_COUNTS:
        if (inst.classIndex() > -1)
            m_Queue.add(StatUtils.toNumberArray(inst.attributeStats(inst.classIndex()).nominalCounts));
        break;

    case CLASS_LABEL_DISTRIBUTION:
        if (inst.classIndex() > -1) {
            dist = new double[inst.attributeStats(inst.classIndex()).nominalCounts.length];
            for (i = 0; i < dist.length; i++)
                dist[i] = inst.attributeStats(inst.classIndex()).nominalCounts[i];
            Utils.normalize(dist);
            m_Queue.add(StatUtils.toNumberArray(dist));
        }
        break;

    case NUM_ATTRIBUTES:
        m_Queue.add(inst.numAttributes());
        break;

    case NUM_INSTANCES:
        m_Queue.add(inst.numInstances());
        break;

    case NUM_CLASS_LABELS:
        if ((inst.classIndex() != -1) && inst.classAttribute().isNominal())
            m_Queue.add(inst.classAttribute().numValues());
        break;

    case NUM_LABELS:
        if ((index != -1) && inst.attribute(index).isNominal())
            m_Queue.add(inst.attribute(index).numValues());
        break;

    case NUM_DISTINCT_VALUES:
        if (index != -1)
            m_Queue.add(inst.attributeStats(index).distinctCount);
        break;

    case NUM_UNIQUE_VALUES:
        if (index != -1)
            m_Queue.add(inst.attributeStats(index).uniqueCount);
        break;

    case NUM_MISSING_VALUES:
        if (index != -1)
            m_Queue.add(inst.attributeStats(index).missingCount);
        break;

    case MIN:
        if ((index != -1) && inst.attribute(index).isNumeric())
            m_Queue.add(inst.attributeStats(index).numericStats.min);
        break;

    case MAX:
        if ((index != -1) && inst.attribute(index).isNumeric())
            m_Queue.add(inst.attributeStats(index).numericStats.max);
        break;

    case MEAN:
        if ((index != -1) && inst.attribute(index).isNumeric())
            m_Queue.add(inst.attributeStats(index).numericStats.mean);
        break;

    case STDEV:
        if ((index != -1) && inst.attribute(index).isNumeric())
            m_Queue.add(inst.attributeStats(index).numericStats.stdDev);
        break;

    case ATTRIBUTE_TYPE:
        if (index != -1)
            m_Queue.add(Attribute.typeToString(inst.attribute(index)));
        break;

    case CLASS_TYPE:
        if (inst.classIndex() != -1)
            m_Queue.add(Attribute.typeToString(inst.classAttribute()));
        break;

    default:
        result = "Unhandled info type: " + m_Type;
    }

    return result;
}

From source file:adams.flow.transformer.WekaInstancesMerge.java

License:Open Source License

/**
 * Prefixes the attributes./*w ww . j  a  v  a2 s.  c o  m*/
 *
 * @param index   the index of the dataset
 * @param inst   the data to process
 * @return      the processed data
 */
protected Instances prefixAttributes(Instances inst, int index) {
    Instances result;
    String prefix;
    ArrayList<Attribute> atts;
    int i;

    prefix = createPrefix(inst, index);

    // header
    atts = new ArrayList<>();
    for (i = 0; i < inst.numAttributes(); i++)
        atts.add(inst.attribute(i).copy(prefix + inst.attribute(i).name()));

    // data
    result = new Instances(inst.relationName(), atts, inst.numInstances());
    result.setClassIndex(inst.classIndex());
    for (i = 0; i < inst.numInstances(); i++)
        result.add((Instance) inst.instance(i).copy());

    return result;
}

From source file:adams.gui.visualization.debug.inspectionhandler.WekaInstances.java

License:Open Source License

/**
 * Returns further inspection values.//from w  w w. ja v  a  2  s. c o  m
 *
 * @param obj      the object to further inspect
 * @return      the named inspected values
 */
@Override
public Hashtable<String, Object> inspect(Object obj) {
    Hashtable<String, Object> result;
    Instances data;
    Instance inst;

    result = new Hashtable<String, Object>();

    if (obj instanceof Instances) {
        data = (Instances) obj;
        inst = null;
    } else {
        inst = (Instance) obj;
        data = inst.dataset();
    }

    result.put("relation", data.relationName());
    result.put("num attributes", data.numAttributes());
    result.put("class attribute", (data.classIndex() == -1) ? "-none-"
            : ((data.classIndex() + 1) + " (" + data.classAttribute().name() + ")"));
    if (inst == null) {
        result.put("num instances", data.numInstances());
        result.put("instances", data.toArray());
    }

    return result;
}

From source file:adams.gui.visualization.debug.objectrenderer.WekaInstancesRenderer.java

License:Open Source License

/**
 * Performs the actual rendering./*from www  .  ja va2 s.  co m*/
 *
 * @param obj      the object to render
 * @param panel   the panel to render into
 * @return      null if successful, otherwise error message
 */
@Override
protected String doRender(Object obj, JPanel panel) {
    Instance inst;
    Instances data;
    InstancesTable table;
    InstancesTableModel model;
    BaseScrollPane scrollPane;
    PlainTextRenderer plain;
    SpreadSheet sheet;
    Row row;
    int i;
    SpreadSheetRenderer sprenderer;

    if (obj instanceof Instances) {
        data = (Instances) obj;
        if (data.numInstances() == 0) {
            sheet = new DefaultSpreadSheet();
            row = sheet.getHeaderRow();
            row.addCell("I").setContentAsString("Index");
            row.addCell("N").setContentAsString("Name");
            row.addCell("T").setContentAsString("Type");
            row.addCell("C").setContentAsString("Class");
            for (i = 0; i < data.numAttributes(); i++) {
                row = sheet.addRow();
                row.addCell("I").setContent(i + 1);
                row.addCell("N").setContentAsString(data.attribute(i).name());
                row.addCell("T").setContentAsString(Attribute.typeToString(data.attribute(i)));
                row.addCell("C").setContent((i == data.classIndex()) ? "true" : "");
            }
            sprenderer = new SpreadSheetRenderer();
            sprenderer.render(sheet, panel);
        } else {
            model = new InstancesTableModel(data);
            model.setShowAttributeIndex(true);
            table = new InstancesTable(model);
            scrollPane = new BaseScrollPane(table);
            panel.add(scrollPane, BorderLayout.CENTER);
        }
    } else {
        inst = (Instance) obj;
        if (inst.dataset() != null) {
            data = new Instances(inst.dataset(), 0);
            data.add((Instance) inst.copy());
            table = new InstancesTable(data);
            scrollPane = new BaseScrollPane(table);
            panel.add(scrollPane, BorderLayout.CENTER);
        } else {
            plain = new PlainTextRenderer();
            plain.render(obj, panel);
        }
    }

    return null;
}

From source file:adams.ml.data.WekaConverter.java

License:Open Source License

/**
 * Converts a Weka Instances object into an ADAMS Dataset.
 *
 * @param data   the data to convert//from ww  w  . j a  v  a 2 s. co  m
 * @return      the generated data
 * @throws Exception   if conversion fail
 */
public static Dataset toDataset(Instances data) throws Exception {
    Dataset result;
    WekaInstancesToSpreadSheet conv;
    String msg;

    // TODO just use a view?

    conv = new WekaInstancesToSpreadSheet();
    conv.setSpreadSheetType(new DefaultDataset());
    msg = conv.convert();
    if (msg != null)
        throw new Exception("Failed to convert Instances to Dataset: " + msg);

    result = (Dataset) conv.getOutput();
    if (data.classIndex() > -1)
        result.setClassAttribute(data.classIndex(), true);

    return result;
}

From source file:adams.opt.cso.HermioneSimple.java

License:Open Source License

/**
 * For testing only./* ww  w . ja v  a2  s.c o  m*/
 *
 * @param args   the dataset to use
 * @throws Exception   if something fails
 */
public static void main(String[] args) throws Exception {
    Environment.setEnvironmentClass(Environment.class);
    Instances data = DataSource.read(args[0]);
    if (data.classIndex() == -1)
        data.setClassIndex(data.numAttributes() - 1);
    MaxIterationsWithoutImprovement stopping = new MaxIterationsWithoutImprovement();
    stopping.setNumIterations(2);
    stopping.setMinimumImprovement(0.001);
    stopping.setLoggingLevel(LoggingLevel.INFO);
    HermioneSimple simple = new HermioneSimple();
    simple.setEvalParallel(true);
    simple.setMeasure(Measure.CC);
    simple.setStopping(stopping);
    simple.setLoggingLevel(LoggingLevel.INFO);
    simple.setInstances(data);
    /*
    simple.setClassifier(new GPD());
    simple.setHandlers(new AbstractCatSwarmOptimizationDiscoveryHandler[]{
      new GPDGamma(),
      new GPDNoise(),
    });
    */
    LinearRegressionJ cls = new LinearRegressionJ();
    cls.setEliminateColinearAttributes(false);
    cls.setAttributeSelectionMethod(
            new SelectedTag(LinearRegressionJ.SELECTION_NONE, LinearRegressionJ.TAGS_SELECTION));
    simple.setClassifier(new LinearRegressionJ());
    GenericDouble ridge = new GenericDouble();
    ridge.setClassname(new BaseClassname(cls.getClass()));
    ridge.setProperty("ridge");
    ridge.setMinimum(1e-8);
    ridge.setMaximum(1);
    simple.setHandlers(new AbstractCatSwarmOptimizationDiscoveryHandler[] { ridge, });
    DoubleMatrix best = simple.run();
    System.out.println(best);
}