Example usage for weka.attributeSelection GainRatioAttributeEval buildEvaluator

List of usage examples for weka.attributeSelection GainRatioAttributeEval buildEvaluator

Introduction

In this page you can find the example usage for weka.attributeSelection GainRatioAttributeEval buildEvaluator.

Prototype

@Override
public void buildEvaluator(Instances data) throws Exception 

Source Link

Document

Initializes a gain ratio attribute evaluator.

Usage

From source file:sirius.misc.zscore.ZscoreTableModel.java

License:Open Source License

public void compute(final Instances posInstances, final Instances negInstances) {
    if (posInstances == null || negInstances == null) {
        JOptionPane.showMessageDialog(null, "Please load file before computing.", "Error",
                JOptionPane.ERROR_MESSAGE);
        return;/*w ww. j a  va2 s.c om*/
    }
    if (posInstances.numAttributes() != negInstances.numAttributes()) {
        JOptionPane.showMessageDialog(null, "Number of attributes between the two files does not tally.",
                "Error", JOptionPane.ERROR_MESSAGE);
        return;
    }
    this.scoreList = new ArrayList<Scores>();
    this.posInstances = posInstances;
    this.negInstances = negInstances;
    Thread thread = new Thread() {
        public void run() {
            MessageDialog m = new MessageDialog(null, "Progress", "0%");
            int percentCount = posInstances.numAttributes() / 100;
            if (percentCount == 0)
                percentCount = 1;
            for (int x = 0; x < posInstances.numAttributes(); x++) {
                if (x % percentCount == 0)
                    m.update(x / percentCount + "%");
                if (posInstances.attribute(x).isNumeric() == false) {
                    ZscoreTableModel.this.scoreList.add(new Scores(posInstances.attribute(x).name()));
                    continue;
                }
                String name = posInstances.attribute(x).name();
                double posMean = posInstances.attributeStats(x).numericStats.mean;
                double posStdDev = posInstances.attributeStats(x).numericStats.stdDev;
                double negMean = negInstances.attributeStats(x).numericStats.mean;
                double negStdDev = negInstances.attributeStats(x).numericStats.stdDev;
                if (negStdDev == 0)
                    negStdDev = 0.01;
                double totalZScore = 0.0;
                int numGTZScore0_5 = 0;
                int numGTZScore1 = 0;
                int numGTZScore2 = 0;
                int numGTZScore3 = 0;
                for (int y = 0; y < posInstances.numInstances(); y++) {
                    double zScore = Math.abs(((posInstances.instance(y).value(x) - negMean) / negStdDev));
                    totalZScore += zScore;
                    if (zScore > 0.5)
                        numGTZScore0_5++;
                    if (zScore > 1)
                        numGTZScore1++;
                    if (zScore > 2)
                        numGTZScore2++;
                    if (zScore > 3)
                        numGTZScore3++;
                }
                double meanZScore = totalZScore / posInstances.numInstances();
                double percentGTZScore0_5 = (numGTZScore0_5 * 100) / posInstances.numInstances();
                double percentGTZScore1 = (numGTZScore1 * 100) / posInstances.numInstances();
                double percentGTZScore2 = (numGTZScore2 * 100) / posInstances.numInstances();
                double percentGTZScore3 = (numGTZScore3 * 100) / posInstances.numInstances();
                ZscoreTableModel.this.scoreList
                        .add(new Scores(name, posMean, posStdDev, negMean, negStdDev, meanZScore,
                                percentGTZScore0_5, percentGTZScore1, percentGTZScore2, percentGTZScore3, -1));
            }
            try {
                Instances instances = new Instances(posInstances);
                for (int x = 0; x < negInstances.numInstances(); x++)
                    instances.add(negInstances.instance(x));
                instances.setClassIndex(instances.numAttributes() - 1);
                //Evaluate the attributes individually and obtain the gainRatio      
                GainRatioAttributeEval gainRatio = new GainRatioAttributeEval();
                if (instances.numAttributes() > 0) {
                    gainRatio.buildEvaluator(instances);
                }
                for (int x = 0; x < (instances.numAttributes() - 1); x++) {
                    ZscoreTableModel.this.scoreList.get(x).setGainRatio(gainRatio.evaluateAttribute(x));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            Collections.sort(ZscoreTableModel.this.scoreList, new SortByMeanZScore());
            fireTableDataChanged();
            m.dispose();
            ZscoreTableModel.this.label.setText("" + ZscoreTableModel.this.scoreList.size());
        }
    };
    thread.setPriority(Thread.MIN_PRIORITY); // UI has most priority
    thread.start();
}