日撸java_day63-65

文章目录

  • Booster
    • 代码
    • 运行截图

Booster

代码

package machineLearning.adaboosting;

import weka.core.Instances;

import java.io.FileReader;
import java.util.Arrays;

/**
 * ClassName: WeightedInstances
 * Package: machineLearning.adaboosting
 * Description:Weighted instances.
 *
 * @Author: luv_x_c
 * @Create: 2023/8/13 15:12
 */
public class WeightedInstances extends Instances {
    /**
     * Just the requirement of some classes, any number is ok.
     */
    private static final long serialVersionUID = 11087456L;

    /**
     * Weights.
     */
    private double[] weights;

    /**
     * The first constructor.
     *
     * @param paraFileReader The given reader to read data from file.
     */
    public WeightedInstances(FileReader paraFileReader) throws Exception {
        super(paraFileReader);
        setClassIndex(numAttributes() - 1);

        //Initialize weights.
        weights = new double[numInstances()];
        double tempAverage = 1.0 / numInstances();
        Arrays.fill(weights, tempAverage);
        System.out.println("Instances weights are: " + Arrays.toString(weights));
    }//Of the first constructor

    /**
     * The second constructor.
     *
     * @param paraInstances The given instances.
     */
    public WeightedInstances(Instances paraInstances) {
        super(paraInstances);
        setClassIndex(numAttributes() - 1);

        // Initialize weights.
        weights = new double[numInstances()];
        double tempAverage = 1.0 / numInstances();
        Arrays.fill(weights, tempAverage);
        System.out.println("Instances weights are: " + Arrays.toString(weights));
    }//Of the second constructor

    /**
     * Getter.
     *
     * @param paraIndex The given index.
     * @return The weight of the given index.
     */
    public double getWeight(int paraIndex) {
        return weights[paraIndex];
    }//Of getWeight

    /**
     * Adjust the weights.
     *
     * @param paraCorrectArray Indicate which instance have been correctly classify.
     * @param paraAlpha        The weight of the last classifier.
     */
    public void adjustWeights(boolean[] paraCorrectArray, double paraAlpha) {
        //Step1. Calculate alpha.
        double tempIncrease = Math.exp(paraAlpha);

        // Step2. Adjust.
        double tempWeightsSum = 0;
        for (int i = 0; i < weights.length; i++) {
            if (paraCorrectArray[i]) {
                weights[i] /= tempIncrease;
            } else {
                weights[i] *= tempIncrease;
            }//Of if
            tempWeightsSum += weights[i];
        }//Of for i

        // Step3. Normalize.
        for (int i = 0; i < weights.length; i++) {
            weights[i] /= tempWeightsSum;
        }//Of for i

        System.out.println("After adjusting, instances weights are: " + Arrays.toString(weights));
    }//Of adjustWeights

    /**
     * Test the method.
     */
    public void adjustWeightsTest() {
        boolean[] tempCorrectArray = new boolean[numInstances()];
        Arrays.fill(tempCorrectArray, true);

        double tempWeightError = 0.3;

        adjustWeights(tempCorrectArray, tempWeightError);

        System.out.println("After adjusting: ");

        System.out.println(toString());
    }// Of adjustWeightsTest

    @Override
    public String toString() {
        String resultString =
                "I am a weighted Instances object.\r\n" + " I have " + numInstances() + " " +
                        "instances and " + (numAttributes() - 1) + " conditional attributes.\r\n" +
                        " My weights are: " + Arrays.toString(weights) + "\r\n" +
                        " My data are:\r\n" + super.toString();
        return resultString;
    }//Of toString

    /**
     * @param args Not provided.
     */
    public static void main(String[] args) {
        WeightedInstances tempWeightInstances = null;
        String tempFileName = "E:\\java_code\\data\\sampledata\\iris.arff";

        try {
            FileReader fileReader = new FileReader(tempFileName);
            tempWeightInstances = new WeightedInstances(fileReader);
            fileReader.close();
        } catch (Exception ee) {
            System.out.println("Cannot read the file: " + tempFileName + "\r\n" + ee);
            System.exit(0);
        }//Of try

        System.out.println(tempWeightInstances.toString());

        tempWeightInstances.adjustWeightsTest();
    }//Of main
}//OF class WeightedInstances

