基于Spark實現(xiàn)隨機森林代碼
更新時間:2019年08月10日 11:50:09 作者:KevinYunhe
這篇文章主要為大家詳細介紹了基于Spark實現(xiàn)隨機森林代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了基于Spark實現(xiàn)隨機森林的具體代碼,供大家參考,具體內(nèi)容如下
public class RandomForestClassficationTest extends TestCase implements Serializable
{
/**
*
*/
private static final long serialVersionUID = 7802523720751354318L;
class PredictResult implements Serializable{
/**
*
*/
private static final long serialVersionUID = -168308887976477219L;
double label;
double prediction;
public PredictResult(double label,double prediction){
this.label = label;
this.prediction = prediction;
}
@Override
public String toString(){
return this.label + " : " + this.prediction ;
}
}
public void test_randomForest() throws JAXBException{
SparkConf sparkConf = new SparkConf();
sparkConf.setAppName("RandomForest");
sparkConf.setMaster("local");
SparkContext sc = new SparkContext(sparkConf);
String dataPath = RandomForestClassficationTest.class.getResource("/").getPath() + "/sample_libsvm_data.txt";
RDD dataSet = MLUtils.loadLibSVMFile(sc, dataPath);
RDD[] rddList = dataSet.randomSplit(new double[]{0.7,0.3},1);
RDD trainingData = rddList[0];
RDD testData = rddList[1];
ClassTag labelPointClassTag = trainingData.elementClassTag();
JavaRDD trainingJavaData = new JavaRDD(trainingData,labelPointClassTag);
int numClasses = 2;
Map categoricalFeatureInfos = new HashMap();
int numTrees = 3;
String featureSubsetStrategy = "auto";
String impurity = "gini";
int maxDepth = 4;
int maxBins = 32;
/**
* 1 numClasses分類個數(shù)為2
* 2 numTrees 表示的是隨機森林中樹的個數(shù)
* 3 featureSubsetStrategy
* 4
*/
final RandomForestModel model = RandomForest.trainClassifier(trainingJavaData,
numClasses,
categoricalFeatureInfos,
numTrees,
featureSubsetStrategy,
impurity,
maxDepth,
maxBins,
1);
JavaRDD testJavaData = new JavaRDD(testData,testData.elementClassTag());
JavaRDD predictRddResult = testJavaData.map(new Function(){
/**
*
*/
private static final long serialVersionUID = 1L;
public PredictResult call(LabeledPoint point) throws Exception {
// TODO Auto-generated method stub
double pointLabel = point.label();
double prediction = model.predict(point.features());
PredictResult result = new PredictResult(pointLabel,prediction);
return result;
}
});
List predictResultList = predictRddResult.collect();
for(PredictResult result:predictResultList){
System.out.println(result.toString());
}
System.out.println(model.toDebugString());
}
}
得到的隨機森林的展示結(jié)果如下:
TreeEnsembleModel classifier with 3 trees Tree 0: If (feature 435 <= 0.0) If (feature 516 <= 0.0) Predict: 0.0 Else (feature 516 > 0.0) Predict: 1.0 Else (feature 435 > 0.0) Predict: 1.0 Tree 1: If (feature 512 <= 0.0) Predict: 1.0 Else (feature 512 > 0.0) Predict: 0.0 Tree 2: If (feature 377 <= 1.0) Predict: 0.0 Else (feature 377 > 1.0) If (feature 455 <= 0.0) Predict: 1.0 Else (feature 455 > 0.0) Predict: 0.0
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
相關(guān)文章
30分鐘入門Java8之默認方法和靜態(tài)接口方法學習
這篇文章主要介紹了30分鐘入門Java8之默認方法和靜態(tài)接口方法學習,詳細介紹了默認方法和接口,有興趣的可以了解一下。2017-04-04
SpringBoot 配置文件中配置的中文,程序讀取出來是亂碼的解決
這篇文章主要介紹了SpringBoot 配置文件中配置的中文,程序讀取出來是亂碼的解決,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09
springsecurity輕松實現(xiàn)角色權(quán)限的示例代碼
這篇文章主要介紹了springsecurity輕松實現(xiàn)角色權(quán)限的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-03-03

