Now we will introduce how to use WEKA to classify data. To classify data, you must specify the column as the prediction category. Because of the data file format, the category is generally the last attribute column. We can use setclassindex to set the category. Then we need to select a classifier. There are many classifier types. We use j48 classifier for the time being. You can use buildclassifier to train the data, and then use classifyinstance to view the predicted category value of the training data. Of course, the predicted category is represented by a number, for example, 0, 1, 2... which indicates that the predicted value belongs to the nth category. For example, if the category value is {sunny, rainy}, 0 indicates sunny, and 1 indicates Rainy.
Package instancetest;
Import WEKA. Core. instances;
Import WEKA. classifiers. Trees. j48;
Import WEKA. classifiers. Trees. j48 .*;
Import java. Io .*;
Public class instancetest {
/**
* @ Param ARGs
*/
Public instances data;
// Set the prediction category to the last one by default.
Public void setclassindex (instances INS)
{
INS. setclassindex (INS. numattributes ()-1 );
}
Public instances getinstance (string filename) throws exception
{
Filereader reader = new filereader (filename );
Data = new instances (Reader );
Return data;
}
Public void classify () throws exception
{
J48 classify = new j48 ();
Classify. buildclassifier (data );
System. Out. println (classify. classifyinstance (data. instance (0 )));
}
Public static void main (string [] ARGs ){
Try
{
Instancetest test = new instancetest ();
Instances date = test. getinstance ("D: // train. ARFF ");
Test. setclassindex (date );
Test. classify ();
}
Catch (exception E)
{
E. printstacktrace ();
}
}
}