java实现随机森林RandomForest的⽰例代码
随机森林是由多棵树组成的分类或回归⽅法。主要思想来源于Bagging算法,Bagging技术思想主要是给定⼀弱分类器及训练集,让该学习算法训练多轮,每轮的训练集由原始训练集中有放回的随机抽取,⼤⼩⼀般跟原始训练集相当,这样依次训练多个弱分类器,最终的分类由这些弱分类器组合,对于分类问题⼀般采⽤多数投票法,对于回归问题⼀般采⽤简单平均法。随机森林在bagging的基础上,每个弱分类器都是决策树,决策树的⽣成过程中中,在属性的选择上增加了依⼀定概率选择属性,在这些属性中选择最佳属性及分割点,传统做法⼀般是全部属性中去选择最佳属性,这样随机森林有了样本选择的随机性,属性选择的随机性,这样⼀来增加了每个分类器的差异性、不稳定性及⼀定程度上避免每个分类器的过拟合(⼀般决策树有过拟合现象),由此组合分类器增加了最终的泛化能⼒。下⾯是代码的简单实现
/**
* 随机森林回归问题
* @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>();java valueof
BufferedReader reader = new BufferedReader(new FileReader(path));
String line = null;
String splits[] = null;
Sample sample = null;
while(null != (adLine())){
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.(Int(mSamples.size())));
}
cart.setData(s);
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);
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());
}
}
以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论