package Spark_MLlib

import org.apache.spark.ml.Pipeline
import org.apache.spark.ml.classification.{LogisticRegression, LogisticRegressionModel}
import org.apache.spark.ml.evaluation.MulticlassClassificationEvaluator
import org.apache.spark.ml.feature.{IndexToString, StringIndexer, VectorIndexer}
import org.apache.spark.ml.linalg.Vectors
import org.apache.spark.sql.SparkSession object 多项式逻辑回归__二分类 {
val spark=SparkSession.builder().master("local").getOrCreate()
import spark.implicits._ //支持把一个RDD隐式转换为一个DataFrame
def main(args: Array[String]): Unit = {
val df =spark.sparkContext.textFile("file:///home/soyo/桌面/spark编程测试数据/soyo.txt")
.map(_.split(",")).map(x=>data_schema(Vectors.dense(x().toDouble,x().toDouble,x().toDouble,x().toDouble),x())).toDF()
df.show()
df.createOrReplaceTempView("data_schema")
val df_data=spark.sql("select * from data_schema where label !='soyo2'") //这里soyo2需要加单引号,不然报错
// df_data.map(x=>x(1)+":"+x(0)).collect().foreach(println)
df_data.show()
val labelIndexer=new StringIndexer().setInputCol("label").setOutputCol("indexedLabel").fit(df_data)
val featureIndexer=new VectorIndexer().setInputCol("features").setOutputCol("indexedFeatures").fit(df_data) //目的在特征向量中建类别索引
val Array(trainData,testData)=df_data.randomSplit(Array(0.7,0.3))
val lr=new LogisticRegression().setLabelCol("indexedLabel").setFeaturesCol("indexedFeatures").setMaxIter().setRegParam(0.3).setElasticNetParam(0.8).setFamily("multinomial")//设置elasticnet混合参数为0.8,setFamily("multinomial"):设置为多项逻辑回归,不设置setFamily为二项逻辑回归
val labelConverter=new IndexToString().setInputCol("prediction").setOutputCol("predictionLabel").setLabels(labelIndexer.labels) val lrPipeline=new Pipeline().setStages(Array(labelIndexer,featureIndexer,lr,labelConverter))
val lrPipeline_Model=lrPipeline.fit(trainData)
val lrPrediction=lrPipeline_Model.transform(testData)
lrPrediction.show(false)
// lrPrediction.take(100).foreach(println)
//模型评估
val evaluator=new MulticlassClassificationEvaluator().setLabelCol("indexedLabel").setPredictionCol("prediction")
val lrAccuracy=evaluator.evaluate(lrPrediction)
println("准确率为: "+lrAccuracy)
val lrError=-lrAccuracy
println("错误率为: "+lrError)
val LRmodel=lrPipeline_Model.stages().asInstanceOf[LogisticRegressionModel]
println("二项逻辑回归模型系数矩阵: "+LRmodel.coefficientMatrix)
println("二项逻辑回归模型的截距向量: "+LRmodel.interceptVector)
println("类的数量(标签可以使用的值): "+LRmodel.numClasses)
println("模型所接受的特征的数量: "+LRmodel.numFeatures) } }

结果:

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

