Machine learning weka, java api call random forest and preservation model

Keywords: Java Attribute

Work needs, understand weka's java api, mainly random forest, just started to learn, record.
I don't know much about it. Go directly to demo. There are some notes in it.

package weka;

import java.io.File;

import weka.classifiers.Classifier;
import weka.classifiers.trees.RandomForest;
import weka.core.Instances;
import weka.core.SerializationHelper;
import weka.core.converters.ArffLoader;

public class demo {

    public static void main(String[] args) throws Exception {
        
        Classifier m_classifier = new RandomForest();  
        File inputFile = new File("F:/java/weka/trainData.arff");//Training corpus file  
        ArffLoader atf = new ArffLoader();   
        atf.setFile(inputFile);  
        Instances instancesTrain = atf.getDataSet(); // Read in training documents      
        inputFile = new File("F:/java/weka/testData.arff");//Test corpus file  
        atf.setFile(inputFile);            
        Instances instancesTest = atf.getDataSet(); // Read in the test file  
        instancesTest.setClassIndex(0); //Setting the line number of the categorized attribute (No. 0 of the first action), instancesTest.numAttributes() can get the total number of attributes.  
        double sum = instancesTest.numInstances(),//Examples of test corpus  
        right = 0.0f;  
        instancesTrain.setClassIndex(0);  
        m_classifier.buildClassifier(instancesTrain); //train
        System.out.println(m_classifier);
        
        // Preservation model
        SerializationHelper.write("LibSVM.model", m_classifier);//Parameter 1 saves the file for the model, and classifier 4 saves the model.
        
        for(int  i = 0;i<sum;i++)//Test classification result 1
        {  
            if(m_classifier.classifyInstance(instancesTest.instance(i))==instancesTest.instance(i).classValue())//If the predictive value is equal to the answer value (the correct answer must be provided by the categorized column in the test corpus, then the result will be meaningful)  
            {  
                right++;//Correct value plus 1  
            }  
        } 
        
        // Get the model saved above
        Classifier classifier8 = (Classifier) weka.core.SerializationHelper.read("LibSVM.model"); 
        double right2 = 0.0f;  
        for(int  i = 0;i<sum;i++)//Test Classification Result 2 (Pass)
        {  
            if(classifier8.classifyInstance(instancesTest.instance(i))==instancesTest.instance(i).classValue())//If the predictive value is equal to the answer value (the correct answer must be provided by the categorized column in the test corpus, then the result will be meaningful)  
            {  
                right2++;//Correct value plus 1  
            }  
        } 
        System.out.println(right);
        System.out.println(right2);
        System.out.println(sum);
        System.out.println("RandomForest classification precision:"+(right/sum));  
    }
}

Original text: Brief Book Thinkin Liu Blog: IT old five
It includes the use of random forests, including training, model preservation and algorithm results. Actual use is divided into two parts, training in the background, client using the model calculation results after training. Of course, there are real-time training, which I will learn later.
Random forests may have a lot of configuration parameters to adjust, follow-up slowly to learn.

Posted by Swedie on Fri, 22 Mar 2019 12:36:53 -0700