代码来自闵老师”日撸 Java 三百行(61-70天)
日撸 Java 三百行(61-70天,决策树与集成学习)_闵帆的博客-优快云博客
学习过程中理解算法参考了:(十三)通俗易懂理解——Adaboost算法原理 - 知乎 (zhihu.com)
今天的代码的核心是方法adjustWeights(boolean[] paraCorrectArray, double paraAlpha)。分类正确的实例,权重调整为原值除以Math.exp(paraAlpha);分类错误的调整为原值乘以Math.exp(paraAlpha)。
测试方法里,之所以for循环到tempCorrectArray长度的一半,仅仅是为了测试。相当于设置弱分类器分类正确了一半。
package machinelearning.adaboosting;
import java.io.FileReader;
import java.util.Arrays;
import weka.core.Instances;
/**
* Weighted instances.<br>
*
* @author WX873
*
*/
public class WeightedInstances extends Instances{
/**
* Just the requirement of some classes, any number is ok.
*/
private static final long serialVersionUID = 11087456L;
/**
* Weights
*/
private double[] weights;
/**
* **************************************************
* The first constructor.
*
* @param paraFileReader The given reader to read data from file.
* @throws Exception
* **************************************************
*/
public WeightedInstances(FileReader paraFileReader) throws Exception{
// TODO Auto-generated constructor stub
super(paraFileReader);
setClassIndex(numAttributes() - 1);
// Initialize weights
weights = new double[numInstances()];
double tempAverage = 1.0/numInstances();
for (int i = 0; i < weights.length; i++) {
weights[i] = tempAverage;
}//of for i
System.out.println("Instances weights are: " + Arrays.toString(weights));
}//of the first constructor
/**
* **********************************************************
* The second constructor.
*
* @param paraInstances
* **********************************************************
*/
public WeightedInstances(Instances paraInstances) {
// TODO Auto-generated constructor stub
super(paraInstances);
setClassIndex(numAttributes() - 1);
//Initialize weights
weights = new double[numInstances()];
double tempAverage = 1.0/numInstances();
for (int i = 0; i < weights.length; i++) {
weights[i] = tempAverage;
}//of for i
System.out.println("Instances weights are: " + Arrays.toString(weights));
}//of the second constructor
/**
* ***************************************************
* Getter.
*
* @param paraIndex The given index.
* @return The weight of the given index.
* ***************************************************
*/
public double getWeight(int paraIndex) {
return weights[paraIndex];
}//of getWeight
/**
* *****************************************************
* Adjust the weights.
*
* @param paraCorrectArray Indicate which instances have been correctly classified.
* @param paraAlpha The weight of the last classifier.
* *****************************************************
*/
public void adjustWeights(boolean[] paraCorrectArray, double paraAlpha) {
//Step 1. Calculate alpha.
double tempIncrease = Math.exp(paraAlpha);
//Step 2. Adjust.
double tempWeightsSum = 0; // For normalization.
for (int i = 0; i < weights.length; i++) {
if (paraCorrectArray[i]) {
weights[i] /= tempIncrease;
} else {
weights[i] *= tempIncrease;
}//of if
tempWeightsSum += weights[i];
}//of for i
// Step 3. Normalize.
for (int i = 0; i < weights.length; i++) {
weights[i] /= tempWeightsSum;
}//of for i
System.out.println("After adjusting, instances weights are: " + Arrays.toString(weights));
}//of adjustWeights
/**
* *********************************************
* Test the method.
* *********************************************
*/
public void adjustWeightsTest() {
boolean[] tempCorrectArray = new boolean[numInstances()];
for (int i = 0; i < tempCorrectArray.length / 2; i++) { //仅仅是测试adjustWeights()方法,因为还没有分类器,设置分类正确了一半
tempCorrectArray[i] = true;
}//of for i
double tempWeightedError = 0.3;
adjustWeights(tempCorrectArray, tempWeightedError); //仅仅是测试adjustWeights()方法,因为还没有分类器
System.out.println("After adjusting");
System.out.println(toString());
}//of adjustWeightsTest
/**
* ********************************************************
* For display.
* ********************************************************
*/
public String toString() {
String resultString = "I am a weighted Instances object.\r\n" + "I have " + numInstances() + " instances and "
+ (numAttributes() - 1) + " conditional attributes.\r\n" + "My weights are: " + Arrays.toString(weights)
+ "\r\n" + "My data are: \r\n" + super.toString();
return resultString;
}//of toString
/**
* ***********************************************************
* The entrance of the program.
*
* @param args
* ***********************************************************
*/
public static void main(String args[]) {
WeightedInstances tempWeightedInstances = null;
String tempFilename = "E:/Datasets/UCIdatasets/其他数据集/iris.arff";
try {
FileReader tempFileReader = new FileReader(tempFilename);
tempWeightedInstances = new WeightedInstances(tempFileReader);
tempFileReader.close();
} catch (Exception exception1) {
// TODO: handle exception
System.out.println("Cannot read the file: " + tempFilename + "\r\n" + exception1);
System.exit(0);
}//of try
System.out.println(tempWeightedInstances.toString());
tempWeightedInstances.adjustWeightsTest();
}//of main
}//of WeightedInstances