实体类

/**

* 样例

* @author Administrator

*

*/

public class Instance {

public double[] dim;//各个维度值

public int label;//类别标号

public Instance(double[] dim, int label) {

this.dim = dim;

this.label = label;

}

}

父类分类器

public abstract class Classifier {

public double errorRate;

public int errorNumber;

public abstract int classify(Instance instance) ;

}

自己实现的简单分类器,功能很弱,只是演示,可换用其他分类器

/**

* 简单分类器

* @author wangyongkang

*

*/

public class SimpleClassifier extends Classifier{

double threshold ;//分类的阈值

int dimNum;//对哪个维度分类

int fuhao = 1;//对阈值两边的处理

public int classify(Instance instance) {

if(instance.dim[dimNum] >= threshold) {

return fuhao;

}else {

return -fuhao;

}

}

/**

* 训练出threshold和fuhao

* @param instances

* @param W 样例的权重

* @param dim 对样例的哪个维度进行训练

*/

public void train(Instance[] instances, double[] W, int dimNum) {

errorRate = Double.MAX_VALUE;

this.dimNum = dimNum;

double adaThreshold = 0;

int adaFuhao = 0;

for(Instance instance : instances) {

threshold = instance.dim[dimNum];

for(int fuhaoIt = 0; fuhaoIt < 2; fuhaoIt ++) {

fuhao = -fuhao;

double error = 0;

int errorNum = 0;

for(int i = 0; i< instances.length; i++) {

if(classify(instances[i]) != instances[i].label) {

error += W[i];

errorNum++;

}

}

if(errorRate > error){

errorRate = error;

errorNumber = errorNum;

adaThreshold = threshold;

adaFuhao = fuhao;

}

}

}

threshold = adaThreshold;

fuhao = adaFuhao;

}

}

adaboost类

/**

* 实现adaboost功能

* @author Administrator

*

*/

public class Adaboost {

Instance[] instances;

List classifierList = null;//各个弱分类器

List alphaList = null;//每个弱分类器的权重

public Adaboost(Instance[] instances) {

this.instances = instances;

}

public void adaboost(int T) {

int len = this.instances.length;

double[] W = new double[len];//权重

for(int i = 0; i < len; i ++) {

W[i] = 1.0 / len;

}

classifierList = new ArrayList();

alphaList = new ArrayList();

for(int t = 0; t < T; t++) {

Classifier cf = getMinErrorRateClassifier(W);

classifierList.add(cf);

double errorRate = cf.errorRate;

//计算弱分类器的权重

double alpha = 0.5 * Math.log((1 - errorRate) / errorRate);

alphaList.add(alpha);

//更新样例的权重

double z = 0;

for(int i = 0; i < W.length; i++) {

W[i] = W[i] * Math.exp(-alpha * instances[i].label * cf.classify(instances[i]));

z += W[i];

}

for(int i = 0; i < W.length; i++) {

W[i] /= z;

}

System.out.println(getErrorCount());

}

}

private int getErrorCount() {

int count = 0;

for(Instance instance : instances) {

if(predict(instance) != instance.label)

count ++;

}

return count;

}

/**

* 预测

* @param instance

* @return

*/

public int predict(Instance instance) {

double p = 0;

for(int i = 0; i < classifierList.size(); i++) {

p += classifierList.get(i).classify(instance) * alphaList.get(i);

}

if(p > 0) return 1;

return -1;

}

/**

* 得到错误率最低的分类器

* @param W

* @return

*/

private Classifier getMinErrorRateClassifier(double[] W) {

double errorRate = Double.MAX_VALUE;

SimpleClassifier minErrorRateClassifier = null;

int dimLength = instances[0].dim.length;

for(int i = 0; i < dimLength; i++) {

SimpleClassifier sc = new SimpleClassifier();

sc.train(instances, W, i);

if(errorRate > sc.errorRate){

errorRate = sc.errorRate;

minErrorRateClassifier = sc;

}

}

return minErrorRateClassifier;

}

}

测试类

public class AdaboostTest {

public static void main(String[] args) {

double[] ins1 = {0,3};

double[] ins2 = {1,3};

double[] ins3 = {2,3};

double[] ins4 = {3,1};

double[] ins5 = {4,1};

double[] ins6 = {5,1};

double[] ins7 = {6,3};

double[] ins8 = {7,3};

double[] ins9 = {8,0};

double[] ins10 = {9,1};

Instance instance1 = new Instance(ins1, 1);

Instance instance2 = new Instance(ins2, 1);

Instance instance3 = new Instance(ins3, 1);

Instance instance4 = new Instance(ins4, -1);

Instance instance5 = new Instance(ins5, -1);

Instance instance6 = new Instance(ins6, -1);

Instance instance7 = new Instance(ins7, 1);

Instance instance8 = new Instance(ins8, 1);

Instance instance9 = new Instance(ins9, 1);

Instance instance10 = new Instance(ins10, -1);

Instance[] instances = {instance1, instance2, instance3, instance4, instance5, instance6, instance7, instance8, instance9, instance10 };

Adaboost ab = new Adaboost(instances);

ab.adaboost(10);

}

}

