AdaBoosting :一个把多个弱分类器(分类精度略高于50%)叠加成为强分类器(分类精度高于90%)的算法。
其步骤如下:
初始化训练数据的权值。训练弱分类器。如某个训练样本能被弱分类器准确分类,那么在构造下一个训练集时,其对应的权值减小。若被错误分类,那么权重增大。权值更新过的训练集会被用于训练下一个分类器。组合所有的弱分类器为一个强分类器。加大分类误差率小的弱分类器的权重,使其在最终的分类函数中起着较大的决定作用,而降低分类误差率大的弱分类器的权重,使其在最终的分类函数中起着较小的决定作用。
今天的代码为集成器的内容。
集成器的主要步骤如下:
- 首先为训练集中的每个样本赋予相等的权值。
- 第一个基分类器为样本进行分类,并进行训练集中的样本的权值调整。(分类正确的样本权值降低,分类错误的权值上升)
- 将权值调整过的训练集传入下一个基分类器进行训练。
- 重复2,3步。
最后获得各个基分类器分类错误的权值总和。若一个基分类器分类错误的权值越大,那么在集成后进行标签投票时的权值越小。
输入:arff数据集。
输出:各个基分类器随机选择的属性,进行调整后的训练集中的各样本的权值以及分类错误率。集成器的最终准确率。
优化目标:可能没有优化目标。
package knn5;
import java.io.FileReader;
import weka.core.Instance;
import weka.core.Instances;
public class Booster {
SimpleClassifier[] classifiers;
int numClassifiers;
boolean stopAfterConverge = false;
double[] classifierWeights;
Instances trainingData;
Instances testingData;
public Booster(String paraTrainingFilename) {
try {
FileReader tempFileReader = new FileReader(paraTrainingFilename);
trainingData = new Instances(tempFileReader);
tempFileReader.close();
} catch (Exception ee) {
System.out.println("Cannot read the file: " + paraTrainingFilename + "\r\n" + ee);
System.exit(0);
} // Of try
trainingData.setClassIndex(trainingData.numAttributes() - 1);
testingData = trainingData;
stopAfterConverge = true;
System.out.println("****************Data**********\r\n" + trainingData);
}// Of the first constructor
public void setNumBaseClassifiers(int paraNumBaseClassifiers) {
numClassifiers = paraNumBaseClassifiers;
classifiers = new SimpleClassifier[numClassifiers];
classifierWeights = new double[numClassifiers];
}// Of setNumBaseClassifiers
public void train() {
WeightedInstances tempWeightedInstances = null;
double tempError;
numClassifiers = 0;
for (int i = 0; i < classifiers.length; i++) {
if (i == 0) {
tempWeightedInstances = new WeightedInstances(trainingData);
} else {
tempWeightedInstances.adjustWeights(classifiers[i - 1].computeCorrectnessArray(),
classifierWeights[i - 1]);
} // Of if
classifiers[i] = new StumpClassifier(tempWeightedInstances);
classifiers[i].train();
tempError = classifiers[i].computeWeightedError();
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++;
if (stopAfterConverge) {
double tempTrainingAccuracy = computeTrainingAccuray();
System.out.println("The accuracy of the booster is: " + tempTrainingAccuracy + "\r\n");
if (tempTrainingAccuracy > 0.999999) {
System.out.println("Stop at the round: " + i + " due to converge.\r\n");
break;
} // Of if
} // Of if
} // Of for i
}// Of train
public int classify(Instance paraInstance) {
double[] tempLabelsCountArray = new double[trainingData.classAttribute().numValues()];
for (int i = 0; i < numClassifiers; i++) {
int tempLabel = classifiers[i].classify(paraInstance);
tempLabelsCountArray[tempLabel] += classifierWeights[i];
} // Of for i
int resultLabel = -1;
double tempMax = -1;
for (int i = 0; i < tempLabelsCountArray.length; i++) {
if (tempMax < tempLabelsCountArray[i]) {
tempMax = tempLabelsCountArray[i];
resultLabel = i;
} // Of if
} // Of for
return resultLabel;
}// Of classify
public double test() {
return test(testingData);
}// Of test
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
public double computeTrainingAccuray() {
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 computeTrainingAccuray
public static void main(String[] args) {
System.out.println("Starting AdaBoosting...");
Booster tempBooster = new Booster("C:\\\\Users\\\\ASUS\\\\Desktop\\\\文件\\\\iris.arff");
tempBooster.setNumBaseClassifiers(100);
tempBooster.train();
System.out.println("The training accuracy is: " + tempBooster.computeTrainingAccuray());
tempBooster.test();
}// Of main
}// Of class Booster
运行截图: