package Spark_MLlib

import org.apache.spark.ml.Pipeline
import org.apache.spark.ml.classification.{DecisionTreeClassificationModel, DecisionTreeClassifier}
import org.apache.spark.ml.evaluation.MulticlassClassificationEvaluator
import org.apache.spark.ml.feature.{IndexToString, StringIndexer, VectorIndexer}
import org.apache.spark.ml.linalg.{Vector, Vectors}
import org.apache.spark.mllib.tree.DecisionTree
import org.apache.spark.sql.SparkSession /**
* Created by soyo on 17-11-5.
*/
case class data_schemas(features:Vector,label:String)
object 决策树 {
val spark=SparkSession.builder().master("local").appName("决策树").getOrCreate()
import spark.implicits._
def main(args: Array[String]): Unit = { val source_DF=spark.sparkContext.textFile("file:///home/soyo/桌面/spark编程测试数据/soyo2.txt")
.map(_.split(",")).map(x=>data_schemas(Vectors.dense(x().toDouble,x().toDouble,x().toDouble,x().toDouble),x())).toDF()
source_DF.createOrReplaceTempView("decisonTree")
val DF=spark.sql("select * from decisonTree")
DF.show()
//分别获取标签列和特征列,进行索引和重命名(索引的目的是将字符串label数值化方便机器学习算法学习)
val lableIndexer=new StringIndexer().setInputCol("label").setOutputCol("indexedLabel").fit(DF)
val featureIndexer= new VectorIndexer().setInputCol("features").setOutputCol("indexedFeatures").setMaxCategories().fit(DF)
val labelConverter= new IndexToString().setInputCol("prediction").setOutputCol("predictedLabel").setLabels(lableIndexer.labels)
// 训练数据和测试数据
val Array(trainData,testData)=DF.randomSplit(Array(0.7,0.3))
val decisionTreeClassifier=new DecisionTreeClassifier().setLabelCol("indexedLabel").setFeaturesCol("indexedFeatures")
//构建机器学习工作流
val dt_pipeline=new Pipeline().setStages(Array(lableIndexer,featureIndexer,decisionTreeClassifier,labelConverter))
val dt_model=dt_pipeline.fit(trainData)
//进行预测
val dtprediction=dt_model.transform(testData)
dtprediction.show()
//评估决策树模型
val evaluatorClassifier=new MulticlassClassificationEvaluator().setLabelCol("indexedLabel").setPredictionCol("prediction").setMetricName("accuracy")
val accuracy=evaluatorClassifier.evaluate(dtprediction)
println("准确率为: "+accuracy)
val error=-accuracy
println("错误率为: "+error)
val treeModelClassifier=dt_model.stages().asInstanceOf[DecisionTreeClassificationModel]
val schema_DecisionTree=treeModelClassifier.toDebugString
println("决策树的模型结构为: "+schema_DecisionTree) }
}

结果为:

+-----------------+------+
|         features| label|
+-----------------+------+
|[5.1,3.5,1.4,0.2]|hadoop|
|[4.9,3.0,1.4,0.2]|hadoop|
|[4.7,3.2,1.3,0.2]|hadoop|
|[4.6,3.1,1.5,0.2]|hadoop|
|[5.0,3.6,1.4,0.2]|hadoop|
|[5.4,3.9,1.7,0.4]|hadoop|
|[4.6,3.4,1.4,0.3]|hadoop|
|[5.0,3.4,1.5,0.2]|hadoop|
|[4.4,2.9,1.4,0.2]|hadoop|
|[4.9,3.1,1.5,0.1]|hadoop|
|[5.4,3.7,1.5,0.2]|hadoop|
|[4.8,3.4,1.6,0.2]|hadoop|
|[4.8,3.0,1.4,0.1]|hadoop|
|[4.3,3.0,1.1,0.1]|hadoop|
|[5.8,4.0,1.2,0.2]|hadoop|
|[5.7,4.4,1.5,0.4]|hadoop|
|[5.4,3.9,1.3,0.4]|hadoop|
|[5.1,3.5,1.4,0.3]|hadoop|
|[5.7,3.8,1.7,0.3]|hadoop|
|[5.1,3.8,1.5,0.3]|hadoop|
+-----------------+------+
only showing top 20 rows

