训练弱学习器

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

Related Posts

Keras Dense层输入未被展平

这是我的测试代码: from keras import…

无法将分类变量输入随机森林

我有10个分类变量和3个数值变量。我在分割后直接将它们…

如何在Keras中对每个输出应用Sigmoid函数?

这是我代码的一部分。 model = Sequenti…

如何选择类概率的最佳阈值?

我的神经网络输出是一个用于多标签分类的预测类概率表: …

在Keras中使用深度学习得到不同的结果

我按照一个教程使用Keras中的深度神经网络进行文本分…

‘MatMul’操作的输入’b’类型为float32,与参数’a’的类型float64不匹配

我写了一个简单的TensorFlow代码,但不断遇到T…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注