WEKA entry 2

Source: Internet
Author: User

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 ();
}
}

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.