+-----------------+------+------------+-----------------+--------------+-------------+----------+--------------+
|         features| label|indexedLabel|  indexedFeatures| rawPrediction|  probability|prediction|predictedLabel|
+-----------------+------+------------+-----------------+--------------+-------------+----------+--------------+
|[4.4,3.0,1.3,0.2]|hadoop|         1.0|[4.4,3.0,1.3,0.2]|[0.0,36.0,0.0]|[0.0,1.0,0.0]|       1.0|        hadoop|
|[4.6,3.4,1.4,0.3]|hadoop|         1.0|[4.6,3.4,1.4,0.3]|[0.0,36.0,0.0]|[0.0,1.0,0.0]|       1.0|        hadoop|
|[4.6,3.6,1.0,0.2]|hadoop|         1.0|[4.6,3.6,1.0,0.2]|[0.0,36.0,0.0]|[0.0,1.0,0.0]|       1.0|        hadoop|
|[4.9,2.4,3.3,1.0]| spark|         0.0|[4.9,2.4,3.3,1.0]| [0.0,0.0,1.0]|[0.0,0.0,1.0]|       2.0|         Scala|
|[5.0,2.0,3.5,1.0]| spark|         0.0|[5.0,2.0,3.5,1.0]| [1.0,0.0,0.0]|[1.0,0.0,0.0]|       0.0|         spark|
|[5.0,2.3,3.3,1.0]| spark|         0.0|[5.0,2.3,3.3,1.0]|[29.0,0.0,0.0]|[1.0,0.0,0.0]|       0.0|         spark|
|[5.0,3.2,1.2,0.2]|hadoop|         1.0|[5.0,3.2,1.2,0.2]|[0.0,36.0,0.0]|[0.0,1.0,0.0]|       1.0|        hadoop|
|[5.0,3.3,1.4,0.2]|hadoop|         1.0|[5.0,3.3,1.4,0.2]|[0.0,36.0,0.0]|[0.0,1.0,0.0]|       1.0|        hadoop|
|[5.0,3.4,1.6,0.4]|hadoop|         1.0|[5.0,3.4,1.6,0.4]|[0.0,36.0,0.0]|[0.0,1.0,0.0]|       1.0|        hadoop|
|[5.0,3.6,1.4,0.2]|hadoop|         1.0|[5.0,3.6,1.4,0.2]|[0.0,36.0,0.0]|[0.0,1.0,0.0]|       1.0|        hadoop|
|[5.1,3.5,1.4,0.2]|hadoop|         1.0|[5.1,3.5,1.4,0.2]|[0.0,36.0,0.0]|[0.0,1.0,0.0]|       1.0|        hadoop|
|[5.1,3.7,1.5,0.4]|hadoop|         1.0|[5.1,3.7,1.5,0.4]|[0.0,36.0,0.0]|[0.0,1.0,0.0]|       1.0|        hadoop|
|[5.2,3.4,1.4,0.2]|hadoop|         1.0|[5.2,3.4,1.4,0.2]|[0.0,36.0,0.0]|[0.0,1.0,0.0]|       1.0|        hadoop|
|[5.2,4.1,1.5,0.1]|hadoop|         1.0|[5.2,4.1,1.5,0.1]|[0.0,36.0,0.0]|[0.0,1.0,0.0]|       1.0|        hadoop|
|[5.4,3.0,4.5,1.5]| spark|         0.0|[5.4,3.0,4.5,1.5]|[29.0,0.0,0.0]|[1.0,0.0,0.0]|       0.0|         spark|
|[5.4,3.9,1.7,0.4]|hadoop|         1.0|[5.4,3.9,1.7,0.4]|[0.0,36.0,0.0]|[0.0,1.0,0.0]|       1.0|        hadoop|
|[5.5,2.4,3.7,1.0]| spark|         0.0|[5.5,2.4,3.7,1.0]|[29.0,0.0,0.0]|[1.0,0.0,0.0]|       0.0|         spark|
|[5.5,2.4,3.8,1.1]| spark|         0.0|[5.5,2.4,3.8,1.1]|[29.0,0.0,0.0]|[1.0,0.0,0.0]|       0.0|         spark|
|[5.5,2.5,4.0,1.3]| spark|         0.0|[5.5,2.5,4.0,1.3]|[29.0,0.0,0.0]|[1.0,0.0,0.0]|       0.0|         spark|
|[5.5,2.6,4.4,1.2]| spark|         0.0|[5.5,2.6,4.4,1.2]|[29.0,0.0,0.0]|[1.0,0.0,0.0]|       0.0|         spark|
|[5.5,4.2,1.4,0.2]|hadoop|         1.0|[5.5,4.2,1.4,0.2]|[0.0,36.0,0.0]|[0.0,1.0,0.0]|       1.0|        hadoop|
|[5.6,2.5,3.9,1.1]| spark|         0.0|[5.6,2.5,3.9,1.1]|[29.0,0.0,0.0]|[1.0,0.0,0.0]|       0.0|         spark|
|[5.6,2.7,4.2,1.3]| spark|         0.0|[5.6,2.7,4.2,1.3]|[29.0,0.0,0.0]|[1.0,0.0,0.0]|       0.0|         spark|
|[5.6,3.0,4.1,1.3]| spark|         0.0|[5.6,3.0,4.1,1.3]|[29.0,0.0,0.0]|[1.0,0.0,0.0]|       0.0|         spark|
|[5.7,2.6,3.5,1.0]| spark|         0.0|[5.7,2.6,3.5,1.0]|[29.0,0.0,0.0]|[1.0,0.0,0.0]|       0.0|         spark|
|[5.8,2.6,4.0,1.2]| spark|         0.0|[5.8,2.6,4.0,1.2]|[29.0,0.0,0.0]|[1.0,0.0,0.0]|       0.0|         spark|
|[5.8,4.0,1.2,0.2]|hadoop|         1.0|[5.8,4.0,1.2,0.2]|[0.0,36.0,0.0]|[0.0,1.0,0.0]|       1.0|        hadoop|
|[6.1,2.6,5.6,1.4]| Scala|         2.0|[6.1,2.6,5.6,1.4]|[29.0,0.0,0.0]|[1.0,0.0,0.0]|       0.0|         spark|
|[6.2,2.2,4.5,1.5]| spark|         0.0|[6.2,2.2,4.5,1.5]| [0.0,0.0,1.0]|[0.0,0.0,1.0]|       2.0|         Scala|
|[6.2,3.4,5.4,2.3]| Scala|         2.0|[6.2,3.4,5.4,2.3]|[0.0,0.0,31.0]|[0.0,0.0,1.0]|       2.0|         Scala|
|[6.3,2.5,5.0,1.9]| Scala|         2.0|[6.3,2.5,5.0,1.9]|[0.0,0.0,31.0]|[0.0,0.0,1.0]|       2.0|         Scala|
|[6.3,2.8,5.1,1.5]| Scala|         2.0|[6.3,2.8,5.1,1.5]|[29.0,0.0,0.0]|[1.0,0.0,0.0]|       0.0|         spark|
|[6.4,2.8,5.6,2.1]| Scala|         2.0|[6.4,2.8,5.6,2.1]|[0.0,0.0,31.0]|[0.0,0.0,1.0]|       2.0|         Scala|
|[6.4,2.8,5.6,2.2]| Scala|         2.0|[6.4,2.8,5.6,2.2]|[0.0,0.0,31.0]|[0.0,0.0,1.0]|       2.0|         Scala|
|[6.4,3.2,4.5,1.5]| spark|         0.0|[6.4,3.2,4.5,1.5]|[29.0,0.0,0.0]|[1.0,0.0,0.0]|       0.0|         spark|
|[6.4,3.2,5.3,2.3]| Scala|         2.0|[6.4,3.2,5.3,2.3]|[0.0,0.0,31.0]|[0.0,0.0,1.0]|       2.0|         Scala|
|[6.5,2.8,4.6,1.5]| spark|         0.0|[6.5,2.8,4.6,1.5]|[29.0,0.0,0.0]|[1.0,0.0,0.0]|       0.0|         spark|
|[6.6,2.9,4.6,1.3]| spark|         0.0|[6.6,2.9,4.6,1.3]|[29.0,0.0,0.0]|[1.0,0.0,0.0]|       0.0|         spark|
|[6.6,3.0,4.4,1.4]| spark|         0.0|[6.6,3.0,4.4,1.4]|[29.0,0.0,0.0]|[1.0,0.0,0.0]|       0.0|         spark|
|[6.8,3.2,5.9,2.3]| Scala|         2.0|[6.8,3.2,5.9,2.3]|[0.0,0.0,31.0]|[0.0,0.0,1.0]|       2.0|         Scala|
|[6.9,3.1,4.9,1.5]| spark|         0.0|[6.9,3.1,4.9,1.5]|[29.0,0.0,0.0]|[1.0,0.0,0.0]|       0.0|         spark|
|[6.9,3.2,5.7,2.3]| Scala|         2.0|[6.9,3.2,5.7,2.3]|[0.0,0.0,31.0]|[0.0,0.0,1.0]|       2.0|         Scala|
|[7.2,3.0,5.8,1.6]| Scala|         2.0|[7.2,3.0,5.8,1.6]|[29.0,0.0,0.0]|[1.0,0.0,0.0]|       0.0|         spark|
|[7.2,3.2,6.0,1.8]| Scala|         2.0|[7.2,3.2,6.0,1.8]|[0.0,0.0,31.0]|[0.0,0.0,1.0]|       2.0|         Scala|
|[7.6,3.0,6.6,2.1]| Scala|         2.0|[7.6,3.0,6.6,2.1]|[0.0,0.0,31.0]|[0.0,0.0,1.0]|       2.0|         Scala|
|[7.7,3.0,6.1,2.3]| Scala|         2.0|[7.7,3.0,6.1,2.3]|[0.0,0.0,31.0]|[0.0,0.0,1.0]|       2.0|         Scala|
|[7.7,3.8,6.7,2.2]| Scala|         2.0|[7.7,3.8,6.7,2.2]|[0.0,0.0,31.0]|[0.0,0.0,1.0]|       2.0|         Scala|
|[7.9,3.8,6.4,2.0]| Scala|         2.0|[7.9,3.8,6.4,2.0]|[0.0,0.0,31.0]|[0.0,0.0,1.0]|       2.0|         Scala|
+-----------------+------+------------+-----------------+--------------+-------------+----------+--------------+

