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:tucil2ai.Functions.java

public void loadFile() {
    try {//from   w ww.  ja v  a 2s  . c  o m
        ConverterUtils.DataSource source = new ConverterUtils.DataSource("/data/data.arff");
        Instances data = source.getDataSet();
        if (data.classIndex() == -1)
            data.setClassIndex(data.numAttributes() - 1);
    } catch (Exception ex) {
        Logger.getLogger(Functions.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:xlong.urlclassify.others.SPegasos.java

License:Open Source License

/**
 * Method for building the classifier.//from  w  ww  .j  a va2s  .c  o  m
 * 
 * @param data the set of training instances.
 * @throws Exception if the classifier can't be built successfully.
 */
public void buildClassifier(Instances data) throws Exception {
    reset();

    // can classifier handle the data?
    getCapabilities().testWithFail(data);

    data = new Instances(data);
    data.deleteWithMissingClass();

    if (data.numInstances() > 0 && !m_dontReplaceMissing) {
        m_replaceMissing = new ReplaceMissingValues();
        m_replaceMissing.setInputFormat(data);
        data = Filter.useFilter(data, m_replaceMissing);
    }

    // check for only numeric attributes
    boolean onlyNumeric = true;
    for (int i = 0; i < data.numAttributes(); i++) {
        if (i != data.classIndex()) {
            if (!data.attribute(i).isNumeric()) {
                onlyNumeric = false;
                break;
            }
        }
    }

    if (!onlyNumeric) {
        m_nominalToBinary = new NominalToBinary();
        m_nominalToBinary.setInputFormat(data);
        data = Filter.useFilter(data, m_nominalToBinary);
    }

    if (!m_dontNormalize && data.numInstances() > 0) {

        m_normalize = new Normalize();
        m_normalize.setInputFormat(data);
        data = Filter.useFilter(data, m_normalize);
    }

    m_weights = new double[data.numAttributes() + 1];
    m_data = new Instances(data, 0);

    if (data.numInstances() > 0) {
        train(data);
    }
}