WEKA integration Learning

Source: Internet
Author: User
Tags random seed

Import java. Io .*;
Import WEKA. classifiers .*;
Import WEKA. classifiers. Meta. vote;
Import WEKA. Core. instance;
Import WEKA. Core. instances;
Import WEKA. Core. selectedtag;
Import WEKA. Core. converters. arffloader;
Public class test {

/**
* @ Param ARGs
*/
Public static void main (string [] ARGs ){
// Todo auto-generated method stub
Instances tranins = NULL; // training data
Instances testins = NULL; // Test Data
Classifier cfs1 = NULL; // classifier 1
Classifier cfs2 = NULL; // classifier 2
Classifier cfs3 = NULL; // classifier 3
Classifier [] cfsarray = new classifier [3]; // classifier Array
Try
{
File file = new file ("C: // program files // WEKA-3-6 // data // segment-challenge.arff"); // training data
Arffloader loader = new arffloader ();
Loader. setfile (File );
Tranins = loader. getdataset (); // read data

File = new file ("C: // program files // WEKA-3-6 // data // segment-test.arff"); // Test Data
Loader. setfile (File );
Testins = loader. getdataset ();

Testins. setclassindex (testins. numattributes ()-1); // you can specify the location of a category.
Tranins. setclassindex (tranins. numattributes ()-1 );

Cfs1 = (classifier) class. forname ("WEKA. classifiers. BAYes. naivebayes"). newinstance (); // Algorithm
Cfs2 = (classifier) class. forname ("WEKA. classifiers. Trees. j48"). newinstance ();
Cfs3 = (classifier) class. forname ("WEKA. classifiers. Rules. zeror"). newinstance ();
Cfsarray [0] = cfs1;
Cfsarray [1] = cfs2;
Cfsarray [2] = cfs3;

// Classifier Decision-Making Method
Vote ensemble = new vote ();
Selectedtag tag1 = new selectedtag (vote. majority_voting_rule, vote. tags_rules); // (voting)
Ensemble. setcombinationrule (tag1 );
Ensemble. setclassifiers (cfsarray );
Ensemble. setseed (2); // sets the Random Seed.
Ensemble. buildclassifier (tranins); // train the classifier.

Instance testinst;
Evaluation testingevaluation1 = new evaluation (testins); // class used to detect the classification model
Evaluation testingevaluation2 = new evaluation (testins );
Evaluation testingevaluation3 = new evaluation (testins );
Evaluation testingevaluation4 = new evaluation (testins );
Int length = testins. numinstances ();

// Learn independently
For (INT I = 0; I <length; I ++)
{
Testinst = testins. instance (I );
Testingevaluation1.evaluatemodelonceandrecordprediction (cfs1, testinst );
}
System. Out. println ("classification accuracy rate:" + (1-testingevaluation1.errorrate ()));

For (INT I = 0; I <length; I ++)
{
Testinst = testins. instance (I );
Testingevaluation2.evaluatemodelonceandrecordprediction (cfs2, testinst );
}
System. Out. println ("classification accuracy rate:" + (1-testingevaluation2.errorrate ()));

For (INT I = 0; I <length; I ++)
{
Testinst = testins. instance (I );
Testingevaluation3.evaluatemodelonceandrecordprediction (cfs3, testinst );
}
System. Out. println ("classification accuracy rate:" + (1-testingevaluation3.errorrate ()));

// Integrated Learning
For (INT I = 0; I <length; I ++)
{
Testinst = testins. instance (I );
Testingevaluation4.evaluatemodelonceandrecordprediction (ensemble, testinst );
}
System. Out. println ("classification accuracy rate:" + (1-testingevaluation4.errorrate ()));
}
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.