Spark 实现mllib分类 朴素贝叶斯,SVM,决策树以及随机森林。

一.简述

  Spark是当下非常流行的数据分析框架,而其中的机器学习包Mllib也是其诸多亮点之一,相信很多人也像我那样想要快些上手spark。下面我将列出实现mllib分类的简明代码,代码中将简述训练集和样本集的结构,以及各分类算法的参数含义。分类模型包括朴素贝叶斯,SVM,决策树以及随机森林。

 

二.实现代码

 
import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import java.util.LinkedList;
import java.util.List;
 
import org.apache.spark.mllib.linalg.Vector;
import org.apache.spark.mllib.linalg.Vectors;
import org.apache.spark.mllib.regression.LabeledPoint;
 
import org.apache.spark.mllib.classification.NaiveBayes;
import org.apache.spark.mllib.classification.NaiveBayesModel;
 
import org.apache.spark.mllib.classification.SVMModel;
import org.apache.spark.mllib.classification.SVMWithSGD;
 
import java.util.HashMap;
import java.util.Map;
import org.apache.spark.mllib.tree.DecisionTree;
import org.apache.spark.mllib.tree.model.DecisionTreeModel;
 
import org.apache.spark.mllib.tree.RandomForest;
import org.apache.spark.mllib.tree.model.RandomForestModel;
 
public class test {
     public static void main(String[] arg){
        //生成spark对象
         SparkConf conf = new SparkConf();
         conf.set( "spark.testing.memory" , "2147480000" );  // spark的运行配置,意指占用内存2G
         JavaSparkContext sc = new JavaSparkContext( "local[*]" , "Spark" , conf);      //第一个参数为本地模式,[*]尽可能地获取多的cpu;第二个是spark应用程序名,可以任意取;第三个为配置文件
         
         //训练集生成
         LabeledPoint pos = new LabeledPoint( 1.0 , Vectors.dense( 2.0 , 3.0 , 3.0 )); //规定数据结构为LabeledPoint,1.0为类别标号,Vectors.dense(2.0, 3.0, 3.0)为特征向量
         LabeledPoint neg = new LabeledPoint( 0.0 , Vectors.sparse( 3 , new int [] { 2 , 1 , 1 }, new double [] { 1.0 , 1.0 , 1.0 })); //特征值稀疏时,利用sparse构建
        List l = new LinkedList(); //利用List存放训练样本
         l.add(neg);
         l.add(pos);
         JavaRDD<LabeledPoint>training = sc.parallelize(l); //RDD化,泛化类型为LabeledPoint 而不是List
         final NaiveBayesModel nb_model = NaiveBayes.train(training.rdd());       
         
         //测试集生成
         double []  d = { 1 , 1 , 2 };
         Vector v =  Vectors.dense(d); //测试对象为单个vector,或者是RDD化后的vector
 
         //朴素贝叶斯
       System.out.println(nb_model.predict(v)); // 分类结果
       System.out.println(nb_model.predictProbabilities(v)); // 计算概率值
 
       
       //支持向量机
       int numIterations = 100 ; //迭代次数
       final SVMModel svm_model = SVMWithSGD.train(training.rdd(), numIterations); //构建模型
       System.out.println(svm_model.predict(v));
 
       //决策树
       Integer numClasses = 2 ; //类别数量
       Map<Integer, Integer> categoricalFeaturesInfo = new HashMap();
       String impurity = "gini" ; //对于分类问题,我们可以用熵entropy或Gini来表示信息的无序程度 ,对于回归问题,我们用方差(Variance)来表示无序程度,方差越大,说明数据间差异越大
       Integer maxDepth = 5 ; //最大树深
       Integer maxBins = 32 ; //最大划分数
       final DecisionTreeModel tree_model = DecisionTree.trainClassifier(training, numClasses,categoricalFeaturesInfo, impurity, maxDepth, maxBins); //构建模型
       System.out.println( "决策树分类结果:" );  
       System.out.println(tree_model.predict(v));
       
       //随机森林
       Integer numTrees = 3 ; // Use more in practice.
       String featureSubsetStrategy = "auto" ; // Let the algorithm choose.
       Integer seed = 12345 ;
       // Train a RandomForest model.
       final RandomForestModel forest_model = RandomForest.trainRegressor(training,
         categoricalFeaturesInfo, numTrees, featureSubsetStrategy, impurity, maxDepth, maxBins, seed); //参数与决策数基本一致,除了seed
       System.out.println( "随机森林结果:" );  
       System.out.println(forest_model.predict(v));
     }
   }

三.注意

1.利用spark进行数据分析时,数据一般要转化为RDD(利用spark所提供接口读取外部文件,一般会自动转化为RDD,通过MapReduce处理同样可以产生与接口匹配的训练集)

2.训练样本统一为标签向量(LabelPoint)。样本集为List,但是转化为RDD时,数据类型却为JavaRDD<LabeledPoint>(模型训练时,接口只接收数据类型为JavaRDD<LabeledPoint>)

3.分类predict返回结果为类别标签,贝叶斯模型可返回属于不同类的概率(python没用该接口)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

麦香鸡翅

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值