转自:http://www.cnblogs.com/canyangfeixue/p/7227998.html 对于威胁检测算法使用神经网络训练有用!!!TODO待实验

/**
* Created by lkl on 2017/7/21.
*/
//import com.ibm.spark.exercise.util.LogUtils
//import com.ibm.spark.exercise.util.LogUtils
import org.apache.spark.ml.Pipeline
import org.apache.spark.ml.classification.MultilayerPerceptronClassifier
import org.apache.spark.ml.evaluation.MulticlassClassificationEvaluator
import org.apache.spark.ml.feature.{IndexToString, StringIndexer, Word2Vec}
import org.apache.spark.sql.SQLContext
import org.apache.spark.{SparkContext, SparkConf}
import org.apache.spark.{SparkConf, SparkContext}
import org.apache.spark.sql.SQLContext
object mllib { final val VECTOR_SIZE = 1000
// def main(args: Array[String]) {
// if (args.length < 1) {
// println("Usage:SMSClassifier SMSTextFile")
// sys.exit(1)
// }
def main(args: Array[String]) {
val conf = new SparkConf().setMaster("local").setAppName("test")
val sc = new SparkContext(conf)
val sqlContext = new org.apache.spark.sql.SQLContext(sc) // val role = "jdbc:mysql://192.168.0.37:3306/emotional?user=root&password=123456&useUnicode=true&characterEncoding=utf8&autoReconnect=true&failOverReadOnly=false"
// import sqlContext.implicits._
// val df = sc.textFile("hdfs://192.168.0.211:9000/user/hadoop/emotion/SMS.txt").map(line=>(line.split(" ")(0),line.split(" ")(1),line.split(" ")(2),line.split(" ")(3))).toDF("id","innserSessionid","words","value")
// df.printSchema()
// df.insertIntoJDBC(role, "SMS", true)
val sqlCtx =new org.apache.spark.sql.SQLContext(sc)
import sqlContext.implicits._
// 读取hdfs 数据源,格式如下:以空格隔开,最后一列数字列是分析标题后,人为打上的标签,
值是按照情绪程度,值选择于【-1,-0.75,-0.5,-0.25,,0.25,0.50,0.75,1】其中之一。
// 10090 C779C882AA39436A89C463BCB406B838 涨停板,复盘,全,靠,新,股,撑,门面,万科,A,尾盘,封板 0.75
// 10091 519A9C6AD0A845298B0B3924117C0B4F 一,行业,再现,重大,利好,板块,反弹,仍,将,继续 0.75
// 10092 C86CEC7DB9794311AF386C3D7B0B7CBD 藁城区,3,大,项目,新,获,规划证,开发,房企,系,同,一家 0
// 10093 FCEA2FFC1C2F4D6C808F2CBC2FF18A8C 完善,对,境外,企业,和,对外,投资,统计,监测 0.5
// 10094 204A77847F03404986331810E039DFC2 财联社,电报 0
// 10095 E571B9EF451F4D5F8426A1FA06CD9EE6 审计署,部分,央企,业绩,不,实 -0.5
// 10096 605264A2F6684CC4BB4B2A0B6A8FA078 厨卫,品牌,新,媒体,榜,看看,谁家,的,官微,最,爱,卖萌 0.25
val parsedRDD = sc.textFile("hdfs://192.168.0.211:9000/user/hadoop/emotion/SMS.txt").map(line=>{
val a = line.split(" ")
if(a.length == 4 ){
(line.split(" ")(3),line.split(" ")(2).split(","))
}else{
("","".split(","))
}
})
val msgDF = sqlCtx.createDataFrame(parsedRDD).toDF("label","message")
val labelIndexer = new StringIndexer().setInputCol("label").setOutputCol("indexedLabel").fit(msgDF)
val word2Vec = new Word2Vec().setInputCol("message").setOutputCol("features").setVectorSize(VECTOR_SIZE).setMinCount(1) val layers = Array[Int](VECTOR_SIZE,250,500,200)
val mlpc = new MultilayerPerceptronClassifier().setLayers(layers).setBlockSize(512).setSeed(1234L).setMaxIter(128).setFeaturesCol("features").setLabelCol("indexedLabel").setPredictionCol("prediction") val labelConverter = new IndexToString().setInputCol("prediction").setOutputCol("predictedLabel").setLabels(labelIndexer.labels) val Array(trainingData, testData) = msgDF.randomSplit(Array(0.8, 0.2))
val pipeline = new Pipeline().setStages(Array(labelIndexer,word2Vec,mlpc,labelConverter))
val model = pipeline.fit(trainingData)
val predictionResultDF = model.transform(testData)
//below 2 lines are for debug use
predictionResultDF.printSchema
predictionResultDF.select("message","label","predictedLabel").show(30)
val evaluator = new MulticlassClassificationEvaluator().setLabelCol("indexedLabel").setPredictionCol("prediction").setMetricName("precision")
val predictionAccuracy = evaluator.evaluate(predictionResultDF)
println("Testing Accuracy is %2.4f".format(predictionAccuracy * 100) + "%")
// sc.stop }
}
 

结果如下:

+--------------------+-----+--------------+
| message|label|predictedLabel|
+--------------------+-----+--------------+
|[价格会, 一飞, 冲天, 神秘,...| 0.5| 0.5|
|[审计署, 部分, 央企, 业绩,...| -0.5| 0.5|
|[广电, 总局, 新浪, 微博, ...| -0.5| 0.5|
|[叶檀, 若, 粤, 港澳湾区, ...| 0.25| 0.5|
| [万达, 崩, 万科, 起]| 0| 0.5|
|[外汇, 小白, 必, 看, 视频...| 0.25| 0.5|
|[乐视, 回, 应发, 不, 出,...|-0.75| 0.5|
|[万达, 电影, 高开, 1.69...| 0.5| 0.5|
|[万科, A, 股, 6月, 23...| 0.75| 0.5|
|[金价, 周一, 反弹, 扭转, ...| 0.5| 0.5|
|[收评, 两, 市, 震荡, 沪指...| 0.25| 0.5|
|[点睛, 军工, 混改, 加速, ...| 0.5| 0.5|
|[棉花, 日报, 棉花, 短期, ...| 0.25| 0.5|
|[探秘, 巴铁, 试验线, 部分,...|-0.75| 0.5|
|[万达, 复星, 股价, 暴跌, ...|-0.75| 0.5|
|[油价, 迎, 年内, 最, 大,...|-0.25| 0.5|
|[2017年, IPO, 被, 否...|-0.75| 0.5|
|[股, 转, 监事长, 邓映翎, ...| -0.5| 0.5|
|[发改委, 国内, 汽, 柴油, ...|-0.25| 0.5|
|[周报, 明晟, MSCI, 宣布...| 0.5| 0.5|
|[夏季, 达沃斯, 共识, 中国,...| 0.5| 0.5|
|[重磅, 又, 一, 家, 公司,...|-0.75| 0.5|
|[麦格里, 重磅, 警告, OPE...| -0.5| 0.5|
|[韩国, 娱乐, 公司, TO-W...| 0.5| 0.5|
| [新, 三, 板, 周报]| 0| 0.5|
|[分享, 华尔街, 对, 美国, ...| 0.5| 0.5|
|[盛和, 资源, 2015年, 公...| 0| 0.5|
|[交易, 实况, 黄金, 两, 连...| -0.5| 0.5|
|[徽商, 银行, 内斗戏, 第二,...| -0.5| 0.5|
|[2017, 夏季, 达沃斯, 论...| 0.25| 0.5|

最新文章

  1. jsp页面中jstl标签详解
  2. async 和 await小结
  3. TSuperEnumerator、TSuperAvlIterator、ObjectFindFirst
  4. print_r、echo、var_dump三者的区别
  5. mac app icon 设置
  6. 表设计VIso
  7. 开始学习C++ Templates
  8. NSPoint
  9. C# Timer执行方法
  10. r语言之散点图绘制及参数
  11. C++和JNI的数据转换
  12. Android架构分析之LOG模块
  13. 数据库ACID,SQL和NoSQL
  14. ASP.NET Core 统一异常处理和返回
  15. Spring Boot + Spring Cloud 实现权限管理系统 后端篇(十五):系统服务监控
  16. 关于Bootstrap自定义图标
  17. Sliding Window Maximum LT239
  18. Android使用AOP
  19. SqlHelper DBHelper
  20. 【C语言】指针数组

热门文章

  1. C#-基础部分思维导图
  2. SQL SERVER-常用命令2
  3. POJ 2369
  4. Maven简单介绍(Maven是什么)
  5. 匿名訪问之(一)web application级别
  6. po层和vo层中po和vo是什么意思
  7. hdu 4628 Pieces(状态压缩+记忆化搜索)
  8. zzulioj--1777--和尚特烦恼3——何时能下山(水题)
  9. Laravel-redis-订阅发布
  10. WIN32常用