Example usage for weka.core Instances attributeToDoubleArray

List of usage examples for weka.core Instances attributeToDoubleArray

Introduction

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

Prototype


publicdouble[] attributeToDoubleArray(int index) 

Source Link

Document

Gets the value of all instances in this dataset for a particular attribute.

Usage

From source file:adams.data.instancesanalysis.pls.AbstractMultiClassPLS.java

License:Open Source License

/**
 * Preprocesses the data.//w  w w.java 2s.c o  m
 *
 * @param instances the data to process
 * @return the preprocessed data
 */
protected Instances preTransform(Instances instances, Map<String, Object> params) throws Exception {
    Map<Integer, double[]> classValues;
    int i;
    int index;

    switch (m_PredictionType) {
    case ALL:
        classValues = null;
        break;
    default:
        classValues = new HashMap<>();
        for (i = 0; i < m_ClassAttributeIndices.size(); i++) {
            index = m_ClassAttributeIndices.get(i);
            classValues.put(index, instances.attributeToDoubleArray(index));
        }
    }

    if (classValues != null)
        params.put(PARAM_CLASSVALUES, classValues);

    if (!isInitialized()) {
        if (m_ReplaceMissing) {
            m_Missing = new ReplaceMissingValues();
            m_Missing.setInputFormat(instances);
        } else {
            m_Missing = null;
        }

        m_ClassMean = new HashMap<>();
        m_ClassStdDev = new HashMap<>();
        for (i = 0; i < m_ClassAttributeIndices.size(); i++) {
            index = m_ClassAttributeIndices.get(i);
            switch (m_PreprocessingType) {
            case CENTER:
                m_ClassMean.put(index, instances.meanOrMode(index));
                m_ClassStdDev.put(index, 1.0);
                m_Filter = new Center();
                ((Center) m_Filter).setIgnoreClass(true);
                break;
            case STANDARDIZE:
                m_ClassMean.put(index, instances.meanOrMode(index));
                m_ClassStdDev.put(index, StrictMath.sqrt(instances.variance(index)));
                m_Filter = new Standardize();
                ((Standardize) m_Filter).setIgnoreClass(true);
                break;
            case NONE:
                m_ClassMean.put(index, 0.0);
                m_ClassStdDev.put(index, 1.0);
                m_Filter = null;
                break;
            default:
                throw new IllegalStateException("Unhandled preprocessing type; " + m_PreprocessingType);
            }
        }
        if (m_Filter != null)
            m_Filter.setInputFormat(instances);
    }

    // filter data
    if (m_Missing != null)
        instances = Filter.useFilter(instances, m_Missing);
    if (m_Filter != null)
        instances = Filter.useFilter(instances, m_Filter);

    return instances;
}

From source file:adams.data.instancesanalysis.pls.AbstractSingleClassPLS.java

License:Open Source License

/**
 * Preprocesses the data./*from  w  w  w  .  jav  a  2s  . c om*/
 *
 * @param instances the data to process
 * @return the preprocessed data
 */
protected Instances preTransform(Instances instances, Map<String, Object> params) throws Exception {
    double[] classValues;

    switch (m_PredictionType) {
    case ALL:
        classValues = null;
        break;
    default:
        classValues = instances.attributeToDoubleArray(instances.classIndex());
    }

    if (classValues != null)
        params.put(PARAM_CLASSVALUES, classValues);

    if (!isInitialized()) {
        if (m_ReplaceMissing) {
            m_Missing = new ReplaceMissingValues();
            m_Missing.setInputFormat(instances);
        } else {
            m_Missing = null;
        }

        switch (m_PreprocessingType) {
        case CENTER:
            m_ClassMean = instances.meanOrMode(instances.classIndex());
            m_ClassStdDev = 1;
            m_Filter = new Center();
            ((Center) m_Filter).setIgnoreClass(true);
            break;
        case STANDARDIZE:
            m_ClassMean = instances.meanOrMode(instances.classIndex());
            m_ClassStdDev = StrictMath.sqrt(instances.variance(instances.classIndex()));
            m_Filter = new Standardize();
            ((Standardize) m_Filter).setIgnoreClass(true);
            break;
        case NONE:
            m_ClassMean = 0;
            m_ClassStdDev = 1;
            m_Filter = null;
            break;
        default:
            throw new IllegalStateException("Unhandled preprocessing type; " + m_PreprocessingType);
        }
        if (m_Filter != null)
            m_Filter.setInputFormat(instances);
    }

    // filter data
    if (m_Missing != null)
        instances = Filter.useFilter(instances, m_Missing);
    if (m_Filter != null)
        instances = Filter.useFilter(instances, m_Filter);

    return instances;
}

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

