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:meka.gui.explorer.classify.ShowMacroCurve.java

License:Open Source License

/**
 * Creates a panel displaying the data.//from  w  w w.ja  v a  2s  .c o  m
 *
 * @param data          the plot data
 * @return              the panel
 * @throws Exception    if plot generation fails
 */
protected VisualizePanel createPanel(Instances data) throws Exception {
    VisualizePanel result = new ThresholdVisualizePanel();
    PlotData2D plot = new PlotData2D(data);
    plot.setPlotName("Macro-averaged Performance");
    plot.m_displayAllPoints = true;
    boolean[] connectPoints = new boolean[data.numInstances()];
    for (int cp = 1; cp < connectPoints.length; cp++)
        connectPoints[cp] = true;
    plot.setConnectPoints(connectPoints);
    result.addPlot(plot);
    if (data.attribute(SAMPLES) != null)
        result.setXIndex(data.attribute(SAMPLES).index());
    if (data.attribute(ACCURACY) != null)
        result.setYIndex(data.attribute(ACCURACY).index());
    return result;
}

From source file:meka.gui.explorer.classify.ShowMicroCurve.java

License:Open Source License

/**
 * Creates a panel displaying the data./*from  w  ww. j a  va2s.c om*/
 *
 * @param data          the plot data
 * @return              the panel
 * @throws Exception    if plot generation fails
 */
protected VisualizePanel createPanel(Instances data) throws Exception {
    VisualizePanel result = new ThresholdVisualizePanel();
    PlotData2D plot = new PlotData2D(data);
    plot.setPlotName("Micro-averaged Performance");
    plot.m_displayAllPoints = true;
    boolean[] connectPoints = new boolean[data.numInstances()];
    for (int cp = 1; cp < connectPoints.length; cp++)
        connectPoints[cp] = true;
    plot.setConnectPoints(connectPoints);
    result.addPlot(plot);
    if (data.attribute(SAMPLES) != null)
        result.setXIndex(data.attribute(SAMPLES).index());
    if (data.attribute(ACCURACY) != null)
        result.setYIndex(data.attribute(ACCURACY).index());
    return result;
}

From source file:meka.gui.guichooser.PrecisionRecallCurve.java

License:Open Source License

/**
 * Called by the menu items action listener.
 *///from  ww w  .  jav  a 2  s  .co  m
@Override
protected void launch() {
    m_FileChooser = GUIHelper.newConverterFileChooser();
    // choose file
    int retVal = m_FileChooser.showOpenDialog(null);
    if (retVal != JFileChooser.APPROVE_OPTION)
        return;
    File file = m_FileChooser.getSelectedFile();

    // create plot
    Instances data;
    try {
        data = m_FileChooser.getLoader().getDataSet();
    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, "Error loading file '" + file + "':\n" + e, "Error",
                JOptionPane.ERROR_MESSAGE);
        e.printStackTrace();
        return;
    }
    data.setClassIndex(data.numAttributes() - 1);
    ThresholdVisualizePanel vmc = new ThresholdVisualizePanel();
    vmc.setROCString("(Area under PRC = " + Utils.doubleToString(ThresholdCurve.getPRCArea(data), 4) + ")");
    vmc.setName(data.relationName());
    PlotData2D tempd = new PlotData2D(data);
    tempd.setPlotName(data.relationName());
    tempd.m_displayAllPoints = true;
    // specify which points are connected
    boolean[] cp = new boolean[data.numInstances()];
    for (int n = 1; n < cp.length; n++)
        cp[n] = true;
    try {
        tempd.setConnectPoints(cp);
        vmc.addPlot(tempd);
        if (data.attribute(ThresholdCurve.RECALL_NAME) != null)
            vmc.setXIndex(data.attribute(ThresholdCurve.RECALL_NAME).index());
        if (data.attribute(ThresholdCurve.PRECISION_NAME) != null)
            vmc.setYIndex(data.attribute(ThresholdCurve.PRECISION_NAME).index());
    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, "Error adding plot:\n" + e, "Error", JOptionPane.ERROR_MESSAGE);
        e.printStackTrace();
        return;
    }

    MekaFrame frame = new MekaFrame();
    frame.setTitle(getName());
    frame.setDefaultCloseOperation(MekaFrame.DISPOSE_ON_CLOSE);
    frame.getContentPane().setLayout(new BorderLayout());
    frame.getContentPane().add(vmc);
    frame.setSize(800, 600);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}

