WEKA entry 3

Source: Internet
Author: User

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

}

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.