+-----------------+-----+------------+------------------+------------------------------------------+----------------------------------------+----------+---------------+
|features         |label|indexedLabel|indexedFeatures   |rawPrediction                             |probability                             |prediction|predictionLabel|
+-----------------+-----+------------+------------------+------------------------------------------+----------------------------------------+----------+---------------+
|[4.6,3.1,1.5,0.2]|soyo1|0.0         |[4.6,3.1,1.5,1.0] |[0.3841092104753886,-0.384109210475388]   |[0.6831353764654857,0.3168646235345142] |0.0       |soyo1          |
|[4.6,3.2,1.4,0.2]|soyo1|0.0         |[4.6,3.2,1.4,1.0] |[0.4118074545189242,-0.41180745451892353] |[0.6950031457169539,0.3049968542830461] |0.0       |soyo1          |
|[4.6,3.4,1.4,0.3]|soyo1|0.0         |[4.6,3.4,1.4,2.0] |[0.41345332780578103,-0.41345332780578037]|[0.6957004614212158,0.30429953857878417]|0.0       |soyo1          |
|[4.7,3.2,1.6,0.2]|soyo1|0.0         |[4.7,3.2,1.6,1.0] |[0.39085103161962165,-0.390851031619621]  |[0.6860468315498303,0.31395316845016974]|0.0       |soyo1          |
|[4.9,3.0,1.4,0.2]|soyo1|0.0         |[4.9,3.0,1.4,1.0] |[0.37736738933115554,-0.377367389331155]  |[0.6802095073085258,0.3197904926914742] |0.0       |soyo1          |
|[4.9,3.1,1.5,0.1]|soyo1|0.0         |[4.9,3.1,1.5,0.0] |[0.4169034023763003,-0.4169034023762997]  |[0.697159256477463,0.302840743522537]   |0.0       |soyo1          |
|[5.0,3.0,1.6,0.2]|soyo1|0.0         |[5.0,3.0,1.6,1.0] |[0.356410966431853,-0.35641096643185244]  |[0.6710244037082002,0.32897559629179984]|0.0       |soyo1          |
|[5.0,3.4,1.5,0.2]|soyo1|0.0         |[5.0,3.4,1.5,1.0] |[0.4357693082570414,-0.4357693082570408]  |[0.705065751202206,0.2949342487977939]  |0.0       |soyo1          |
|[5.0,3.4,1.6,0.4]|soyo1|0.0         |[5.0,3.4,1.6,3.0] |[0.35970271300556683,-0.35970271300556617]|[0.6724760743873281,0.3275239256126718] |0.0       |soyo1          |
|[5.1,3.4,1.5,0.2]|soyo1|0.0         |[5.1,3.4,1.5,1.0] |[0.4357693082570414,-0.4357693082570408]  |[0.705065751202206,0.2949342487977939]  |0.0       |soyo1          |
|[5.4,3.4,1.7,0.2]|soyo1|0.0         |[5.4,3.4,1.7,1.0] |[0.4148128853577389,-0.41481288535773825] |[0.6962757951954652,0.3037242048045349] |0.0       |soyo1          |
|[5.6,2.8,4.9,2.0]|soyo3|1.0         |[5.6,2.8,4.9,12.0]|[-0.3845461875044362,0.38454618750443703] |[0.3166754764713344,0.6833245235286656] |1.0       |soyo3          |
|[5.7,3.8,1.7,0.3]|soyo1|0.0         |[5.7,3.8,1.7,2.0] |[0.45089882383236457,-0.4508988238323638] |[0.7113187796385543,0.2886812203614457] |0.0       |soyo1          |
|[5.7,4.4,1.5,0.4]|soyo1|0.0         |[5.7,4.4,1.5,3.0] |[0.5423812503940613,-0.5423812503940606]  |[0.7473941839256351,0.25260581607436505]|0.0       |soyo1          |
|[5.8,2.8,5.1,2.4]|soyo3|1.0         |[5.8,2.8,5.1,16.0]|[-0.5366793780073855,0.5366793780073863]  |[0.2547648665744027,0.7452351334255972] |1.0       |soyo3          |
|[6.0,2.2,5.0,1.5]|soyo3|1.0         |[6.0,2.2,5.0,7.0] |[-0.3343736350128348,0.33437363501283546] |[0.3387774047228901,0.6612225952771099] |1.0       |soyo3          |
|[6.2,2.8,4.8,1.8]|soyo3|1.0         |[6.2,2.8,4.8,10.0]|[-0.3084795922529615,0.30847959225296234] |[0.3504733529544735,0.6495266470455265] |1.0       |soyo3          |
|[6.3,2.9,5.6,1.8]|soyo3|1.0         |[6.3,2.9,5.6,10.0]|[-0.3750852512562874,0.3750852512562882]  |[0.3207841503157466,0.6792158496842534] |1.0       |soyo3          |
|[6.3,3.3,6.0,2.5]|soyo3|1.0         |[6.3,3.3,6.0,17.0]|[-0.5776773099857371,0.577677309985738]   |[0.23951239936093965,0.7604876006390604]|1.0       |soyo3          |
|[6.3,3.4,5.6,2.4]|soyo3|1.0         |[6.3,3.4,5.6,16.0]|[-0.485750239692336,0.4857502396923369]   |[0.2745815258875292,0.7254184741124707] |1.0       |soyo3          |
+-----------------+-----+------------+------------------+------------------------------------------+----------------------------------------+----------+---------------+
only showing top 20 rows

准确率为: 1.0
错误率为: 0.0
二项逻辑回归模型系数矩阵: 0.0  0.17220032593884316  -0.1047821144965127  -0.03279419190091169  
0.0  -0.172200325938843   0.10478211449651276  0.03279419190091169   
二项逻辑回归模型的截距向量: [0.04025556371065551,-0.04025556371065551]
类的数量(标签可以使用的值): 2
模型所接受的特征的数量: 4

