forked from sPredictorX1708/Ultimate-Java-Resources
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandomForest.java
More file actions
67 lines (55 loc) · 2.06 KB
/
RandomForest.java
File metadata and controls
67 lines (55 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import java.io.File;
import weka.classifiers.Classifier;
import weka.classifiers.Evaluation;
import weka.core.Instances;
import weka.core.converters.ArffLoader;
import weka.filters.Filter;
import weka.filters.unsupervised.attribute.StringToWordVector;
import java.io.IOException;
import weka.classifiers.trees.RandomForest;
public class RandomForest {
/** file names are defined*/
public static final String TRAINING_DATA_SET_FILENAME="decision-train.arff";
public static final String TESTING_DATA_SET_FILENAME="decision-test.arff";
/**
* This method is to load the data set.
* @param fileName
* @return
* @throws IOException
*/
public static Instances getDataSet(String fileName) throws IOException {
/**
* we can set the file i.e., loader.setFile("finename") to load the data
*/
int classIdx = 1;
/** the arffloader to load the arff file */
ArffLoader loader = new ArffLoader();
/** load the traing data */
loader.setSource(RandomForestDemo.class.getResourceAsStream("/" + fileName));
/**
* we can also set the file like loader3.setFile(new
* File("test-confused.arff"));
*/
//loader.setFile(new File(fileName));
Instances dataSet = loader.getDataSet();
/** set the index based on the data given in the arff files */
dataSet.setClassIndex(classIdx);
return dataSet;
}
public static void process() throws Exception {
Instances trainingDataSet = getDataSet(TRAINING_DATA_SET_FILENAME);
Instances testingDataSet = getDataSet(TESTING_DATA_SET_FILENAME);
RandomForest forest=new RandomForest();
forest.setNumTrees(10);
forest.buildClassifier(trainingDataSet);
Evaluation eval = new Evaluation(trainingDataSet);
eval.evaluateModel(forest, testingDataSet);
/** Print the algorithm summary */
System.out.println("** Decision Tress Evaluation with Datasets **");
System.out.println(eval.toSummaryString());
System.out.print(" the expression for the input data as per alogorithm is ");
System.out.println(forest);
System.out.println(eval.toMatrixString());
System.out.println(eval.toClassDetailsString());
}
}