java實現(xiàn)隨機森林RandomForest的示例代碼
隨機森林是由多棵樹組成的分類或回歸方法。主要思想來源于Bagging算法,Bagging技術(shù)思想主要是給定一弱分類器及訓練集,讓該學習算法訓練多輪,每輪的訓練集由原始訓練集中有放回的隨機抽取,大小一般跟原始訓練集相當,這樣依次訓練多個弱分類器,最終的分類由這些弱分類器組合,對于分類問題一般采用多數(shù)投票法,對于回歸問題一般采用簡單平均法。隨機森林在bagging的基礎(chǔ)上,每個弱分類器都是決策樹,決策樹的生成過程中中,在屬性的選擇上增加了依一定概率選擇屬性,在這些屬性中選擇最佳屬性及分割點,傳統(tǒng)做法一般是全部屬性中去選擇最佳屬性,這樣隨機森林有了樣本選擇的隨機性,屬性選擇的隨機性,這樣一來增加了每個分類器的差異性、不穩(wěn)定性及一定程度上避免每個分類器的過擬合(一般決策樹有過擬合現(xiàn)象),由此組合分類器增加了最終的泛化能力。下面是代碼的簡單實現(xiàn)
/** * 隨機森林 回歸問題 * @author ysh 1208706282 * */ public class RandomForest { List<Sample> mSamples; List<Cart> mCarts; double mFeatureRate; int mMaxDepth; int mMinLeaf; Random mRandom; /** * 加載數(shù)據(jù) 回歸樹 * @param path * @param regex * @throws Exception */ public void loadData(String path,String regex) throws Exception{ mSamples = new ArrayList<Sample>(); BufferedReader reader = new BufferedReader(new FileReader(path)); String line = null; String splits[] = null; Sample sample = null; while(null != (line=reader.readLine())){ splits = line.split(regex); sample = new Sample(); sample.label = Double.valueOf(splits[0]); sample.feature = new ArrayList<Double>(splits.length-1); for(int i=0;i<splits.length-1;i++){ sample.feature.add(new Double(splits[i+1])); } mSamples.add(sample); } reader.close(); } public void train(int iters){ mCarts = new ArrayList<Cart>(iters); Cart cart = null; for(int iter=0;iter<iters;iter++){ cart = new Cart(); cart.mFeatureRate = mFeatureRate; cart.mMaxDepth = mMaxDepth; cart.mMinLeaf = mMinLeaf; cart.mRandom = mRandom; List<Sample> s = new ArrayList<Sample>(mSamples.size()); for(int i=0;i<mSamples.size();i++){ s.add(mSamples.get(cart.mRandom.nextInt(mSamples.size()))); } cart.setData(s); cart.train(); mCarts.add(cart); System.out.println("iter: "+iter); s = null; } } /** * 回歸問題簡單平均法 分類問題多數(shù)投票法 * @param sample * @return */ public double classify(Sample sample){ double val = 0; for(Cart cart:mCarts){ val += cart.classify(sample); } return val/mCarts.size(); } /** * @param args * @throws Exception */ public static void main(String[] args) throws Exception { // TODO Auto-generated method stub RandomForest forest = new RandomForest(); forest.loadData("F:/2016-contest/20161001/train_data_1.csv", ","); forest.mFeatureRate = 0.8; forest.mMaxDepth = 3; forest.mMinLeaf = 1; forest.mRandom = new Random(); forest.mRandom.setSeed(100); forest.train(100); List<Sample> samples = Cart.loadTestData("F:/2016-contest/20161001/valid_data_1.csv", true, ","); double sum = 0; for(Sample s:samples){ double val = forest.classify(s); sum += (val-s.label)*(val-s.label); System.out.println(val+" "+s.label); } System.out.println(sum/samples.size()+" "+sum); System.out.println(System.currentTimeMillis()); } }
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Intellij Idea中批量導入第三方j(luò)ar包的全過程
引入jar包一般都是針對小的java項目,這篇文章主要給大家介紹了關(guān)于Intellij Idea中批量導入第三方j(luò)ar包的相關(guān)資料,文中通過圖文介紹的非常詳細,需要的朋友可以參考下2021-10-10Java日期工具類操作字符串Date和LocalDate互轉(zhuǎn)
這篇文章主要介紹了Java日期工具類操作字符串Date和LocalDate互轉(zhuǎn),文章首先通過需要先引入坐標展開主題的相關(guān)內(nèi)容介紹,需要的朋友可以參一下2022-06-06