准确率为: 0.8958333333333334
错误率为: 0.10416666666666663
决策树的结构为: DecisionTreeClassificationModel (uid=dtc_218264842cd2) of depth 5 with 15 nodes
  If (feature 2 <= 1.9)
   Predict: 1.0
  Else (feature 2 > 1.9)
   If (feature 3 <= 1.7)
    If (feature 0 <= 4.9)
     Predict: 2.0
    Else (feature 0 > 4.9)
     If (feature 1 <= 2.2)
      If (feature 2 <= 4.0)
       Predict: 0.0
      Else (feature 2 > 4.0)
       Predict: 2.0
     Else (feature 1 > 2.2)
      Predict: 0.0
   Else (feature 3 > 1.7)
    If (feature 2 <= 4.8)
     If (feature 0 <= 5.9)
      Predict: 0.0
     Else (feature 0 > 5.9)
      Predict: 2.0
    Else (feature 2 > 4.8)
     Predict: 2.0

Spark 决策树--分类模型的更多相关文章

  1. Spark 决策树--回归模型

    package Spark_MLlib import org.apache.spark.ml.Pipeline import org.apache.spark.ml.evaluation.Regres ...

  2. spark 决策树分类算法demo

    分类(Classification) 下面的例子说明了怎样导入LIBSVM 数据文件,解析成RDD[LabeledPoint],然后使用决策树进行分类.GINI不纯度作为不纯度衡量标准并且树的最大深度 ...

  3. R语言决策树分类模型

    rm(list=ls()) gc() memory.limit(4000) library(corrplot) library(rpart) data_health<-read.csv(&quo ...

  4. Spark学习笔记——构建分类模型

    Spark中常见的三种分类模型:线性模型.决策树和朴素贝叶斯模型. 线性模型,简单而且相对容易扩展到非常大的数据集:线性模型又可以分成:1.逻辑回归:2.线性支持向量机 决策树是一个强大的非线性技术, ...

  5. Spark机器学习4·分类模型(spark-shell)

    线性模型 逻辑回归--逻辑损失(logistic loss) 线性支持向量机(Support Vector Machine, SVM)--合页损失(hinge loss) 朴素贝叶斯(Naive Ba ...

  6. 笔记︱风控分类模型种类(决策、排序)比较与模型评估体系(ROC/gini/KS/lift)

    每每以为攀得众山小,可.每每又切实来到起点,大牛们,缓缓脚步来俺笔记葩分享一下吧,please~ --------------------------- 本笔记源于CDA-DSC课程,由常国珍老师主讲 ...

  7. 初识spark的MLP模型

    初识Spark的MLP模型 1. MLP介绍 Multi-layer Perceptron(MLP),即多层感知器,是一个前馈式的.具有监督的人工神经网络结构.通过多层感知器可包含多个隐藏层,实现对非 ...

  8. sklearn CART决策树分类

    sklearn CART决策树分类 决策树是一种常用的机器学习方法,可以用于分类和回归.同时,决策树的训练结果非常容易理解,而且对于数据预处理的要求也不是很高. 理论部分 比较经典的决策树是ID3.C ...

  9. ML(4): 决策树分类

    决策树(Decision Tree)是用于分类和预测的主要技术,它着眼于从一组无规则的事例推理出决策树表示形式的分类规则,采用自顶向下的递归方式,在决策树的内部节点进行属性值的比较,并根据不同属性判断 ...

随机推荐

  1. javascript事件中'return false'详解

    浏览器中有很多异步事件,如click,mouseenter,mouseover等等,当用户执行相应操作之后,触发这个事件,然后执行相应的事件处理函数,一般情况下,我们可以通过三种方式给元素添加事件处理 ...

  2. 字符串类String类的判断功能

    StringDemo.java /* * Object:是类层级结构中的根类,所有的类都直接或间接的继承自该类. * 如果一个方法的形式参数是Object,那么这里我们就可以传递它的任意的子类对象. ...

  3. AndroidSweetSheet:ViewPager的实现(2)

     AndroidSweetSheet:ViewPager的实现(2) 附录文章9说明了AndroidSweetSheet典型的列表样式实现,本文写一个例子,说明AndroidSweetSheet以 ...

  4. php.ini中date.timezone设置分析

    date.timezone设置php5默认date.timezone为utc,改为date.timezone = PRC即可解决时间相差八小时的问题,但我在php的官方文档中看了半天也没找到这个参数啊 ...

  5. codeforces 363B

    #include<stdio.h> #include<string.h> #define inf 999999999 #define N 151000 int a[N],c[N ...

  6. CentOS虚拟机与本机同步时间

    接着之前的任务,还是为了在VMWare上搭建分布式hadoop集群.搭着搭着注意到虚拟机上的时间和本机是不同步的,而且可以说是乱七八糟,3台虚拟机时间都与本机差了8个小时以上.首先确认不是时区的问题, ...

  7. Codeforces 629D Babaei and Birthday Cake(线段树优化dp)

    题意: n个蛋糕编号从小到大编号,j号蛋糕可以放在i号上面,当且仅当j的体积严格大于i且i<j,问最终可得的最大蛋糕体积. 分析: 实质为求最长上升子序列问题,设dp[i]从头开始到第i位的最长 ...

  8. Spring中通过java的@Valid注解和@ControllerAdvice实现全局异常处理。

    通过java原生的@Valid注解和spring的@ControllerAdvice和@ExceptionHandler实现全局异常处理的方法: controller中加入@Valid注解: @Req ...

  9. Java度线程——生产消费问题

    /*JDK1.4版本:生产者,消费者.多生产者,多消费者的问题.if判断标记,只有一次,会导致不该运行的线程运行了.出现了数据错误的情况.while判断标记,解决了线程获取执行权后,是否要运行! no ...

  10. Ubuntu 16.04硬盘有坏道,开机显示blk_update_request:I/0 error

    可以尝试以下方式解决: 1.检查坏道(效果明显,但是比较慢,检查出来并没有什么用,只是知道有坏块) sudo badblocks -s -v -o /root/bb.log /dev/sda1 2.快 ...