Spark 多项式逻辑回归__二分类的更多相关文章

  1. Spark 多项式逻辑回归__多分类

    package Spark_MLlib import org.apache.spark.ml.Pipeline import org.apache.spark.ml.classification.{B ...

  2. Spark 二项逻辑回归__二分类

    package Spark_MLlib import org.apache.spark.ml.Pipeline import org.apache.spark.ml.classification.{B ...

  3. scikit-learn机器学习(二)逻辑回归进行二分类(垃圾邮件分类),二分类性能指标,画ROC曲线,计算acc,recall,presicion,f1

    数据来自UCI机器学习仓库中的垃圾信息数据集 数据可从http://archive.ics.uci.edu/ml/datasets/sms+spam+collection下载 转成csv载入数据 im ...

  4. 机器学习---逻辑回归(二)(Machine Learning Logistic Regression II)

    在<机器学习---逻辑回归(一)(Machine Learning Logistic Regression I)>一文中,我们讨论了如何用逻辑回归解决二分类问题以及逻辑回归算法的本质.现在 ...

  5. stanford coursera 机器学习编程作业 exercise 3(逻辑回归实现多分类问题)

    本作业使用逻辑回归(logistic regression)和神经网络(neural networks)识别手写的阿拉伯数字(0-9) 关于逻辑回归的一个编程练习,可参考:http://www.cnb ...

  6. Logistic Regression(逻辑回归)(二)—深入理解

    (整理自AndrewNG的课件,转载请注明.整理者:华科小涛@http://www.cnblogs.com/hust-ghtao/) 上一篇讲解了Logistic Regression的基础知识,感觉 ...

  7. 【原】Spark之机器学习(Python版)(二)——分类

    写这个系列是因为最近公司在搞技术分享,学习Spark,我的任务是讲PySpark的应用,因为我主要用Python,结合Spark,就讲PySpark了.然而我在学习的过程中发现,PySpark很鸡肋( ...

  8. Spark Mllib逻辑回归算法分析

    原创文章,转载请注明: 转载自http://www.cnblogs.com/tovin/p/3816289.html 本文以spark 1.0.0版本MLlib算法为准进行分析 一.代码结构 逻辑回归 ...

  9. Spark LogisticRegression 逻辑回归之建模

    导入包 import org.apache.spark.sql.SparkSession import org.apache.spark.sql.Dataset import org.apache.s ...

随机推荐

  1. Python-基本图形绘制及库引用

    turtle库的使用 概述:turtle(海龟)库是turtle绘图体系的python实现 turtle库的理解: -有一只海龟,其实在窗体正中心,在画布上游走 -走过的轨迹形成了绘制的图形 -海龟由 ...

  2. JS 比较运算符 逻辑运算符

    逻辑运算符 三元运算符 摘自:http://www.w3school.com.cn/js/js_comparisons.asp

  3. 《C语言程序设计(第四版)》阅读心得(四 文件操作)

    第10章  对文件的输入输出 函数名 调用形式 功能 fopen fopen(“a1”,”r”); 打开一个文件 fclose fclose( fp ); 关闭数据文件 fgetc fgetc( fp ...

  4. Xcode4.5.1破解iOS免证书开发真机调试与ipa发布

    开发环境使用Mac OSX Mountain Lion 10.8 + Xcode 4.5.1,iOS设备需要越狱并从Cydia安装AppSync.Xcode4.5.1的安装破解详细步骤如下: 第一步, ...

  5. 莫比乌斯反演套路一--令t=pd--BZOJ2820: YY的GCD

    t<=10000组询问:有多少x,y,满足$x\epsilon [1,n],y\epsilon [1,m],(x,y)为质数$.n,m<=1e7. 首先式子列出来,f(i)--1<= ...

  6. MongoDB学习day02--数据库增删改查

    (window系统,在cmd命令提示符中使用) 一.数据库使用 管理mongodb数据库:mongo,连接本地数据库,或mongo 127.0.0.1:27017,连接其他服务器:mongo  ip: ...

  7. html自动换行

    对于div,p等块级元素 正常文字的换行(亚洲文字和非亚洲文字)元素拥有默认的white-space:normal,当定义的宽度之后自动换行html css 1.(IE浏览器)连续的英文字符和阿拉伯数 ...

  8. openstack setup demo Overview

    Overview openstack是一套开源的云计算部署平台,通过一系列service提供IAAS.每一个service都提供API.具体的service列表如下: dashboard Horizo ...

  9. mysql利用timestamp来进行帖子排序

    select * from table order by timestamp descorder by 该列 desc timestamp字段也可以用来排序,对应Java类型的.net.timesta ...

  10. ABAP学习之旅——多种方式建立模块化功能

    在ABAP中.有多种方法能够建立模块化的功能. 以下依次对其种三种进行介绍. 一.            使用子程序(Subroutine) 1.      基本的语法: FORM subname. ...