This time we will introduce the evaluation class. In the last time, we simply predicted the classification value, and there was no other evaluation data. In this case, we use the evalution class. First, initialize an evaluation object. The evaluation class has no constructor without any parameters. Generally, the instances object is used as the constructor parameter. If we do not have training data or test data, we can use the cross validation method, that is, cross verification. The four parameters of the Cross validatemodel method are: the first is the classifier, and the second is the dataset evaluated on a specific dataset, the third parameter is the number of cross-checks (10 is more common), and the fourth is a random number object. If you have a training set and a test set, you can use the evaluatemodel method in the evaluation class. The parameters in the method are as follows: the first one is a trained classifier, the second parameter is the dataset evaluated on a dataset.
Package instancetest;
Import WEKA. Core. instances;
Import WEKA. classifiers. Trees. j48;
Import WEKA. classifiers. Evaluation;
Import java. Io .*;
Import java. util. Random;
Public class instancetest {
/**
* @ Param ARGs
*/
Public static instances data; // training data
Public static instances testdata; // Test Data
// Obtain the dataset
Public instances gettestdate (string file, Boolean flag) throws exception
{
Filereader reader = new filereader (File );
If (! Flag)
{
Testdata = new instances (Reader );
Return testdata;
}
Else
{
Data = new instances (Reader );
Return data;
}
}
// Set the prediction category to the last one by default.
Public void setclassindex (instances INS)
{
INS. setclassindex (INS. numattributes ()-1 );
}
/*
Public void classify () throws exception
{
J48 classify = new j48 ();
Classify. buildclassifier (data );
System. Out. println (classify. classifyinstance (data. instance (0 )));
}
*/
Public void crossvalidation () throws exception
{
J48 classify = new j48 ();
Evaluation EVAL = new evaluation (data );
Eval. crossvalidatemodel (classify, Data, 10, new random (1 ));
System. Out. println (eval. toclassdetailsstring ());
System. Out. println (eval. tosummarystring ());
System. Out. println (eval. tomatrixstring ());
}
Public void validation () throws exception
{
J48 classify = new j48 ();
Classify. buildclassifier (data );
Evaluation EVAL = new evaluation (testdata );
Eval. evaluatemodel (classify, testdata );
System. Out. println (eval. toclassdetailsstring ());
System. Out. println (eval. tosummarystring ());
System. Out. println (eval. tomatrixstring ());
}
Public static void main (string [] ARGs ){
Try
{
Instancetest test = new instancetest ();
Test. gettestdate ("C: // program files // WEKA-3-7 // data // segment-challenge.arff", true );
Test. gettestdate ("C: // program files // WEKA-3-7 // data // segment-test.arff", false );
Test. setclassindex (data );
Test. setclassindex (testdata );
Test. validation ();
}
Catch (exception E)
{
E. printstacktrace ();
}
}
}