训练弱学习者

时间:2012-08-25 16:12:11

标签: java machine-learning artificial-intelligence classification adaboost

我正在使用AdaBoost实施一个应用程序来分类大象是亚洲还是非洲大象。我的输入数据是:

Elephant size: 235  Elephant weight: 3568  Sample weight: 0.1  Elephant type: Asian
Elephant size: 321  Elephant weight: 4789  Sample weight: 0.1  Elephant type: African
Elephant size: 389  Elephant weight: 5689  Sample weight: 0.1  Elephant type: African
Elephant size: 210  Elephant weight: 2700  Sample weight: 0.1  Elephant type: Asian
Elephant size: 270  Elephant weight: 3654  Sample weight: 0.1  Elephant type: Asian
Elephant size: 289  Elephant weight: 3832  Sample weight: 0.1  Elephant type: African
Elephant size: 368  Elephant weight: 5976  Sample weight: 0.1  Elephant type: African
Elephant size: 291  Elephant weight: 4872  Sample weight: 0.1  Elephant type: Asian
Elephant size: 303  Elephant weight: 5132  Sample weight: 0.1  Elephant type: African
Elephant size: 246  Elephant weight: 2221  Sample weight: 0.1  Elephant type: African

我创建了一个Classifier类:

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()中训练了一个分类器,按“大小”分类,treshold = 250就像这样:

 main.trainAWeakClassifier("size", 250);

在我的分类器对每只大象进行分类后,我会计算分类器错误,更新每个样本(大象)的权重并计算分类器的权重。我的问题是:

如何创建下一个分类器以及它如何更多地关注错误分类的样本(我知道样本权重是关键,但它是如何工作的,因为我不知道如何实现它)? 我是否正确创建了第一个分类器?

1 个答案:

答案 0 :(得分:0)

那么,你计算错误率并可以对实例进行分类,但你缺少的是分类器的更新,并根据Ada Boost公式将它们组合成一个。 看看这里的算法: Wikipedia's Ada Boost webpage