From source file:meka.gui.guichooser.ROC.java

License:Open Source License

/**
 * Called by the menu items action listener.
 *//*from w w  w.j ava  2 s  .c  o  m*/
@Override
protected void launch() {
    m_FileChooser = GUIHelper.newConverterFileChooser();
    // choose file
    int retVal = m_FileChooser.showOpenDialog(null);
    if (retVal != JFileChooser.APPROVE_OPTION)
        return;
    File file = m_FileChooser.getSelectedFile();

    // create plot
    Instances data;
    try {
        data = m_FileChooser.getLoader().getDataSet();
    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, "Error loading file '" + file + "':\n" + e, "Error",
                JOptionPane.ERROR_MESSAGE);
        e.printStackTrace();
        return;
    }
    data.setClassIndex(data.numAttributes() - 1);
    ThresholdVisualizePanel vmc = new ThresholdVisualizePanel();
    vmc.setROCString("(Area under ROC = " + Utils.doubleToString(ThresholdCurve.getROCArea(data), 4) + ")");
    vmc.setName(data.relationName());
    PlotData2D tempd = new PlotData2D(data);
    tempd.setPlotName(data.relationName());
    tempd.m_displayAllPoints = true;
    // specify which points are connected
    boolean[] cp = new boolean[data.numInstances()];
    for (int n = 1; n < cp.length; n++)
        cp[n] = true;
    try {
        tempd.setConnectPoints(cp);
        vmc.addPlot(tempd);
        if (data.attribute(ThresholdCurve.FP_RATE_NAME) != null)
            vmc.setXIndex(data.attribute(ThresholdCurve.FP_RATE_NAME).index());
        if (data.attribute(ThresholdCurve.TP_RATE_NAME) != null)
            vmc.setYIndex(data.attribute(ThresholdCurve.TP_RATE_NAME).index());
    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, "Error adding plot:\n" + e, "Error", JOptionPane.ERROR_MESSAGE);
        e.printStackTrace();
        return;
    }

    MekaFrame frame = new MekaFrame();
    frame.setTitle(getName());
    frame.setDefaultCloseOperation(MekaFrame.DISPOSE_ON_CLOSE);
    frame.getContentPane().setLayout(new BorderLayout());
    frame.getContentPane().add(vmc);
    frame.setSize(800, 600);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}

From source file:milk.classifiers.MINND.java

License:Open Source License

/**
 * Updates the minimum and maximum values for all the attributes
 * based on a new exemplar./*from   ww  w  .j a  v a  2 s .  c o m*/
 *
 * @param ex the new exemplar
 */
private void updateMinMax(Exemplar ex) {
    Instances insts = ex.getInstances();
    int m = 0;
    for (int j = 0; j < insts.numAttributes(); j++) {
        if ((j != ex.idIndex()) && (j != ex.classIndex())) {
            if (insts.attribute(j).isNumeric()) {
                for (int k = 0; k < insts.numInstances(); k++) {
                    Instance ins = insts.instance(k);
                    if (!ins.isMissing(j)) {
                        if (Double.isNaN(m_MinArray[m])) {
                            m_MinArray[m] = ins.value(j);
                            m_MaxArray[m] = ins.value(j);
                        } else {
                            if (ins.value(j) < m_MinArray[m])
                                m_MinArray[m] = ins.value(j);
                            else if (ins.value(j) > m_MaxArray[m])
                                m_MaxArray[m] = ins.value(j);
                        }
                    }
                }
            }
            m++;
        }
    }
}

