基于Spark實(shí)現(xiàn)隨機(jī)森林代碼
本文實(shí)例為大家分享了基于Spark實(shí)現(xiàn)隨機(jī)森林的具體代碼,供大家參考,具體內(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分類(lèi)個(gè)數(shù)為2 * 2 numTrees 表示的是隨機(jī)森林中樹(shù)的個(gè)數(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()); } }
得到的隨機(jī)森林的展示結(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
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
javaweb用戶(hù)注銷(xiāo)后點(diǎn)擊瀏覽器返回刷新頁(yè)面重復(fù)登錄問(wèn)題的解決方法
這篇文章主要為大家詳細(xì)介紹了javaweb用戶(hù)注銷(xiāo)后點(diǎn)擊瀏覽器返回刷新頁(yè)面重復(fù)登錄問(wèn)題的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09java中catalina.home與catalina.base區(qū)別點(diǎn)整理
在本篇文章里小編給大家整理的是關(guān)于java項(xiàng)目中catalina.home與catalina.base區(qū)別點(diǎn),需要的朋友們可以學(xué)習(xí)下。2020-02-02SpringBoot Jackson日期格式化統(tǒng)一配置的實(shí)現(xiàn)
Spring項(xiàng)目中經(jīng)常需要配置日期時(shí)間格式格式,本文主要介紹了SpringBoot Jackson日期格式化統(tǒng)一配置的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-08-08SpringBoot優(yōu)先加載指定Bean的實(shí)現(xiàn)
SpringBoot框架在啟動(dòng)時(shí)可以自動(dòng)將托管的Bean實(shí)例化,一般情況下它的依賴(lài)注入特性可以正確處理Bean之間的依賴(lài)關(guān)系,無(wú)需手動(dòng)指定某個(gè) Bean優(yōu)先創(chuàng)建實(shí)例,文中有詳細(xì)的代碼示例,需要的朋友可以參考下2023-05-05alibaba?seata服務(wù)端具體實(shí)現(xiàn)
seata是來(lái)處理分布式服務(wù)之間互相調(diào)用的事務(wù)問(wèn)題,本文重點(diǎn)給大家介紹alibaba-seata實(shí)現(xiàn)方法,文中通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-02-02Java 8 Stream操作類(lèi)型及peek示例解析
這篇文章主要介紹了Java 8 Stream操作類(lèi)型及peek示例解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04詳解Maven POM(項(xiàng)目對(duì)象模型)
這篇文章主要介紹了Maven POM(項(xiàng)目對(duì)象模型)的相關(guān)資料,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-07-07