Total members 11895 |It is currently Sun Dec 22, 2024 12:15 pm 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));



}
}




_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


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

  Related Posts  to : Weka java code for Random Forest Cross Validation
 KFold Cross-validation Random Forest Binary Classification     -  
 Cost Sensitive Classifier Random Forest Java in weka     -  
 random forest algorithm classifier     -  
 Get the important variables of random forest classifier     -  
 R script for RandomForest with Cross-validation and Sampling     -  
 Random Forest Classification (Binary )- Supervised Learning     -  
 What will be validation for Zip code ?     -  
 US Zip Code Validation     -  
 Generating Random Number in java     -  
 Cross platform c++ programming     -  



Topic Tags

Weka Classifiers
cron





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com