From source file:milk.classifiers.MINND.java

License:Open Source License

/**
 * Scale the given exemplar so that the returned exemplar
 * has the value of 0 to 1 for each dimension
 * // w ww. java  2 s.c om
 * @param before the given exemplar
 * @return the resultant exemplar after scaling
 * @exception if given exampler cannot be scaled properly
 */
private Exemplar scale(Exemplar before) throws Exception {
    Instances data = before.getInstances();
    Exemplar after = new Exemplar(before, 0);
    for (int i = 0; i < data.numInstances(); i++) {
        Instance datum = data.instance(i);
        Instance inst = (Instance) datum.copy();
        int k = 0;
        for (int j = 0; j < data.numAttributes(); j++) {
            if ((j != before.idIndex()) && (j != before.classIndex())) {
                if (data.attribute(j).isNumeric())
                    inst.setValue(j, (datum.value(j) - m_MinArray[k]) / (m_MaxArray[k] - m_MinArray[k]));
                k++;
            }
        }
        after.add(inst);
    }
    return after;
}

From source file:milk.core.Exemplar.java

License:Open Source License

/**
 * Main method for testing this class -- just prints out a set
 * of Exemplars.  Assume the ID index is 0.
 *
 * @param argv should contain one element: the name of an ARFF file
 *///from  w  ww  . j a  v a 2 s . com
public static void main(String[] args) {

    try {
        Reader r = null;
        if (args.length > 1) {
            throw (new Exception("Usage: Instances <filename>"));
        } else if (args.length == 0) {
            r = new BufferedReader(new InputStreamReader(System.in));
        } else {
            r = new BufferedReader(new FileReader(args[0]));
        }
        Instances i = new Instances(r);
        i.setClassIndex(i.numAttributes() - 1);

        Attribute id = i.attribute(0);
        if (!id.isNominal())
            throw new Exception("The first attribute is not nominal");

        Exemplar[] egs = new Exemplar[id.numValues()];
        for (int j = 0; j < egs.length; j++)
            egs[j] = null;

        for (int j = 0; j < i.numInstances(); j++) {
            Instance ins = i.instance(j);
            int idv = (int) ins.value(0);
            if (egs[idv] == null)
                egs[idv] = new Exemplar(ins, 0);
            else
                egs[idv].add(ins);
        }

        for (int j = 0; j < egs.length; j++)
            System.out.println(egs[j].toString());
    } catch (Exception ex) {
        System.err.println(ex.getMessage());
    }
}

From source file:milk.core.Exemplars.java

License:Open Source License

/**
 * Constructor using the given dataset and set ID index to 
 * the given ID index.  Any instances with class value or ID
 * value missing will be dropped.//from w ww  . j av a2 s .co m
 *
 * @param dataset the instances from which the header 
 * information is to be taken
 * @param idIndex the ID attribute's index 
 * @exception Exception if the class index of the dataset 
 * is not set(i.e. -1) or the data is not a multi-instance data
 */
public Exemplars(Instances dataset, int idIndex) throws Exception {
    if (dataset.classIndex() == -1)
        throw new Exception(" Class Index negative (class not set yet)!");

    m_ClassIndex = dataset.classIndex();
    m_RelationName = dataset.relationName();
    int numAttr = dataset.numAttributes();
    m_Attributes = new Attribute[numAttr];
    for (int i = 0; i < numAttr; i++)
        m_Attributes[i] = dataset.attribute(i);

    m_IdIndex = idIndex;
    Attribute id = m_Attributes[m_IdIndex];
    if ((m_IdIndex > numAttr) || (m_IdIndex < 0) || (!id.isNominal()))
        throw new Exception("ID index is wrong!");

    m_Exemplars = new Vector(id.numValues());

    for (int j = 0; j < dataset.numInstances(); j++) {
        Instance ins = dataset.instance(j);
        add(ins);
    }
}

