Example usage for weka.core Instances classAttribute

List of usage examples for weka.core Instances classAttribute

Introduction

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

Prototype


publicAttribute classAttribute() 

Source Link

Document

Returns the class attribute.

Usage

From source file:sirius.clustering.main.ClustererClassificationPane.java

License:Open Source License

private void start() {
    //Run Classifier
    if (this.inputDirectoryTextField.getText().length() == 0) {
        JOptionPane.showMessageDialog(parent, "Please set Input Directory to where the clusterer output are!",
                "Evaluate Classifier", JOptionPane.ERROR_MESSAGE);
        return;/*from  ww  w  .j ava2s.  c o m*/
    }
    if (m_ClassifierEditor.getValue() == null) {
        JOptionPane.showMessageDialog(parent, "Please choose Classifier!", "Evaluate Classifier",
                JOptionPane.ERROR_MESSAGE);
        return;
    }
    if (validateStatsSettings(1) == false) {
        return;
    }
    if (this.clusteringClassificationThread == null) {
        startButton.setEnabled(false);
        stopButton.setEnabled(true);
        tabbedClassifierPane.setSelectedIndex(0);
        this.clusteringClassificationThread = (new Thread() {
            public void run() {
                //Clear the output text area
                levelOneClassifierOutputTextArea.setText("");
                resultsTableModel.reset();
                //double threshold = Double.parseDouble(classifierOneThresholdTextField.getText());                
                //cross-validation
                int numFolds;
                if (jackKnifeRadioButton.isSelected())
                    numFolds = -1;
                else
                    numFolds = Integer.parseInt(foldsField.getText());
                StringTokenizer st = new StringTokenizer(inputDirectoryTextField.getText(), File.separator);
                String filename = "";
                while (st.hasMoreTokens()) {
                    filename = st.nextToken();
                }
                StringTokenizer st2 = new StringTokenizer(filename, "_.");
                numOfCluster = 0;
                if (st2.countTokens() >= 2) {
                    st2.nextToken();
                    String numOfClusterString = st2.nextToken().replaceAll("cluster", "");
                    try {
                        numOfCluster = Integer.parseInt(numOfClusterString);
                    } catch (NumberFormatException e) {
                        JOptionPane.showMessageDialog(parent,
                                "Please choose the correct file! (Output from Utilize Clusterer)", "ERROR",
                                JOptionPane.ERROR_MESSAGE);
                    }
                }
                Classifier template = (Classifier) m_ClassifierEditor.getValue();

                for (int x = 0; x <= numOfCluster && clusteringClassificationThread != null; x++) {//Test each cluster
                    try {
                        long totalTimeStart = 0, totalTimeElapsed = 0;
                        totalTimeStart = System.currentTimeMillis();
                        statusLabel.setText("Reading in cluster" + x + " file..");
                        String inputFilename = inputDirectoryTextField.getText()
                                .replaceAll("_cluster" + numOfCluster + ".arff", "_cluster" + x + ".arff");
                        String outputScoreFilename = inputDirectoryTextField.getText()
                                .replaceAll("_cluster" + numOfCluster + ".arff", "_cluster" + x + ".score");
                        BufferedWriter output = new BufferedWriter(new FileWriter(outputScoreFilename));
                        Instances inst = new Instances(new FileReader(inputFilename));
                        //Assume that class attribute is the last attribute - This should be the case for all Sirius produced Arff files
                        inst.setClassIndex(inst.numAttributes() - 1);
                        Random random = new Random(1);//Simply set to 1, shall implement the random seed option later
                        inst.randomize(random);
                        if (inst.attribute(inst.classIndex()).isNominal())
                            inst.stratify(numFolds);
                        // for timing
                        ClassifierResults classifierResults = new ClassifierResults(false, 0);
                        String classifierName = m_ClassifierEditor.getValue().getClass().getName();
                        classifierResults.updateList(classifierResults.getClassifierList(), "Classifier: ",
                                classifierName);
                        classifierResults.updateList(classifierResults.getClassifierList(), "Training Data: ",
                                inputFilename);
                        classifierResults.updateList(classifierResults.getClassifierList(), "Time Used: ",
                                "NA");
                        //ArrayList<Double> resultList = new ArrayList<Double>();     
                        if (jackKnifeRadioButton.isSelected() || numFolds > inst.numInstances() - 1)
                            numFolds = inst.numInstances() - 1;
                        for (int fold = 0; fold < numFolds && clusteringClassificationThread != null; fold++) {//Doing cross-validation          
                            statusLabel.setText("Cluster: " + x + " - Training Fold " + (fold + 1) + "..");
                            Instances train = inst.trainCV(numFolds, fold, random);
                            Classifier current = null;
                            try {
                                current = Classifier.makeCopy(template);
                                current.buildClassifier(train);
                                Instances test = inst.testCV(numFolds, fold);
                                statusLabel.setText("Cluster: " + x + " - Testing Fold " + (fold + 1) + "..");
                                for (int jj = 0; jj < test.numInstances(); jj++) {
                                    double[] result = current.distributionForInstance(test.instance(jj));
                                    output.write("Cluster: " + x);
                                    output.newLine();
                                    output.newLine();
                                    output.write(test.instance(jj).stringValue(test.classAttribute()) + ",0="
                                            + result[0]);
                                    output.newLine();
                                }
                            } catch (Exception ex) {
                                ex.printStackTrace();
                                statusLabel.setText("Error in cross-validation!");
                                startButton.setEnabled(true);
                                stopButton.setEnabled(false);
                            }
                        }
                        output.close();
                        totalTimeElapsed = System.currentTimeMillis() - totalTimeStart;
                        classifierResults.updateList(classifierResults.getResultsList(), "Total Time Used: ",
                                Utils.doubleToString(totalTimeElapsed / 60000, 2) + " minutes "
                                        + Utils.doubleToString((totalTimeElapsed / 1000.0) % 60.0, 2)
                                        + " seconds");
                        double threshold = validateFieldAsThreshold(classifierOneThresholdTextField.getText(),
                                "Threshold Field", classifierOneThresholdTextField);
                        String filename2 = inputDirectoryTextField.getText()
                                .replaceAll("_cluster" + numOfCluster + ".arff", "_cluster" + x + ".score");
                        PredictionStats classifierStats = new PredictionStats(filename2, 0, threshold);

                        resultsTableModel.add("Cluster " + x, classifierResults, classifierStats);
                        resultsTable.setRowSelectionInterval(x, x);
                        computeStats(numFolds);//compute and display the results                    
                    } catch (Exception e) {
                        e.printStackTrace();
                        statusLabel.setText("Error in reading file!");
                        startButton.setEnabled(true);
                        stopButton.setEnabled(false);
                    }
                } //end of cluster for loop

                resultsTableModel.add("Summary - Equal Weightage", null, null);
                resultsTable.setRowSelectionInterval(numOfCluster + 1, numOfCluster + 1);
                computeStats(numFolds);
                resultsTableModel.add("Summary - Weighted Average", null, null);
                resultsTable.setRowSelectionInterval(numOfCluster + 2, numOfCluster + 2);
                computeStats(numFolds);

                if (clusteringClassificationThread != null)
                    statusLabel.setText("Done!");
                else
                    statusLabel.setText("Interrupted..");
                startButton.setEnabled(true);
                stopButton.setEnabled(false);
                if (classifierOne != null) {
                    levelOneClassifierOutputScrollPane.getVerticalScrollBar()
                            .setValue(levelOneClassifierOutputScrollPane.getVerticalScrollBar().getMaximum());
                }
                clusteringClassificationThread = null;

            }
        });
        this.clusteringClassificationThread.setPriority(Thread.MIN_PRIORITY);
        this.clusteringClassificationThread.start();
    } else {
        JOptionPane.showMessageDialog(parent,
                "Cannot start new job as previous job still running. Click stop to terminate previous job",
                "ERROR", JOptionPane.ERROR_MESSAGE);
    }
}