License:Open Source License

/**
 * Executes the flow item.//from  w  w w .  jav  a2 s.  c o  m
 *
 * @return      null if everything is fine, otherwise error message
 */
@Override
protected String doExecute() {
    String result;
    SpreadSheet sheet;
    Instances data;
    int i;
    int n;
    Index index;
    ArrayHistogram stat;

    result = null;
    m_Queue.clear();

    try {
        sheet = null;
        data = (Instances) m_InputToken.getPayload();
        stat = new ArrayHistogram();
        stat.setBinCalculation(m_BinCalculation);
        stat.setNumBins(m_NumBins);
        stat.setBinWidth(m_BinWidth);
        stat.setNormalize(m_Normalize);
        stat.setUseFixedMinMax(m_UseFixedMinMax);
        stat.setManualMin(m_ManualMin);
        stat.setManualMax(m_ManualMax);
        stat.setDisplayRanges(true);
        stat.setNumDecimals(m_NumDecimals);

        for (i = 0; i < m_Locations.length; i++) {
            switch (m_DataType) {
            case ROW_BY_INDEX:
                index = new Index(m_Locations[i].stringValue());
                index.setMax(data.numInstances());
                stat.add(StatUtils.toNumberArray(data.instance(index.getIntIndex()).toDoubleArray()));
                break;

            case COLUMN_BY_INDEX:
                index = new WekaAttributeIndex(m_Locations[i].stringValue());
                ((WekaAttributeIndex) index).setData(data);
                stat.add(StatUtils.toNumberArray(data.attributeToDoubleArray(index.getIntIndex())));
                break;

            case COLUMN_BY_REGEXP:
                for (n = 0; n < data.numAttributes(); n++) {
                    if (data.attribute(n).name().matches(m_Locations[i].stringValue())) {
                        stat.add(StatUtils.toNumberArray(data.attributeToDoubleArray(n)));
                        break;
                    }
                }
                break;

            default:
                throw new IllegalStateException("Unhandled data type: " + m_DataType);
            }
        }

        sheet = stat.calculate().toSpreadSheet();
    } catch (Exception e) {
        result = handleException("Error generating the ranges: ", e);
        sheet = null;
    }

    if (sheet != null) {
        for (i = 0; i < sheet.getColumnCount(); i++)
            m_Queue.add(sheet.getColumnName(i));
    }

    return result;
}

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

License:Open Source License

/**
 * Executes the flow item.//from www.j  av a 2  s  .  co  m
 *
 * @return      null if everything is fine, otherwise error message
 */
@Override
protected String doExecute() {
    String result;
    SpreadSheet sheet;
    Instances data;
    int i;
    int n;
    Index index;
    AbstractArrayStatistic stat;

    result = null;

    try {
        sheet = null;
        data = (Instances) m_InputToken.getPayload();
        stat = m_Statistic.shallowCopy(true);

        for (i = 0; i < m_Locations.length; i++) {
            switch (m_DataType) {
            case ROW_BY_INDEX:
                index = new Index(m_Locations[i].stringValue());
                index.setMax(data.numInstances());
                stat.add(StatUtils.toNumberArray(data.instance(index.getIntIndex()).toDoubleArray()));
                break;

            case COLUMN_BY_INDEX:
                index = new WekaAttributeIndex(m_Locations[i].stringValue());
                ((WekaAttributeIndex) index).setData(data);
                stat.add(StatUtils.toNumberArray(data.attributeToDoubleArray(index.getIntIndex())));
                break;

            case COLUMN_BY_REGEXP:
                for (n = 0; n < data.numAttributes(); n++) {
                    if (data.attribute(n).name().matches(m_Locations[i].stringValue())) {
                        stat.add(StatUtils.toNumberArray(data.attributeToDoubleArray(n)));
                        break;
                    }
                }
                break;

            default:
                throw new IllegalStateException("Unhandled data type: " + m_DataType);
            }
        }

        sheet = stat.calculate().toSpreadSheet();
    } catch (Exception e) {
        result = handleException("Error generating the statistic: ", e);
        sheet = null;
    }

    if (sheet != null)
        m_OutputToken = new Token(sheet);

    return result;
}