From source file:milk.gui.experiment.MIResultsPanel.java

License:Open Source License

/**
 * Queries the user enough to make a database query to retrieve experiment
 * results.//w w  w. ja  v  a 2  s .com
 */
protected void setInstancesFromDBaseQuery() {

    try {
        if (m_InstanceQuery == null) {
            m_InstanceQuery = new MIInstanceQuery();
        }
        String dbaseURL = m_InstanceQuery.getDatabaseURL();
        dbaseURL = (String) JOptionPane.showInputDialog(this, "Enter the database URL", "Query Database",
                JOptionPane.PLAIN_MESSAGE, null, null, dbaseURL);
        if (dbaseURL == null) {
            m_FromLab.setText("Cancelled");
            return;
        }
        m_InstanceQuery.setDatabaseURL(dbaseURL);
        m_InstanceQuery.connectToDatabase();
        if (!m_InstanceQuery.experimentIndexExists()) {
            m_FromLab.setText("No experiment index");
            return;
        }
        m_FromLab.setText("Getting experiment index");
        Instances index = m_InstanceQuery.retrieveInstances("SELECT * FROM " + MIInstanceQuery.EXP_INDEX_TABLE);
        if (index.numInstances() == 0) {
            m_FromLab.setText("No experiments available");
            return;
        }
        m_FromLab.setText("Got experiment index");

        DefaultListModel lm = new DefaultListModel();
        for (int i = 0; i < index.numInstances(); i++) {
            lm.addElement(index.instance(i).toString());
        }
        JList jl = new JList(lm);
        ListSelectorDialog jd = new ListSelectorDialog(null, jl);
        int result = jd.showDialog();
        if (result != ListSelectorDialog.APPROVE_OPTION) {
            m_FromLab.setText("Cancelled");
            return;
        }
        Instance selInst = index.instance(jl.getSelectedIndex());
        Attribute tableAttr = index.attribute(MIInstanceQuery.EXP_RESULT_COL);
        String table = MIInstanceQuery.EXP_RESULT_PREFIX + selInst.toString(tableAttr);

        setInstancesFromDatabaseTable(table);
    } catch (Exception ex) {
        m_FromLab.setText("Problem reading database");
    }
}

From source file:ml.ann.SinglePTR.java

@Override
public void buildClassifier(Instances train) throws Exception {
    double[][] input;
    double weightawal = 0.0;
    input = new double[train.numInstances()][train.numAttributes()];
    for (int i = 0; i < train.numInstances(); i++) {
        for (int j = 1; j < train.numAttributes(); j++) {
            System.out.println(train.attribute(j - 1));
            input[i][j] = train.instance(i).value(j - 1);
            System.out.println("input[" + i + "][" + j + "]: " + input[i][j]);
        }//from  w w w .j a v a2 s  . co m
    }

    double[] target = new double[train.numInstances()];

    for (int i = 0; i < train.numInstances(); i++) {
        target[i] = train.instance(i).classValue();
        System.out.println("target[" + i + "]: " + target[i]);
    }

    double[][] weight = new double[train.numAttributes()][1];
    for (int i = 0; i < train.numAttributes(); i++) {
        weight[i][0] = weightawal;
    }

    if (algo == 1) {
        SinglePTR testrun;
        testrun = new SinglePTR(train.numInstances(), train.numAttributes() - 1, 10, 0.1, 0.01, input, target,
                weight, 1, momentum, randomWeight);
    } else if (algo == 2) {
        SinglePTR testrun;
        testrun = new SinglePTR(train.numInstances(), train.numAttributes() - 1, 10, 0.1, 0.01, input, target,
                weight, 2, momentum, randomWeight);
    } else if (algo == 3) {
        SinglePTR testrun;
        testrun = new SinglePTR(train.numInstances(), train.numAttributes() - 1, 10, 0.1, 0.01, input, target,
                weight, 3, momentum, randomWeight);
    }
}