Day 63 : 集成学习之 AdaBoosting (1. 带权数据集)

63.1 AdaBoosting基本算法:先从初始训练集训练一个弱学习器,在根据弱学习器的表现对训练样本进行权重调整,经过若干轮之后,将得到一组分类器,将数据输入这组分类器后会得到一个综合且准确的的分类结果。“三个臭皮匠,顶个诸葛亮”,多个这样的弱分类器相互补充,最后会变成一个强分类器。

63.2 代码:

package dl;

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

import weka.core.Instances;

/**
 * Weighted instances.
 */
public class WeightedInstances extends Instances {

    /**
     * Just the requirement of some classes, any number is ok.
     */
    private static final long serialVersionUID = 110;

    /**
     * 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();
        for (int i = 0; i < weights.length; i++) {
            weights[i] = tempAverage;
        } // Of for i
        System.out.println("Instances weights are: " + Arrays.toString(weights));
    } // Of the first constructor

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

        // Initialize weights
        weights = new double[numInstances()];
        double tempAverage = 1.0 / numInstances();
        for (int i = 0; i < weights.length; i++) {
            weights[i] = tempAverage;
        } // Of for i
        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 instances have been correctly classified.
     * @param paraAlpha
     *            The weight of the last classifier.
     ******************
     */
    public void adjustWeights(boolean[] paraCorrectArray, double paraAlpha) {
        // Step 1. Calculate alpha.
        double tempIncrease = Math.exp(paraAlpha);

        // Step 2. Adjust.
        double tempWeightsSum = 0; // For normalization.
        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

        // Step 3. 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()];
        for (int i = 0; i < tempCorrectArray.length / 2; i++) {
            tempCorrectArray[i] = true;
        } // Of for i

        double tempWeightedError = 0.3;

        adjustWeights(tempCorrectArray, tempWeightedError);

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

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

    /**
     ******************
     * For display.
     ******************
     */
    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

    /**
     ******************
     * For unit test.
     *
     * @param args
     *            Not provided.
     ******************
     */
    public static void main(String args[]) {
        WeightedInstances tempWeightedInstances = null;
        String tempFilename = "C:\\Users\\86183\\IdeaProjects\\deepLearning\\src\\main\\java\\resources\\iris.arff";
        try {
            FileReader tempFileReader = new FileReader(tempFilename);
            tempWeightedInstances = new WeightedInstances(tempFileReader);
            tempFileReader.close();
        } catch (Exception exception1) {
            System.out.println("Cannot read the file: " + tempFilename + "\r\n" + exception1);
            System.exit(0);
        } // Of try

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

        tempWeightedInstances.adjustWeightsTest();
    } // Of main

} // Of class WeightedInstances

63.3 结果(部分)

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值