训练弱者

bjg7j2ky  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(385)

我正在使用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

我创建了一个分类器类:

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);

在我的分类器对每头大象进行分类之后,我计算分类器错误,更新每个样本(大象)的权重,并计算分类器的权重。我的问题是:
我如何创建下一个分类器,它如何更关心错误分类的样本(我知道样本权重是关键,但它如何工作,因为我不知道如何实现它)?我是否正确创建了第一个分类器?

sr4lhrrt

sr4lhrrt1#

好吧,你可以计算错误率并对示例进行分类,但是你缺少的是对分类器的更新,并根据ada-boost公式将它们组合成一个分类器。看看这里的算法:维基百科的adaboost网页

相关问题