From source file:app.RunApp.java

License:Open Source License

/**
 * Action when table attributes clicked with left mouse
 * /*  w  ww  . ja v  a 2s . c o m*/
 * @param evt Event
 */
private void tableAttributesLeftMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tableAttributesLeftMouseClicked
    if (tabsAttributes.getSelectedIndex() == 0) {
        int selected = tableAttributesLeft.getSelectedRow();

        String attr = tableAttributesLeft.getValueAt(selected, 0).toString();

        Instances instances = dataset.getDataSet();

        Attribute currentAttr = instances.attribute(attr);

        double[] attributeValues = instances.attributeToDoubleArray(currentAttr.index());

        HeapSort.sort(attributeValues);

        attributesBoxDiagram2.getChart().setTitle(currentAttr.name());

        attributesBoxDiagram2.getChart().getXYPlot().clearAnnotations();

        ChartUtils.updateXYChart(attributesBoxDiagram2, HeapSort.getSortedArray());
    }
}

From source file:bme.mace.logicdomain.Evaluation.java

License:Open Source License

/**
 * Returns the area under ROC for those predictions that have been collected
 * in the evaluateClassifier(Classifier, Instances) method. Returns
 * Instance.missingValue() if the area is not available.
 * //w  w w . j a  v  a 2s.com
 * @param classIndex the index of the class to consider as "positive"
 * @return the area under the ROC curve or not a number
 */
