日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

机器学习知识点(十六)集成学习AdaBoost算法Java实现

發布時間:2025/4/16 java 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 机器学习知识点(十六)集成学习AdaBoost算法Java实现 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

理解http://blog.csdn.net/fjssharpsword/article/details/61913092中AdaBoost算法,從網上找了一套簡單的代碼加以理解。

1、基分類器,實現一個簡單的分類

1)對象類

package sk.adaboost;public class Instance {public double[] dim; //各個維度值public int label; //類別標號public Instance(double[] dim, int label) {this.dim = dim;this.label = label;} }
2)接口類

package sk.adaboost;public abstract class Classifier {public double errorRate;public int errorNumber;public abstract int classify(Instance instance) ;}


3)基學習器算法

package sk.adaboost;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;} }
4)adaboost集成學習算法

package sk.adaboost;import java.util.ArrayList; import java.util.List;public class Adaboost {Instance[] instances;List<Classifier> classifierList = null; //各個弱分類器List<Double> alphaList = null; //每個弱分類器的權重public Adaboost(Instance[] instances) {this.instances = instances;}public List<String> adaboost(int T) {//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<Classifier>();alphaList = new ArrayList<Double>();List<String> iHP=new ArrayList<String>();for(int t = 0; t < 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;}iHP.add(String.valueOf(getErrorCount()));//預測結果插入}return iHP;}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 List<String> adaboost(int T)算法中,先初始化權重然后開始T輪的基學習器算法執行和權重更新,private Classifier getMinErrorRateClassifier(double[] W)開展基學習器訓練并返回錯誤率,返回的錯誤計算權重并更新分布。

5)測試adaboost算法,采用投票法的結合策略輸出集成學習預測結果

package sk.adaboost;import java.util.List;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);List<String> iHP=ab.adaboost(10);//輸出預測結果,根據多數頭投票法的結合策略int pcount=0,ncount=0;for(String hp:iHP){if (hp.equals("1")) pcount++;//預測為正例的數字if (hp.equals("0")) ncount++;//預測為正例的數字}if (pcount>=ncount) System.out.println("1");else System.out.println("0");} }
6)助于理解算法本身,實際應用中基學習器可以換成其他算法。

總結

以上是生活随笔為你收集整理的机器学习知识点(十六)集成学习AdaBoost算法Java实现的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。