adaboost java_Adaboost的java实现 | 学步园相关推荐

  1. android jni 回调 java_android linux线程通过JNI回调java函数 | 学步园

    Linux线程通过JNI回调JAVA函数 最近做的一个小工程需要用到回调函数,由linux层回调到java层,调试的时候会遇到一些问题,免得忘记,在这里记录一下: JNI的各种数据类型和数据结构我就不 ...

  2. java memcachedclient_memcached client — memcached client for java使用 | 学步园

    memcached client for java是另一个memcached的java客户端 代码: (1)MemcachedServer -- memcached的服务器 public class ...

  3. java值传递试题_面试题:java参数传递 | 学步园

    public class TestString { public static void link(String a){ a+="World"; } public static v ...

  4. jmeter java接口,jmeter并发测试java接口 | 学步园

    Sample 这里我用到主要JMeter的线程和报表,扩展了他的"Java请求"这个应用类别.要扩展此应用,要用到lib/ext/ApacheJMeter_java.jar,他封装 ...

  5. a 寻路算法 java_A*(也叫A star, A星)寻路算法Java版 | 学步园

    寻路 首先要理解什么是A*寻路算法,可以参考这三篇文章: 下面为测试地图,0表示可以通行,1表示障碍物: 要从点(5, 1)到点(5, 5),通过A*寻路算法找到以路径为@所示: 在代码中可以修改障碍 ...

  6. java 创建存储过程_如何在pl/sql中创建及调用JAVA存储过程 | 学步园

    1.创建一个java存储过程helloworld create or replace and compile java source named helloworld as public class ...

  7. mysql存储java对象_Mysql存储java对象 | 学步园

    mysql  设置字段为 blob 保存对象,先将对象序列化为byte[]  使用 setObject(byte[] bytes) ByteArrayOutputStream baos = new B ...

  8. java 短地址_URL短地址压缩算法 微博短地址原理解析(再转与Java实现) | 学步园

    主要是一次发新浪微博,发网址时看到网址被压缩了,才对这个有兴趣,于是查了查,查到这些 和其Java实现的博文 对第一篇博文,留了疑问,主要是觉得于四个得到的压缩地址中取一个,那之前循环计算不是有些浪费 ...

  9. java 求最大公因数_三种算法求最大公约数——Java实现 | 学步园

    求两个自然数m和n的最大公约数. 连续整除检测: 1. t=min{m,n}: 2. m除以t,如果余数为0,则执行步骤3,否则,执行步骤4: 3. n除以t,如果余数为0,返回t的值作为结果,否则, ...

最新文章

  1. inline函数使用容易导致出错的问题
  2. Blender文档翻译:Operators tutorial(操作教程)
  3. 信安精品课:第1章网络信息安全概述精讲笔记
  4. 系统软键盘Android在外接物理键盘时,如何强制调用系统软键盘?
  5. 最小生成树-Prim算法的Python实现
  6. Element表格固定第一列和第一行,并通过属性名动态渲染数据
  7. 单晶硅各向异性刻蚀技术
  8. linux 脚本里切换用户密码,shell,切换用户,执行指定,脚本
  9. 【图像重建】基于Split Bregman实现稀疏图像重建附matlab代码
  10. 一文带你认识微内核,华为“鸿蒙”操作系统微内核到底是什么?
  11. 华为S5700-SI 系统打补丁
  12. java基本数据类型Char
  13. checkv的基本使用
  14. extern的几种用法
  15. ARMv7 Processor modes
  16. Python列表、元组、字典相关练习题记录——第三天
  17. train_test_split(),随机划分训练集和测试集的函数
  18. 关于缓存一致性的一些介绍
  19. hadoop 权威指南 HBase
  20. 3D视觉基础(基本原理及3D传感器基本参数)

热门文章

  1. freopen()函数
  2. 9.5---所有字符串的排列组合(CC150)
  3. 学习PHP-感谢帅哥分享O(∩_∩)O~
  4. linux安装步骤_图解 Debian 10(Buster)安装步骤 | Linux 中国
  5. xterm远程连服务器连不上_VS Code Remote 发布!开启远程开发新时代
  6. 晶体管游戏 linux,Industrial Linux
  7. 简易天气java论文_【Java小项目】简单的天气预报
  8. Python+OpenCV:阈值分割
  9. 用python语言实现反恐精英基础版-案例
  10. Apache-配置、测试和调试