public double areaUnderROC(int classIndex) {

    // Check if any predictions have been collected
    if (m_Predictions == null) {
        return Instance.missingValue();
    } else {
        ThresholdCurve tc = new ThresholdCurve();
        Instances result = tc.getCurve(m_Predictions, classIndex);
        double rocArea = ThresholdCurve.getROCArea(result);
        if (rocArea < 0.5) {
            rocArea = 1 - rocArea;
        }

        int tpIndex = result.attribute(ThresholdCurve.TP_RATE_NAME).index();
        int fpIndex = result.attribute(ThresholdCurve.FP_RATE_NAME).index();
        double[] tpRate = result.attributeToDoubleArray(tpIndex);
        double[] fpRate = result.attributeToDoubleArray(fpIndex);

        try {
            FileWriter fw;
            if (classIndex == 0)
                fw = new FileWriter("C://1.csv", true);
            else
                fw = new FileWriter("C://1.csv", true);

            BufferedWriter bw = new BufferedWriter(fw);
            int length = fpRate.length;
            for (int i = 255; i >= 0; i--) {

                int index = i * (length - 1) / 255;
                bw.write(fpRate[index] + ",");
            }
            bw.write("\n");
            for (int i = 255; i >= 0; i--) {
                int index = i * (length - 1) / 255;
                bw.write(tpRate[index] + ",");
            }
            bw.write("\n");

            bw.close();
            fw.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return rocArea;
    }
}

From source file:c4.pkg5crossv.Preview.java

/**
 * method to write arff data into s.o.p.
 * @throws IOException // ww w  . j a  v  a  2s.c  om
 */

public static void showData() throws IOException {

    String source = MainWindow.browsedFileLabel.getText();
    Instances data = DataLoad.loadData(source.replace("\\", "/"));
    data.setClassIndex(data.numAttributes() - 1);
    String field = "";
    for (int i = 0; i < data.numAttributes(); i++) {
        // Print the current attribute.
        System.out.print(data.attribute(i).name() + ": ");
        previewTextArea.append("\n" + data.attribute(i).name() + ": ");
        // Print the values associated with the current attribute.
        double[] values = data.attributeToDoubleArray(i);

        System.out.println(Arrays.toString(values));
        previewTextArea.append(Arrays.toString(values));
    }

}

From source file:cezeri.evaluater.FactoryEvaluation.java

public static Evaluation performCrossValidate(Classifier model, Instances datax, int folds, boolean show_text,
        boolean show_plot, TFigureAttribute attr) {
    Random rand = new Random(1);
    Instances randData = new Instances(datax);
    randData.randomize(rand);// w w w  .j a v a 2 s. c om
    if (randData.classAttribute().isNominal()) {
        randData.stratify(folds);
    }
    Evaluation eval = null;
    try {
        // perform cross-validation
        eval = new Evaluation(randData);
        //            double[] simulated = new double[0];
        //            double[] observed = new double[0];
        //            double[] sim = new double[0];
        //            double[] obs = new double[0];
        for (int n = 0; n < folds; n++) {
            Instances train = randData.trainCV(folds, n, rand);
            Instances validation = randData.testCV(folds, n);
            // build and evaluate classifier
            Classifier clsCopy = Classifier.makeCopy(model);
            clsCopy.buildClassifier(train);

            //                sim = eval.evaluateModel(clsCopy, validation);
            //                obs = validation.attributeToDoubleArray(validation.classIndex());
            //                if (show_plot) {
            //                    double[][] d = new double[2][sim.length];
            //                    d[0] = obs;
            //                    d[1] = sim;
            //                    CMatrix f1 = CMatrix.getInstance(d);
            //                    f1.transpose().plot(attr);
            //                }
            //                if (show_text) {
            //                    // output evaluation
            //                    System.out.println();
            //                    System.out.println("=== Setup for each Cross Validation fold===");
            //                    System.out.println("Classifier: " + model.getClass().getName() + " " + Utils.joinOptions(model.getOptions()));
            //                    System.out.println("Dataset: " + randData.relationName());
            //                    System.out.println("Folds: " + folds);
            //                    System.out.println("Seed: " + 1);
            //                    System.out.println();
            //                    System.out.println(eval.toSummaryString("=== " + folds + "-fold Cross-validation ===", false));
            //                }
            simulated = FactoryUtils.concatenate(simulated, eval.evaluateModel(clsCopy, validation));
            observed = FactoryUtils.concatenate(observed,
                    validation.attributeToDoubleArray(validation.classIndex()));
            //                simulated = FactoryUtils.mean(simulated,eval.evaluateModel(clsCopy, validation));
            //                observed = FactoryUtils.mean(observed,validation.attributeToDoubleArray(validation.classIndex()));
        }

        if (show_plot) {
            double[][] d = new double[2][simulated.length];
            d[0] = observed;
            d[1] = simulated;
            CMatrix f1 = CMatrix.getInstance(d);
            attr.figureCaption = "overall performance";
            f1.transpose().plot(attr);
        }
        if (show_text) {
            // output evaluation
            System.out.println();
            System.out.println("=== Setup for Overall Cross Validation===");
            System.out.println(
                    "Classifier: " + model.getClass().getName() + " " + Utils.joinOptions(model.getOptions()));
            System.out.println("Dataset: " + randData.relationName());
            System.out.println("Folds: " + folds);
            System.out.println("Seed: " + 1);
            System.out.println();
            System.out.println(eval.toSummaryString("=== " + folds + "-fold Cross-validation ===", false));
        }
    } catch (Exception ex) {
        Logger.getLogger(FactoryEvaluation.class.getName()).log(Level.SEVERE, null, ex);
    }
    return eval;
}

From source file:cezeri.evaluater.FactoryEvaluation.java

public static Evaluation performCrossValidateTestAlso(Classifier model, Instances datax, Instances test,
        boolean show_text, boolean show_plot) {
    TFigureAttribute attr = new TFigureAttribute();
    Random rand = new Random(1);
    Instances randData = new Instances(datax);
    randData.randomize(rand);//from   ww w.  j a  va2  s.co m

    Evaluation eval = null;
    int folds = randData.numInstances();
    try {
        eval = new Evaluation(randData);
        for (int n = 0; n < folds; n++) {
            //                randData.randomize(rand);
            //                Instances train = randData;                
            Instances train = randData.trainCV(folds, n);
            //                Instances train = randData.trainCV(folds, n, rand);
            Classifier clsCopy = Classifier.makeCopy(model);
            clsCopy.buildClassifier(train);
            Instances validation = randData.testCV(folds, n);
            //                Instances validation = test.testCV(test.numInstances(), n%test.numInstances());
            //                CMatrix.fromInstances(train).showDataGrid();
            //                CMatrix.fromInstances(validation).showDataGrid();

            simulated = FactoryUtils.concatenate(simulated, eval.evaluateModel(clsCopy, validation));
            observed = FactoryUtils.concatenate(observed,
                    validation.attributeToDoubleArray(validation.classIndex()));
        }

        if (show_plot) {
            double[][] d = new double[2][simulated.length];
            d[0] = observed;
            d[1] = simulated;
            CMatrix f1 = CMatrix.getInstance(d);
            attr.figureCaption = "overall performance";
            f1.transpose().plot(attr);
        }
        if (show_text) {
            // output evaluation
            System.out.println();
            System.out.println("=== Setup for Overall Cross Validation===");
            System.out.println(
                    "Classifier: " + model.getClass().getName() + " " + Utils.joinOptions(model.getOptions()));
            System.out.println("Dataset: " + randData.relationName());
            System.out.println("Folds: " + folds);
            System.out.println("Seed: " + 1);
            System.out.println();
            System.out.println(eval.toSummaryString("=== " + folds + "-fold Cross-validation ===", false));
        }
    } catch (Exception ex) {
        Logger.getLogger(FactoryEvaluation.class.getName()).log(Level.SEVERE, null, ex);
    }
    return eval;
}

From source file:cezeri.evaluater.FactoryEvaluation.java

private static Evaluation doTest(boolean isTrained, Classifier model, Instances train, Instances test,
        boolean show_text, boolean show_plot, TFigureAttribute attr) {
    Instances data = new Instances(train);
    Random rand = new Random(1);
    data.randomize(rand);/*from   ww  w.  j a v  a2 s  .  c o m*/
    Evaluation eval = null;
    try {
        //            double[] simulated = null;
        eval = new Evaluation(train);
        if (isTrained) {
            simulated = eval.evaluateModel(model, test);
        } else {
            Classifier clsCopy = Classifier.makeCopy(model);
            clsCopy.buildClassifier(train);
            simulated = eval.evaluateModel(clsCopy, test);
        }
        if (show_plot) {
            observed = test.attributeToDoubleArray(test.classIndex());
            double[][] d = new double[2][simulated.length];
            d[0] = observed;
            d[1] = simulated;
            CMatrix f1 = CMatrix.getInstance(d);
            String[] items = { "Observed", "Simulated" };
            attr.items = items;
            attr.figureCaption = model.getClass().getCanonicalName();
            f1.transpose().plot(attr);
            //                if (attr.axis[0].isEmpty() && attr.axis[1].isEmpty()) {
            //                    f1.transpose().plot(attr);
            //                } else {
            //                    f1.transpose().plot(model.getClass().getCanonicalName(), attr.items, attr.axis);
            //                }
        }
        if (show_text) {
            System.out.println();
            System.out.println("=== Setup for Test ===");
            System.out.println(
                    "Classifier: " + model.getClass().getName() + " " + Utils.joinOptions(model.getOptions()));
            System.out.println("Dataset: " + test.relationName());
            System.out.println();
            System.out.println(eval.toSummaryString("=== Test Results ===", false));
        }
    } catch (Exception ex) {
        Logger.getLogger(FactoryEvaluation.class.getName()).log(Level.SEVERE, null, ex);
    }
    return eval;
}