Total members 11663 |It is currently Thu Sep 21, 2017 6:16 am Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





Random Forest Classifier with Cross Validation testing. Note that you have to add Weka.jar to the java path.
java code
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package wekatest;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Random;
import weka.classifiers.Evaluation;
import weka.classifiers.bayes.NaiveBayes;
import weka.classifiers.trees.RandomForest;

import weka.core.Instances;

/**
*
* @author samy
*/
public class WekaTest {

/**
* @param args the command line arguments
*/
public static void main(String[] args) throws Exception {
BufferedReader br = null;
int numFolds = 10;
br = new BufferedReader(new FileReader("D:\\martin\\DataSet\\Mozilla.arff"));

Instances trainData = new Instances(br);
trainData.setClassIndex(trainData.numAttributes() - 1);
br.close();
RandomForest rf = new RandomForest();
rf.setNumTrees(100);

// rf.buildClassifier(trainData);
Evaluation evaluation = new Evaluation(trainData);
evaluation.crossValidateModel(rf, trainData, numFolds, new Random(1));


System.out.println(evaluation.toSummaryString("\nResults\n======\n", true));
System.out.println(evaluation.toClassDetailsString());
System.out.println("Results For Class -1- ");
System.out.println("Precision= " + evaluation.precision(0));
System.out.println("Recall= " + evaluation.recall(0));
System.out.println("F-measure= " + evaluation.fMeasure(0));
System.out.println("Results For Class -2- ");
System.out.println("Precision= " + evaluation.precision(1));
System.out.println("Recall= " + evaluation.recall(1));
System.out.println("F-measure= " + evaluation.fMeasure(1));



}
}





Author:
Mastermind
User avatar Posts: 2707
Have thanks: 74 time
Post new topic Reply to topic  [ 1 post ] 

  Related Posts  to : Weka java code for Random Forest Cross Validation
 Cost Sensitive Classifier Random Forest Java in weka     -  
 random forest algorithm classifier     -  
 R script for RandomForest with Cross-validation and Sampling     -  
 US Zip Code Validation     -  
 What will be validation for Zip code ?     -  
 Generating Random Number in java     -  
 Cross platform c++ programming     -  
 php Random quote     -  
 php Random image     -  
 Random To File     -  



Topic Tags

Weka Classifiers







Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team