我正在使用AdaBoost实现一个应用程序,用于分类大象是亚洲象还是非洲象。我的输入数据是:
Elephant size: 235 Elephant weight: 3568 Sample weight: 0.1 Elephant type: AsianElephant size: 321 Elephant weight: 4789 Sample weight: 0.1 Elephant type: AfricanElephant size: 389 Elephant weight: 5689 Sample weight: 0.1 Elephant type: AfricanElephant size: 210 Elephant weight: 2700 Sample weight: 0.1 Elephant type: AsianElephant size: 270 Elephant weight: 3654 Sample weight: 0.1 Elephant type: AsianElephant size: 289 Elephant weight: 3832 Sample weight: 0.1 Elephant type: AfricanElephant size: 368 Elephant weight: 5976 Sample weight: 0.1 Elephant type: AfricanElephant size: 291 Elephant weight: 4872 Sample weight: 0.1 Elephant type: AsianElephant size: 303 Elephant weight: 5132 Sample weight: 0.1 Elephant type: AfricanElephant size: 246 Elephant weight: 2221 Sample weight: 0.1 Elephant type: African
我创建了一个分类器类:
import java.util.ArrayList;public class Classifier {private String feature;private int treshold;private double errorRate;private double classifierWeight;public void classify(Elephant elephant){ if(feature.equals("size")){ if(elephant.getSize()>treshold){ elephant.setClassifiedAs(ElephantType.African); } else{ elephant.setClassifiedAs(ElephantType.Asian); } } else if(feature.equals("weight")){ if(elephant.getWeight()>treshold){ elephant.setClassifiedAs(ElephantType.African); } else{ elephant.setClassifiedAs(ElephantType.Asian); } }}public void countErrorRate(ArrayList<Elephant> elephants){ double misclassified = 0; for(int i=0;i<elephants.size();i++){ if(elephants.get(i).getClassifiedAs().equals(elephants.get(i).getType()) == false){ misclassified++; } } this.setErrorRate(misclassified/elephants.size());}public void countClassifierWeight(){ this.setClassifierWeight(0.5*Math.log((1.0-errorRate)/errorRate));}public Classifier(String feature, int treshold){ setFeature(feature); setTreshold(treshold);}
我在main()中训练了一个基于“size”特征和阈值为250的分类器,就像这样:
main.trainAWeakClassifier("size", 250);
在我的分类器对每头大象进行分类后,我计算分类器的错误率,更新每个样本(即大象)的权重,并计算分类器的权重。我的问题是:
我该如何创建下一个分类器?它如何更加关注被错误分类的样本(我知道样本权重是关键,但它是如何工作的,因为我不知道如何实现它)?我是否正确地创建了第一个分类器?
回答:
嗯,你计算了错误率并且可以对实例进行分类,但你缺少的是更新分类器并根据AdaBoost公式将它们组合成一个。请查看此处的算法:维基百科的AdaBoost网页