package machineLearning.adaboosting;

import weka.core.Instance;

import java.io.FileReader;
import java.util.Arrays;

/**
 * ClassName: StumpClassifier
 * Package: machineLearning.adaboosting
 * Description:The stump classifier.
 *
 * @Author: luv_x_c
 * @Create: 2023/8/17 20:37
 */
public class StumpClassifier extends SimpleClassifier {
    /**
     * The best cut for the current attribute on weightInstances.
     */
    double bestCut;

    /**
     * The class label for attribute value less than bestCut.
     */
    int leftLeafLabel;

    /**
     * The class label for attribute value no less than bestCut.
     */
    int rightLeafLabel;

    /**
     * The only constructor.
     *
     * @param paraWeightedInstances The given instances.
     */
    public StumpClassifier(WeightedInstances paraWeightedInstances) {
        super(paraWeightedInstances);
    }//Of the only constructor

    public void train() {
        //Step1. Randomly choose an attribute.
        selectedAttribute = random.nextInt(numConditions);

        //Step2. Find all attributes values and sort.
        double[] tempValuesArray = new double[numInstances];
        for (int i = 0; i < tempValuesArray.length; i++) {
            tempValuesArray[i] = weightedInstances.instance(i).value(selectedAttribute);
        }//Of for i
        Arrays.sort(tempValuesArray);

        //Step3. Initialize, classify all instances as the same with the original cut.
        int tempNumLabels = numClasses;
        double[] tempLabelCountArray = new double[tempNumLabels];
        int tempCurrentLabel;

        //Step3.1 Scan all labels to obtain their counts.
        for (int i = 0; i < numInstances; i++) {
            // The label of the ith instance
            tempCurrentLabel = (int) weightedInstances.instance(i).classValue();
            tempLabelCountArray[tempCurrentLabel] += weightedInstances.getWeight(i);
        }//Of for i

        //Step3.2 Find the label with the maximal count.
        double tempMaxCorrect = 0;
        int tempBestLabel = -1;
        for (int i = 0; i < tempLabelCountArray.length; i++) {
            if (tempMaxCorrect < tempLabelCountArray[i]) {
                tempMaxCorrect = tempLabelCountArray[i];
                tempBestLabel = i;
            }//Of if
        }//Of for i

        //Steep3.3 The cut is a little  smaller than the minimal value.
        bestCut = tempValuesArray[0] - 0.1;
        leftLeafLabel = tempBestLabel;
        rightLeafLabel = tempBestLabel;

        // Step4. Check candidate cuts one by one.
        // Step4.1 To handle multi-class data, left and right.
        double tempCut;
        double[][] tempLabelCountMatrix = new double[2][tempNumLabels];

        for (int i = 0; i < tempValuesArray.length - 1; i++) {
            // Step4.1 Some attribute values are identical, ignore them.
            if (tempValuesArray[i] == tempValuesArray[i + 1]) {
                continue;
            }//Of if
            tempCut = (tempValuesArray[i] + tempValuesArray[i + 1]) / 2;

            // Step4.2 Scan all labels to obtain their counts wrt, the cut .
            // Initialize again since it is used many times.
            for (int j = 0; j < 2; j++) {
                for (int k = 0; k < tempNumLabels; k++) {
                    tempLabelCountMatrix[j][k] = 0;
                }//Of for k
            }//Of for j

            for (int j = 0; j < numInstances; j++) {
                // The label of the jth instance.
                tempCurrentLabel = (int) weightedInstances.instance(j).classValue();
                if (weightedInstances.instance(j).value(selectedAttribute) < tempCut) {
                    tempLabelCountMatrix[0][tempCurrentLabel] += weightedInstances.getWeight(j);
                } else {
                    tempLabelCountMatrix[1][tempCurrentLabel] += weightedInstances.getWeight(j);
                }//Of if
            }//Of for j

            // Step4.3 Left leaf
            double tempLeftMaxCorrect = 0;
            int tempLeftBestLabel = 0;
            for (int j = 0; j < tempLabelCountMatrix[0].length; j++) {
                if (tempLeftMaxCorrect < tempLabelCountMatrix[0][j]) {
                    tempLeftMaxCorrect = tempLabelCountMatrix[0][j];
                    tempLeftBestLabel = j;
                }//Of if
            }//Of for j

            // Step 4.4 Right leaf
            double tempRightMaxCorrect = 0;
            int tempRightBestLabel = 0;
            for (int j = 0; j < tempLabelCountMatrix[0].length; j++) {
                if (tempRightMaxCorrect < tempLabelCountMatrix[1][j]) {
                    tempRightMaxCorrect = tempLabelCountMatrix[1][j];
                    tempRightBestLabel = j;
                }//Of if
            }//Of for j

            // Step 4.5 Compare with the current best
            if (tempMaxCorrect < tempLeftMaxCorrect + tempRightMaxCorrect) {
                tempMaxCorrect = tempLeftMaxCorrect + tempRightMaxCorrect;
                bestCut = tempCut;
                leftLeafLabel = tempLeftBestLabel;
                rightLeafLabel = tempRightBestLabel;
            }//Of if
        }//Of for i

        System.out.println("Attribute = " + selectedAttribute + ", cut =" + bestCut + ", " +
                "leftLeafLabel = " + leftLeafLabel + ", rightLeafLabel" + rightLeafLabel);
    }//Of train


    @Override
    public int classify(Instance paraInstance) {
        int resultLabel = -1;
        if (paraInstance.value(selectedAttribute) < bestCut) {
            resultLabel = leftLeafLabel;
        } else {
            resultLabel = rightLeafLabel;
        }//Of if
        return resultLabel;
    }//Of classify

    @Override
    public String toString() {
        return "I am a stump classifier.\r\n" + "I choose attribute #" + selectedAttribute
                + " with cut value " + bestCut + ".\r\n" + "The left and right leaf labels are " + leftLeafLabel
                + " and " + rightLeafLabel + ", respectively.\r\n" + "My weighted error is: " + computeWeightedError()
                + ".\r\n" + "My weighted accuracy is : " + computeTrainingAccuracy() + ".";
    }//Of toString

    /**
     * For unit test.
     *
     * @param args Not used.
     */
    public static void main(String[] args) {
        WeightedInstances tempWeightedInstance = null;
        String tempFileName = "E:\\java_code\\data\\sampledata\\iris.arff";
        try {
            FileReader fileReader = new FileReader(tempFileName);
            tempWeightedInstance = new WeightedInstances(fileReader);
            fileReader.close();
        } catch (Exception e) {
            System.out.println("Cannot read the file: " + tempFileName + "\r\n" + e);
            System.exit(0);
        }//OF try

        StumpClassifier tempClassifier = new StumpClassifier(tempWeightedInstance);
        tempClassifier.train();
        System.out.println(tempClassifier);

        System.out.println(Arrays.toString(tempClassifier.computeCorrectnessArray()));
    }//OF main
}//Of class StumpClassifier

package machineLearning.adaboosting;

import weka.core.Instance;

import java.util.Random;

/**
 * ClassName: SimpleClassifier
 * Package: machineLearning.adaboosting
 * Description:The super class of any simple classifier.
 *
 * @Author: luv_x_c
 * @Create: 2023/8/14 13:43
 */
public abstract class SimpleClassifier {
    /**
     * The index of the current attribute.
     */
    int selectedAttribute;

    /**
     * Weighted data.
     */
    WeightedInstances weightedInstances;

    /**
     * The accuracy on the training set.
     */
    double trainingAccuracy;

    /**
     * The number of instances.
     */
    int numInstances;

    /**
     * The number of instances.
     */
    int numClasses;

    /**
     * The number of conditional attributes.
     */
    int numConditions;

    Random random = new Random();

    /**
     * The first constructor.
     *
     * @param paraWeightedInstances The given instances.
     */
    public SimpleClassifier(WeightedInstances paraWeightedInstances) {
        weightedInstances = paraWeightedInstances;

        numConditions = weightedInstances.numAttributes() - 1;
        numInstances = weightedInstances.numInstances();
        numClasses = weightedInstances.classAttribute().numValues();
    }// Of the first constructor

    /**
     * Train the classifier.
     */
    public abstract void train();

    /**
     * Classify an instance.
     *
     * @param paraInstance The given instance.
     * @return Predicted label.
     */
    public abstract int classify(Instance paraInstance);