From source file:sirius.clustering.main.TrainClustererPane.java

License:Open Source License

private void start() {
    if (this.fileTextField.getText().length() == 0) {
        JOptionPane.showMessageDialog(parent, "Please choose training file!", "Error",
                JOptionPane.ERROR_MESSAGE);
        return;//from  w w  w . j a v a2 s .  co  m
    }
    if (m_ClustererEditor.getValue() == null) {
        JOptionPane.showMessageDialog(parent, "Please choose clustering method!", "Error",
                JOptionPane.ERROR_MESSAGE);
        return;
    }
    if (clusterThread != null) {
        JOptionPane.showMessageDialog(parent, "Cannot start training of Clusterer as another is running!",
                "Error", JOptionPane.ERROR_MESSAGE);
        return;
    }
    this.startButton.setEnabled(false);
    this.stopButton.setEnabled(true);
    this.numberOfClusterTextField.setText("");
    clusterThread = (new Thread() {
        public void run() {
            try {
                Instances inst = new Instances(new BufferedReader(new FileReader(fileTextField.getText())));
                inst.setClassIndex(m_ClassCombo.getSelectedIndex());
                if (inst.classAttribute().isNumeric()) {
                    JOptionPane.showMessageDialog(parent, "Class must be nominal!", "Error",
                            JOptionPane.ERROR_MESSAGE);
                } else {
                    outputTextArea.setText("");
                    clusterer = (Clusterer) m_ClustererEditor.getValue();
                    statusLabel.setText(" Training Clusterer..");
                    clusterer.buildClusterer(removeClass(inst));
                    ClusterEvaluation eval = new ClusterEvaluation();
                    eval.setClusterer(clusterer);
                    eval.evaluateClusterer(inst);
                    outputTextArea.append(eval.clusterResultsToString());
                    outputTextArea.append("\n");
                    if (clusterer != null) {
                        numberOfClusterTextField.setText("" + clusterer.numberOfClusters());
                        statusLabel.setText(" Clusterer Trained..");
                    }
                }
                startButton.setEnabled(true);
                stopButton.setEnabled(false);
                clusterThread = null;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
    clusterThread.setPriority(Thread.MIN_PRIORITY); // UI has most priority
    clusterThread.start();
}

From source file:sirius.clustering.main.UtilizeClustererPane.java

License:Open Source License

private void start() {
    if (this.clustererTextField.getText().length() == 0) {
        JOptionPane.showMessageDialog(parent, "Please set clusterer!", "Error", JOptionPane.ERROR_MESSAGE);
        return;/*w  w  w  . j  ava2 s.  c  o  m*/
    }
    if (this.inputFileTextField.getText().length() == 0) {
        JOptionPane.showMessageDialog(parent, "Please choose input file!", "Error", JOptionPane.ERROR_MESSAGE);
        return;
    }
    if (this.outputFileTextField.getText().length() == 0) {
        JOptionPane.showMessageDialog(parent, "Please choose output location!", "Error",
                JOptionPane.ERROR_MESSAGE);
        return;
    }
    if (inputThread != null) {
        JOptionPane.showMessageDialog(parent, "Cannot start because another is still running!", "Error",
                JOptionPane.ERROR_MESSAGE);
        return;
    }
    this.bottomStartButton.setEnabled(false);
    this.bottomStopButton.setEnabled(true);
    run = true;
    inputThread = (new Thread() {
        public void run() {
            try {
                statusLabel.setText(" Evaluating Input File..");
                BufferedWriter[] outputArray = new BufferedWriter[clusterer.numberOfClusters()];
                int[] counter = new int[clusterer.numberOfClusters()];
                for (int x = 0; x < clusterer.numberOfClusters(); x++) {
                    outputArray[x] = new BufferedWriter(new FileWriter(outputFileTextField.getText()
                            + File.separator + inputFileName + "_cluster" + x + ".arff"));
                }
                BufferedReader input = new BufferedReader(new FileReader(inputFileTextField.getText()));
                String line;
                while ((line = input.readLine()) != null) {
                    if (run == false)
                        break;
                    for (int x = 0; x < clusterer.numberOfClusters(); x++) {
                        outputArray[x].write(line);
                        outputArray[x].newLine();
                    }
                    if (line.equalsIgnoreCase("@data")) {
                        for (int x = 0; x < clusterer.numberOfClusters(); x++) {
                            outputArray[x].newLine();
                        }
                        break;
                    }
                }
                input.close();
                Instances inst = new Instances(
                        new BufferedReader(new FileReader(inputFileTextField.getText())));
                inst.setClassIndex(m_ClassCombo.getSelectedIndex());
                if (inst.classAttribute().isNumeric()) {
                    JOptionPane.showMessageDialog(parent, "Class must be nominal!", "Error",
                            JOptionPane.ERROR_MESSAGE);
                } else {
                    Instances instWithoutClass = removeClass(inst);
                    outputTextArea.setText("Number of Clusters: " + clusterer.numberOfClusters() + "\r\n");
                    outputTextArea.append("Number of Attributes: " + (inst.numAttributes() - 1) + "\r\n");
                    outputTextArea.append("Number of Instances: " + inst.numInstances() + "\r\n\r\n");
                    for (int x = 0; x < inst.numInstances(); x++) {
                        if (run == false)
                            break;
                        int clusterIndex = clusterer.clusterInstance(instWithoutClass.instance(x));
                        counter[clusterIndex]++;
                        outputArray[clusterIndex].write(inst.instance(x).toString());
                        outputArray[clusterIndex].newLine();
                    }
                    for (int x = 0; x < clusterer.numberOfClusters(); x++) {
                        outputArray[x].close();
                    }
                    inputThread = null;
                    bottomStartButton.setEnabled(true);
                    bottomStopButton.setEnabled(false);
                    if (run != false) {
                        statusLabel.setText(" Evaluating Input File..Done!");
                        for (int x = 0; x < clusterer.numberOfClusters(); x++) {
                            outputTextArea.append("Cluster " + x + ": " + counter[x] + "\r\n");
                        }
                    } else
                        statusLabel.setText(" Evaluating Input File..Interrupted!");
                }
            } catch (Exception e) {
                e.printStackTrace();
                JOptionPane.showMessageDialog(parent,
                        "Please ensure that the Trained Clusterer and Input File have same Attributes!",
                        "Error", JOptionPane.ERROR_MESSAGE);
            }
        }
    });
    inputThread.setPriority(Thread.MIN_PRIORITY); // UI has most priority
    inputThread.start();
}

From source file:smo2.SMO.java

License:Open Source License

/**
 * Method for building the classifier. Implements a one-against-one wrapper
 * for multi-class problems.//w w  w  . j  av a  2 s.c  om
 *
 * @param insts
 *            the set of training instances
 * @exception Exception
 *                if the classifier can't be built successfully
 */
public void buildClassifier(Instances insts) throws Exception {

    if (!m_checksTurnedOff) {
        if (insts.checkForStringAttributes()) {
            throw new UnsupportedAttributeTypeException("Cannot handle string attributes!");
        }
        if (insts.classAttribute().isNumeric()) {
            throw new UnsupportedClassTypeException(
                    "mySMO can't handle a numeric class! Use" + "SMOreg for performing regression.");
        }
        insts = new Instances(insts);
        insts.deleteWithMissingClass();
        if (insts.numInstances() == 0) {
            throw new Exception("No training instances without a missing class!");
        }

        /*
         * Removes all the instances with weight equal to 0. MUST be done
         * since condition (8) of Keerthi's paper is made with the assertion
         * Ci > 0 (See equation (3a).
         */
        Instances data = new Instances(insts, insts.numInstances());
        for (int i = 0; i < insts.numInstances(); i++) {
            if (insts.instance(i).weight() > 0)
                data.add(insts.instance(i));
        }
        if (data.numInstances() == 0) {
            throw new Exception("No training instances left after removing "
                    + "instance with either a weight null or a missing class!");
        }
        insts = data;

    }

    m_onlyNumeric = true;
    if (!m_checksTurnedOff) {
        for (int i = 0; i < insts.numAttributes(); i++) {
            if (i != insts.classIndex()) {
                if (!insts.attribute(i).isNumeric()) {
                    m_onlyNumeric = false;
                    break;
                }
            }
        }
    }

    if (!m_checksTurnedOff) {
        m_Missing = new ReplaceMissingValues();
        m_Missing.setInputFormat(insts);
        insts = Filter.useFilter(insts, m_Missing);
    } else {
        m_Missing = null;
    }

    if (!m_onlyNumeric) {
        m_NominalToBinary = new NominalToBinary();
        m_NominalToBinary.setInputFormat(insts);
        insts = Filter.useFilter(insts, m_NominalToBinary);
    } else {
        m_NominalToBinary = null;
    }

    if (m_filterType == FILTER_STANDARDIZE) {
        m_Filter = new Standardize();
        m_Filter.setInputFormat(insts);
        insts = Filter.useFilter(insts, m_Filter);
    } else if (m_filterType == FILTER_NORMALIZE) {
        m_Filter = new Normalize();
        m_Filter.setInputFormat(insts);
        insts = Filter.useFilter(insts, m_Filter);
    } else {
        m_Filter = null;
    }

    m_classIndex = insts.classIndex();
    m_classAttribute = insts.classAttribute();

    // Generate subsets representing each class
    Instances[] subsets = new Instances[insts.numClasses()];
    for (int i = 0; i < insts.numClasses(); i++) {
        subsets[i] = new Instances(insts, insts.numInstances());
    }
    for (int j = 0; j < insts.numInstances(); j++) {
        Instance inst = insts.instance(j);
        subsets[(int) inst.classValue()].add(inst);
    }
    for (int i = 0; i < insts.numClasses(); i++) {
        subsets[i].compactify();
    }

    // Build the binary classifiers
    Random rand = new Random(m_randomSeed);
    m_classifiers = new BinarymySMO[insts.numClasses()][insts.numClasses()];
    for (int i = 0; i < insts.numClasses(); i++) {
        for (int j = i + 1; j < insts.numClasses(); j++) {
            m_classifiers[i][j] = new BinarymySMO();
            Instances data = new Instances(insts, insts.numInstances());
            for (int k = 0; k < subsets[i].numInstances(); k++) {
                data.add(subsets[i].instance(k));
            }
            for (int k = 0; k < subsets[j].numInstances(); k++) {
                data.add(subsets[j].instance(k));
            }
            data.compactify();
            data.randomize(rand);
            m_classifiers[i][j].buildClassifier(data, i, j, m_fitLogisticModels, m_numFolds, m_randomSeed);
        }
    }
}

From source file:test.org.moa.opencl.IBk.java

License:Open Source License

/**
 * Generates the classifier./*from www .j  a  v  a2s  .co m*/
 *
 * @param instances set of instances serving as training data 
 * @throws Exception if the classifier has not been generated successfully
 */
public void buildClassifier(Instances instances) throws Exception {

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

    // remove instances with missing class
    instances = new Instances(instances);
    instances.deleteWithMissingClass();

    m_NumClasses = instances.numClasses();
    m_ClassType = instances.classAttribute().type();
    m_Train = new Instances(instances, 0, instances.numInstances());

    // Throw away initial instances until within the specified window size
    if ((m_WindowSize > 0) && (instances.numInstances() > m_WindowSize)) {
        m_Train = new Instances(m_Train, m_Train.numInstances() - m_WindowSize, m_WindowSize);
    }

    m_NumAttributesUsed = 0.0;
    for (int i = 0; i < m_Train.numAttributes(); i++) {
        if ((i != m_Train.classIndex())
                && (m_Train.attribute(i).isNominal() || m_Train.attribute(i).isNumeric())) {
            m_NumAttributesUsed += 1.0;
        }
    }

    m_NNSearch.setInstances(m_Train);

    // Invalidate any currently cross-validation selected k
    m_kNNValid = false;

    m_defaultModel = new ZeroR();
    m_defaultModel.buildClassifier(instances);
}

From source file:textmining.TextMining.java

/**
 * Main//  w  w  w  . j a v a  2 s  . c om
 *
 * @param args
 * @throws FileNotFoundException
 * @throws IOException
 * @throws Exception
 */
public static void main(String[] args) throws IOException, Exception {

    //        System.out.println("File selected : "+arff);
    /*OPTIONS*/
    String arff = "C:/wamp/www/AllocineHelper/arff/100_commentaires_1_cat.arff";
    // String arff = "/Users/Mathieu/NetBeansProjects/AllocineHelper/arff/20160104.arff";
    boolean showRegression = Boolean.valueOf("false");
    int nb_folds = 9;
    boolean setIDF = true;
    boolean setTF = true;
    String stemmer = "LovinsStemmer";
    String tokenizer = "Alphabetical";

    //        String arff = args[0];
    //        boolean showRegression = Boolean.valueOf(args[1]);
    //        int nb_folds = Integer.valueOf(args[2]);
    //        boolean setIDF = Boolean.valueOf(args[3]);
    //        boolean setTF = Boolean.valueOf(args[4]);
    //        String stemmer = args[5];
    //        String tokenizer = args[6];
    String stopWords = "C:/wamp/www/AllocineHelper/stopwords_fr.txt";
    ////        String stopWords = "/Users/Mathieu/NetBeansProjects/AllocineHelper/stopwords_fr.txt";        

    //        TestAlgo test1 = new TestAlgo(arff);
    //        test1.setStop_words_path_file(stopWords);
    //        try {
    //            test1.buildData(setIDF, setTF, 1, stemmer, tokenizer);//IDF=>true/false , TF=>true/false , Classe 1 => Commentaires
    //        } catch (Exception ex) {
    //            System.out.println("Fichier inconnu");
    //        }
    //        System.out.println("*************CLASSIFICATION********************");
    //        System.out.println("-------OPTIONS--------");
    //        System.out.println("IDF : " + String.valueOf(setIDF));
    //        System.out.println("TF : " + String.valueOf(setTF));
    //        System.out.println("Nb Folds for Cross Validation : " + nb_folds);
    //        System.out.println("Stemmer : " + stemmer);
    //        System.out.println("Tokenizer : " + tokenizer);
    //        System.out.println("-----------------------");
    //        
    //        System.out.println("*******************");
    //        System.out.println("DECISION TABLE");
    //        System.out.println("*******************");
    //        Classifier decisionTable = (Classifier) new DecisionTable();
    //        test1.setAlgo(decisionTable);
    //        String[] options = weka.core.Utils.splitOptions("-X 1 -S \"weka.attributeSelection.BestFirst -D 1 -N 5\"");
    //        System.out.println(test1.evaluate(options, nb_folds));
    //
    //        System.out.println("*******************");
    //        System.out.println("NAIVE BAYES");
    //        System.out.println("*******************");
    //        Classifier naiveBayes = (Classifier) new NaiveBayes();
    //        test1.setAlgo(naiveBayes);
    //        System.out.println(test1.evaluate(weka.core.Utils.splitOptions(""), nb_folds));
    //
    //        System.out.println("*******************");
    //        System.out.println("J 48");
    //        System.out.println("*******************");
    //        Classifier j48 = new J48();
    //        test1.setAlgo(j48);
    //        System.out.println(test1.evaluate(weka.core.Utils.splitOptions(""), nb_folds));
    //        
    //        System.out.println("*******************");
    //        System.out.println("ONE R");
    //        System.out.println("*******************");
    //        Classifier oneR = new OneR();
    //
    //        test1.setAlgo(oneR);
    //        System.out.println(test1.evaluate(weka.core.Utils.splitOptions(""), nb_folds));
    //
    ////        System.out.println("And the winner is : " + test1.getBestAlgo());
    //        if (showRegression) {
    //
    //            HashMap<String, Classifier> regressionClassifiers = new HashMap<String, Classifier>();
    //
    //            regressionClassifiers.put("LinearRegression", (Classifier) new LinearRegression());
    //            regressionClassifiers.put("SMO Reg", (Classifier) new SMOreg());
    //            regressionClassifiers.put("LeastMedSq", new LeastMedSq());
    //            System.out.println("***********************REGRESSION****************************");
    //
    //            for (Map.Entry<String, Classifier> entry : regressionClassifiers.entrySet()) {
    //                System.out.println("Algo : " + entry.getKey());
    //                Classifier algo = entry.getValue();
    //                test1.setAlgo(algo);
    //                test1.buildData(setIDF, setTF, 0, stemmer, tokenizer);
    //                options = weka.core.Utils.splitOptions("");
    //                System.out.println(test1.evaluateRegression(options, nb_folds));
    //                System.out.println(algo);
    //            }
    //
    //        }
    //Tests predictions
    //        TestAlgo prediction = new TestAlgo(arff);
    //        prediction.setStop_words_path_file(stopWords);
    //        Classifier algo =   (Classifier)new NaiveBayes();
    //        prediction.setAlgo(algo);
    //         prediction.buildData(setIDF, setTF, 1, stemmer,tokenizer);
    //        
    //          String[] options = weka.core.Utils.splitOptions("");
    //          prediction.evaluateRegression(options, nb_folds);
    //         weka.core.SerializationHelper.write("naive_bayes.model", algo);
    //System.exit(-1);
    Instances data;
    //            LinearRegression LR = (LinearRegression)weka.core.SerializationHelper.read("linear_reg.model");
    NaiveBayes NB = (NaiveBayes) weka.core.SerializationHelper.read("naive_bayes.model");
    try (BufferedReader reader = new BufferedReader(
            new FileReader("C:/wamp/www/AllocineHelper/arff/supplied_test.arff"))) {
        data = new Instances(reader);
    }
    data.setClassIndex(data.numAttributes() - 1);
    int nb_good = 0;
    for (int i = 0; i < data.numInstances(); i++) {
        double actualValue = data.instance(i).classValue();

        Instance newInst = data.instance(i);
        System.out.println(newInst);
        double predAlgo = NB.classifyInstance(newInst);
        if (actualValue == predAlgo)
            nb_good++;
        System.out.println(data.classAttribute().value((int) actualValue) + "  => "
                + data.classAttribute().value((int) predAlgo));
        System.out.println("***********************");
    }

    System.out.println("Pourcentage russite prdictions : "
            + ((double) nb_good / (double) data.numInstances() * 100) + " %");
    //         for (int i = 0; i < data.numInstances(); i++) {
    //            double actualValue = data.instance(i).classValue();
    //             System.out.println(actualValue);
    //            Instance newInst = data.instance(i);
    //            
    //            double predAlgo = LR.classifyInstance(newInst);
    //             System.out.println(actualValue + "  => "+predAlgo);
    //        }
    //        
    //        System.out.println("OPTIONS FILTER : STOPWORDS ONLY");
    //        // C_DecisionTable
    //        String rep_DecisionTable = C_DecisionTable(dataFiltered);
    //        System.out.println("DECISION TABLE");
    //        System.out.println(rep_DecisionTable);
    //    
    //        // ---------------
    //        System.out.println("\n--------------------\n");
    //        
    //        // C_NaiveBayes
    //        String rep_NaiveBayes = C_NaiveBayes(dataFiltered);
    //        System.out.println("NAIVE BAYES");
    //        System.out.println(rep_NaiveBayes);
    //        //                       
    //        
    //        System.out.println("OPTIONS FILTER : STOPWORDS + IDF/TF TRANSFORM");
    //        
    //        StringToWordVector wordVector2 = new StringToWordVector();
    //        wordVector2.setInputFormat(data);        
    //        wordVector2.setStopwords(new File(stopWords));
    //        wordVector2.setIDFTransform(true);
    //        wordVector2.setTFTransform(true);
    ////        wordVector2.setStemmer(new SnowballStemmer());
    //        Instances dataFilteredWithOptions = Filter.useFilter(data, wordVector2);
    //        dataFilteredWithOptions.setClassIndex(1);
    //        
    //         System.out.println("LINEAR REGRESSION ON POLARITY");
    //         dataFiltered.setClassIndex(0);
    //        String result_Regression = Regression_on_Polarity(dataFiltered);
    //        System.out.println(result_Regression);
    //        
    //        String rep_DecisionTable_with_Options = C_DecisionTable(dataFilteredWithOptions);        
    //        System.out.println("DECISION TABLE");
    //        System.out.println(rep_DecisionTable_with_Options);
    //        
    //        // C_NaiveBayes
    //        String rep_NaiveBayes_with_Options = C_NaiveBayes(dataFilteredWithOptions);
    //        System.out.println("NAIVE BAYES");
    //        System.out.println(rep_NaiveBayes_with_Options);
    //        
}

From source file:trainableSegmentation.WekaSegmentation.java

License:GNU General Public License

/**
 * Adjust current segmentation state (attributes and classes) to
 * loaded data/*from   w  ww . ja va 2 s .c o  m*/
 * @param data loaded instances
 * @return false if error
 */
public boolean adjustSegmentationStateToData(Instances data) {
    // Check the features that were used in the loaded data
    boolean featuresChanged = false;
    Enumeration<Attribute> attributes = data.enumerateAttributes();
    final int numFeatures = FeatureStack.availableFeatures.length;
    boolean[] usedFeatures = new boolean[numFeatures];

    // Initialize list of names for the features to use
    this.featureNames = new ArrayList<String>();

    float minSigma = Float.MAX_VALUE;
    float maxSigma = Float.MIN_VALUE;

    while (attributes.hasMoreElements()) {
        final Attribute a = attributes.nextElement();
        this.featureNames.add(a.name());
        for (int i = 0; i < numFeatures; i++) {
            if (a.name().startsWith(FeatureStack.availableFeatures[i])) {
                usedFeatures[i] = true;
                if (i == FeatureStack.MEMBRANE) {
                    int index = a.name().indexOf("s_") + 4;
                    int index2 = a.name().indexOf("_", index + 1);
                    final int patchSize = Integer.parseInt(a.name().substring(index, index2));
                    if (patchSize != membranePatchSize) {
                        membranePatchSize = patchSize;
                        this.featureStackArray.setMembranePatchSize(patchSize);
                        featuresChanged = true;
                    }
                    index = a.name().lastIndexOf("_");
                    final int thickness = Integer.parseInt(a.name().substring(index + 1));
                    if (thickness != membraneThickness) {
                        membraneThickness = thickness;
                        this.featureStackArray.setMembraneSize(thickness);
                        featuresChanged = true;
                    }

                } else if (i < FeatureStack.ANISOTROPIC_DIFFUSION) {
                    String[] tokens = a.name().split("_");
                    for (int j = 0; j < tokens.length; j++)
                        if (tokens[j].indexOf(".") != -1) {
                            final float sigma = Float.parseFloat(tokens[j]);
                            if (sigma < minSigma)
                                minSigma = sigma;
                            if (sigma > maxSigma)
                                maxSigma = sigma;
                        }
                }
            }
        }
    }

    IJ.log("Field of view: max sigma = " + maxSigma + ", min sigma = " + minSigma);
    IJ.log("Membrane thickness: " + membraneThickness + ", patch size: " + membranePatchSize);
    if (minSigma != this.minimumSigma && minSigma != 0) {
        this.minimumSigma = minSigma;
        featuresChanged = true;
        this.featureStackArray.setMinimumSigma(minSigma);
    }
    if (maxSigma != this.maximumSigma) {
        this.maximumSigma = maxSigma;
        featuresChanged = true;
        this.featureStackArray.setMaximumSigma(maxSigma);
    }

    // Check if classes match
    Attribute classAttribute = data.classAttribute();
    Enumeration<String> classValues = classAttribute.enumerateValues();

    // Update list of names of loaded classes
    loadedClassNames = new ArrayList<String>();

    int j = 0;
    setNumOfClasses(0);

    while (classValues.hasMoreElements()) {
        final String className = classValues.nextElement().trim();
        loadedClassNames.add(className);
    }

    for (String className : loadedClassNames) {
        IJ.log("Read class name: " + className);

        setClassLabel(j, className);
        addClass();
        j++;
    }

    final boolean[] oldEnableFeatures = this.featureStackArray.getEnabledFeatures();
    // Read checked features and check if any of them changed
    for (int i = 0; i < numFeatures; i++) {
        if (usedFeatures[i] != oldEnableFeatures[i])
            featuresChanged = true;
    }
    // Update feature stack if necessary
    if (featuresChanged) {
        //this.setButtonsEnabled(false);
        this.setEnabledFeatures(usedFeatures);
        // Force features to be updated
        updateFeatures = true;
    }

    return true;
}

From source file:tubes1.myClassifiers.myC45.java

public TreeNode C45Node(Instances i, double parentGain) {
    TreeNode treeNode = new TreeNode();

    int[] count = calculateCount(i);
    for (int j = 0; j < count.length; j++) {
        int c = count[j];
        if (c == i.numInstances()) {
            treeNode.label = j;//from   ww w  . java  2s.  c om
            return treeNode;
        }
    }

    if (i.numAttributes() <= 1) {
        int maxc = -1;
        int maxcj = -1;
        for (int j = 0; j < count.length; j++) {
            if (count[j] > maxc) {
                maxc = count[j];
                maxcj = j;
            }
        }
        treeNode.label = maxcj;
        return treeNode;
    }

    Attribute bestA = null;
    double bestAIG = -1;
    double entropyOfSet = entropy(i);
    for (int j = 0; j < i.numAttributes(); j++) {
        Attribute a = i.attribute(j);
        if (a != i.classAttribute()) {
            double aIG = infoGain(i, a, entropyOfSet);
            if (aIG > bestAIG) {
                bestAIG = aIG;
                bestA = a;
            }
        }
    }
    double childGainRatio = gainRatio(bestAIG, entropyOfSet);
    treeNode.decision = bestA;
    if (childGainRatio > parentGain) {
        Instances[] subSets = splitData(i, bestA);
        for (Instances subSet : subSets) {
            if (subSet.numInstances() > 0) {
                double attributeValue = subSet.firstInstance().value(bestA);
                subSet.deleteAttributeAt(bestA.index());
                TreeNode newBranch = C45Node(subSet, childGainRatio);
                newBranch.branchValue = attributeValue;
                treeNode.addBranch(newBranch);
            }
        }
    } else {
        TreeNode newBranch = new TreeNode();
        newBranch.label = vote(i, bestA);
        newBranch.branchValue = treeNode.branchValue;
        treeNode.addBranch(newBranch);
    }
    return treeNode;
}

From source file:tubes1.myClassifiers.myC45.java

public int[] calculateCount(Instances instances) {

    int numClasses = instances.classAttribute().numValues();
    int numInstances = instances.numInstances();
    int[] num = new int[numClasses];
    for (int i = 0; i < numClasses; i++) {
        num[i] = 0;/*from  w  ww.j  a  va2  s.  c om*/
    }
    for (int i = 0; i < numInstances; i++) {
        Instance instance = instances.instance(i);
        double classValue = instance.value(instance.classAttribute());
        int classIndex = (int) classValue;
        num[classIndex]++;
    }
    return num;
}

From source file:tubes1.myClassifiers.myC45.java

public double[] calculateProportion(Instances instances) {
    int numClasses = instances.classAttribute().numValues();
    int numInstances = instances.numInstances();
    int[] num = calculateCount(instances);
    double[] result = new double[numClasses];
    for (int i = 0; i < numClasses; i++) {
        result[i] = ((double) num[i]) / numInstances;
    }//from w  w  w .  jav  a  2  s  .co m
    return result;
}