隨機森林是由多棵樹組成的分類或回歸方法。主要思想來源于Bagging算法,Bagging技術思想主要是給定一弱分類器及訓練集,讓該學習算法訓練多輪,每輪的訓練集由原始訓練集中有放回的隨機抽取,大小一般跟原始訓練集相當,這樣依次訓練多個弱分類器,最終的分類由這些弱分類器組合,對于分類問題一般采用多數投票法,對于回歸問題一般采用簡單平均法。隨機森林在bagging的基礎上,每個弱分類器都是決策樹,決策樹的生成過程中中,在屬性的選擇上增加了依一定概率選擇屬性,在這些屬性中選擇最佳屬性及分割點,傳統做法一般是全部屬性中去選擇最佳屬性,這樣隨機森林有了樣本選擇的隨機性,屬性選擇的隨機性,這樣一來增加了每個分類器的差異性、不穩定性及一定程度上避免每個分類器的過擬合(一般決策樹有過擬合現象),由此組合分類器增加了最終的泛化能力。下面是代碼的簡單實現
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
/** * 隨機森林 回歸問題 * @author ysh 1208706282 * */ public class RandomForest { List<Sample> mSamples; List<Cart> mCarts; double mFeatureRate; int mMaxDepth; int mMinLeaf; Random mRandom; /** * 加載數據 回歸樹 * @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 ; } } /** * 回歸問題簡單平均法 分類問題多數投票法 * @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()); } } |
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://blog.csdn.net/ysh126/article/details/53125858