    /**
     * Which instance in the  training set are correctly classified.
     *
     * @return The correctness array.
     */
    public boolean[] computeCorrectnessArray() {
        boolean[] resultCorrectnessArray = new boolean[weightedInstances.numInstances()];
        for (int i = 0; i < resultCorrectnessArray.length; i++) {
            Instance tempInstance = weightedInstances.instance(i);
            if ((int) (tempInstance.classValue()) == classify(tempInstance)) {
                resultCorrectnessArray[i] = true;
            }// OF if
        }//Of for i

        return resultCorrectnessArray;
    }//Of computeCorrectnessArray

    /**
     * Compute the accuracy on the training set.
     *
     * @return The training accuracy.
     */
    public double computeTrainingAccuracy() {
        double tempCorrect = 0;
        boolean[] tempCorrectnessArray = computeCorrectnessArray();
        for (int i = 0; i < tempCorrectnessArray.length; i++) {
            if (tempCorrectnessArray[i]) {
                tempCorrect++;
            }//Of if
        }//Of for i

        double resultAccuracy = tempCorrect / tempCorrectnessArray.length;

        return resultAccuracy;
    }//Of computeTrainingAccuracy

    public double computeWeightedError(){
        double resultError=0;
        boolean[]tempCorrectnessArray=computeCorrectnessArray();
        for (int i = 0; i < tempCorrectnessArray.length; i++) {
            if(!tempCorrectnessArray[i]){
                resultError+=weightedInstances.getWeight(i);
            }//Of if
        }//Of for i

        if(resultError<1e-6){
            resultError=1e-6;
        }//Of if

        return resultError;
    }//Of computeWeightedError
}//Of class SimpleClassifier
package machineLearning.adaboosting;

import weka.core.Instance;
import weka.core.Instances;

import java.io.FileReader;
import java.io.IOException;

/**
 * ClassName: Booster
 * Package: machineLearning.adaboosting
 * Description: The booster which ensembles base classifiers.
 *
 * @Author: luv_x_c
 * @Create: 2023/8/18 14:34
 */
public class Booster {
    /**
     * Classifiers.
     */
    SimpleClassifier[] classifiers;

    /**
     * Number of classifiers.
     */
    int numClassifiers;

    /**
     * Whether stop after the training error is 0.
     */
    boolean stopAfterConverge = false;

    /**
     * The weights of classifier.
     */
    double[] classifierWeights;

    /**
     * The training data.
     */
    Instances trainingData;

    /**
     * The testing data.
     */
    Instances testingData;

    /**
     * The first constructor. The testing set is the same as the training data.
     *
     * @param paraTrainingFileName The data file name.
     */
    public Booster(String paraTrainingFileName) {
        // Step1. Read the training set.
        try {
            FileReader fileReader = new FileReader(paraTrainingFileName);
            trainingData = new Instances(fileReader);
            fileReader.close();
        } catch (IOException e) {
            System.out.println("Cannot read the file: " + paraTrainingFileName + "\r\n" + e);
            System.exit(0);
        }//Of try

        // Step2. Set the last attribute as the class index
        trainingData.setClassIndex(trainingData.numAttributes() - 1);

        // Step3. The testing data is the same as the training data
        testingData = trainingData;

        stopAfterConverge = true;

        System.out.println("****************Data**********\r\n" + trainingData);
    }//Of the first constructor

    /**
     * Set the number of base classifier, and allocate space for them.
     *
     * @param paraNumBaseClassifiers The number of base classifier.
     */
    public void setNumBaseClassifiers(int paraNumBaseClassifiers) {
        numClassifiers = paraNumBaseClassifiers;

        // Step1. Allocate space for classifiers
        classifiers = new SimpleClassifier[numClassifiers];

        // Step2. Initialize classifier weights
        classifierWeights = new double[numClassifiers];
    }//Of setNumBaseClassifiers

