当前位置: 首页 > news >正文

日撸代码300行:第63天(集成学习之 AdaBoosting-1)

 代码来自闵老师”日撸 Java 三百行(61-70天)

日撸 Java 三百行(61-70天,决策树与集成学习)_闵帆的博客-CSDN博客

学习过程中理解算法参考了:(十三)通俗易懂理解——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 stubsuper(paraFileReader);setClassIndex(numAttributes() - 1);// Initialize weightsweights = new double[numInstances()];double tempAverage = 1.0/numInstances();for (int i = 0; i < weights.length; i++) {weights[i] = tempAverage;}//of for iSystem.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 stubsuper(paraInstances);setClassIndex(numAttributes() - 1);//Initialize weightsweights = new double[numInstances()];double tempAverage = 1.0/numInstances();for (int i = 0; i < weights.length; i++) {weights[i] = tempAverage;}//of for iSystem.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 iftempWeightsSum += weights[i];}//of for i// Step 3. Normalize.for (int i = 0; i < weights.length; i++) {weights[i] /= tempWeightsSum;}//of for iSystem.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 idouble 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 exceptionSystem.out.println("Cannot read the file: " + tempFilename + "\r\n" + exception1);System.exit(0);}//of trySystem.out.println(tempWeightedInstances.toString());tempWeightedInstances.adjustWeightsTest();}//of main}//of WeightedInstances

http://www.lryc.cn/news/116268.html

相关文章:

  • 抽象父类获取子类的泛型 或接口泛型
  • 题目:2341.数组能形成多少数对
  • NB-IOT 和蜂窝通信(2/3/4/5G)的区别和特点是什么?
  • vue3 动态导入src/page目录下的所有子文件,并自动注册所有页面组件
  • python优雅地爬虫
  • mysql8查看执行sql历史日志、慢sql历史日志,配置开启sql历史日志general_log、慢sql历史日志slow_query_log
  • vscode关闭绑定元素“xxx”隐式具有“any”类型这类错误
  • View绘制流程-Window创建
  • Jenkins build包时虽然单元测试失败了,但是仍然可以成功build包(最终结束时build success)
  • 【vue3】基础知识点-setup语法糖
  • idol!! 2023牛客暑期多校训练营6 C
  • 深入理解Jdk5引入的Java泛型:类型安全与灵活性并存
  • idea在控制台中输出文字显示乱码
  • hacksudo3 通关详解
  • CentOS 虚拟机磁盘扩容(非常实用)
  • docker案例复现
  • 淘宝资源采集(从零开始学习淘宝数据爬取)
  • 【C语言】预处理详解
  • 2023中国(合肥)场景创新峰会成功举办,全息网御被纳入《合肥市第二批场景能力清单》
  • QT QLCDNumber 使用详解
  • 明年,HarmonyOS不再兼容Android应用!
  • 华为OD机试 - 人气最高的店铺(Java JS Python)
  • mysql sql 语句sum求和嵌套数学表达式
  • Java课题笔记~ Servlet编程
  • 修改IDEA的idea.vmoptions参数导致IDEA无法打开(ReservedCodeCacheSize)
  • P1321 单词覆盖还原
  • GODOT游戏引擎简介,包含与unity性能对比测试,以及选型建议
  • Add-in Express for Microsoft Office and Delphi Crack
  • opencv基础-34 图像平滑处理-2D 卷积 cv2.filter2D()
  • webSocket 与传统的 http 有什么优势