    /**
     * Train the booster.
     */
    public void train() {
        // Step1. Initialize.
        WeightedInstances tempWeightedInstances = null;
        double tempError;
        numClassifiers = 0;

        // Step2. Build other classifier.
        for (int i = 0; i < classifiers.length; i++) {
            // Step2.1 Key code: Construct or adjust the weightedInstances
            if (i == 0) {
                tempWeightedInstances = new WeightedInstances(trainingData);
            } else {
                // Adjust the weights of the data
                tempWeightedInstances.adjustWeights(classifiers[i - 1].computeCorrectnessArray(),
                        classifierWeights[i - 1]);
            }//Of if

            // Step 2.2 Train the next classifier.
            classifiers[i] = new StumpClassifier(tempWeightedInstances);
            classifiers[i].train();

            tempError = classifiers[i].computeWeightedError();

            // Key code: Set the classifier weight.
            classifierWeights[i] = 0.5 * Math.log(1 / tempError - 1);
            if (classifierWeights[i] < 1e-6) {
                classifierWeights[i] = 0;
            }//Of if

            System.out.println("Classifier #" + i + " , weighted error = " + tempError + " , " +
                    "weight = " + classifierWeights[i] + "\r\n");

            numClassifiers++;

            // The accuracy is enough.
            if (stopAfterConverge) {
                double tempTrainAccuracy = computeTrainingAccuracy();
                System.out.println("The accuracy of the booster is: " + tempTrainAccuracy + "\r\n");
                if (tempTrainAccuracy > 0.99999) {
                    System.out.println("Stop at the round: " + i + " due to converge.\r\n");
                    break;
                }//Of if
            }//of if
        }//Of for i
    }//Of for train

    /**
     * Classify an instance
     *
     * @param paraInstance The given instance
     * @return The predicted label
     */
    public int classify(Instance paraInstance) {
        double[] tempLabelCountArray = new double[testingData.classAttribute().numValues()];
        for (int i = 0; i < numClassifiers; i++) {
            int tempLabel = classifiers[i].classify(paraInstance);
            tempLabelCountArray[tempLabel] += classifierWeights[i];
        }//Of for i

        int resultLabel = -1;
        double tempMax = -1;
        for (int i = 0; i < tempLabelCountArray.length; i++) {
            if (tempMax < tempLabelCountArray[i]) {
                tempMax = tempLabelCountArray[i];
                resultLabel = i;
            }//Of if
        }//Of for i

        return resultLabel;
    }//Of classify

    /**
     * Test the booster on the training data.
     *
     * @return The classification accuracy.
     */
    public double test() {
        System.out.println("Testing on " + testingData.numInstances() + " instances\r\n");

        return test(testingData);
    }//Of test

    /**
     * Test the booster.
     *
     * @param paraInstances The testing set.
     * @return The classification accuracy.
     */
    public double test(Instances paraInstances) {
        double tempCorrect = 0;
        paraInstances.setClassIndex(paraInstances.numAttributes() - 1);

        for (int i = 0; i < paraInstances.numInstances(); i++) {
            Instance tempInstance = paraInstances.instance(i);
            if (classify(tempInstance) == (int) tempInstance.classValue()) {
                tempCorrect++;
            }//Of if
        }//Of for i

        double resultAccuracy = tempCorrect / paraInstances.numInstances();
        System.out.println("The accuracy is: " + resultAccuracy);

        return resultAccuracy;
    }//Of test

    /**
     * Compute the training accuracy of the booster. It is not weighted.
     *
     * @return The training accuracy.
     */
    public double computeTrainingAccuracy() {
        double tempCorrect = 0;

        for (int i = 0; i < trainingData.numInstances(); i++) {
            if (classify(trainingData.instance(i)) == (int) trainingData.instance(i).classValue()) {
                tempCorrect++;
            }//Of if
        }//Of for i

        double tempAccuracy = tempCorrect / trainingData.numInstances();
        return tempAccuracy;
    }//Of computeTrainingAccuracy

    /**
     * The entrance of the program.
     *
     * @param args Not used now.
     */
    public static void main(String[] args) {
        System.out.println("Starting Adaboosting ..");
        Booster tempBooster = new Booster("E:\\java_code\\data\\sampledata\\iris.arff");

        tempBooster.setNumBaseClassifiers(5);
        tempBooster.train();

        System.out.println("The training accuracy is: " + tempBooster.computeTrainingAccuracy());
        tempBooster.test();
    }//Of main
}//Of class Booster

运行截图

日撸java_day63-65_第1张图片

你可能感兴趣的:(java,python,开发语言)