博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spark学习笔记——基于MLlib的机器学习
阅读量:7070 次
发布时间:2019-06-28

本文共 2661 字,大约阅读时间需要 8 分钟。

使用MLlib库中的机器学习算法对垃圾邮件进行分类

分类的垃圾邮件的如图中分成4个文件夹,两个文件夹是训练集合,两个文件夹是测试集合

build.sbt文件

name := "spark-first"version := "1.0"scalaVersion := "2.11.8"libraryDependencies ++= Seq(  "org.apache.spark" % "spark-core_2.11" % "2.1.0",  "org.apache.hadoop" % "hadoop-common" % "2.7.2",  "mysql" % "mysql-connector-java" % "5.1.31",  "org.apache.spark" %% "spark-sql" % "2.1.0",  "org.apache.spark" %% "spark-streaming" % "2.1.0",  "org.apache.spark" % "spark-mllib_2.11" % "2.1.0")

代码

import org.apache.hadoop.io.{IntWritable, LongWritable, MapWritable, Text}import org.apache.spark.SparkContextimport org.apache.spark.SparkConfimport org.apache.spark._import org.apache.hadoop.mapreduce.Jobimport org.apache.hadoop.mapreduce.lib.input.KeyValueTextInputFormatimport org.apache.hadoop.mapreduce.lib.output.TextOutputFormatimport org.apache.spark.sql.SQLContextimport java.util.Propertiesimport org.apache.spark.streaming.StreamingContextimport org.apache.spark.streaming.StreamingContext._import org.apache.spark.streaming.dstream.DStreamimport org.apache.spark.streaming.Durationimport org.apache.spark.streaming.Secondsimport org.apache.spark.mllib.regression.LabeledPointimport org.apache.spark.mllib.feature.HashingTFimport org.apache.spark.mllib.classification.LogisticRegressionWithSGD/**  * Created by common on 17-4-6.  */object SparkRDD {  def main(args: Array[String]) {    val conf = new SparkConf().setAppName("WordCount").setMaster("local")    val sc = new SparkContext(conf)    val spam = sc.textFile("input/email/spam")    val normal = sc.textFile("input/email/ham")    // 创建一个HashingTF实例来把邮件文本映射为包含10000个特征的向量    val tf = new HashingTF(numFeatures = 10000)    // 各邮件都被切分为单词,每个单词被映射为一个特征    val spamFeatures = spam.map(email => tf.transform(email.split(" ")))    val normalFeatures = normal.map(email => tf.transform(email.split(" ")))    // 创建LabeledPoint数据集分别存放阳性(垃圾邮件)和阴性(正常邮件)的例子    val positiveExamples = spamFeatures.map(features => LabeledPoint(1, features))    val negativeExamples = normalFeatures.map(features => LabeledPoint(0, features))    val trainingData = positiveExamples.union(negativeExamples)    trainingData.cache() // 因为逻辑回归是迭代算法,所以缓存训练数据RDD    // 使用SGD算法运行逻辑回归    val model = new LogisticRegressionWithSGD().run(trainingData)    // 以阳性(垃圾邮件)和阴性(正常邮件)的例子分别进行测试    val posTest = tf.transform(      "Experience with BiggerPenis Today! Grow 3-inches more ...".split(" "))    val negTest = tf.transform(      "That is cold.  Is there going to be a retirement party? ...".split(" "))    println("Prediction for positive test example: " + model.predict(posTest))    println("Prediction for negative test example: " + model.predict(negTest))  }}

 

结果

 

转载地址:http://ishll.baihongyu.com/

你可能感兴趣的文章
手把手教你用动软.NET代码生成器实例教程
查看>>
栈分配的速度快于堆
查看>>
[转] 使用memc-nginx和srcache-nginx模块构建高效透明的缓存机制
查看>>
CF-Pasha and Tea(贪心6)
查看>>
ASCII_01
查看>>
Mac控制远程Linux服务器
查看>>
R语言基础命令与安装
查看>>
SDWebImage从缓存中获取图片
查看>>
python基础一
查看>>
Excel2Json记录
查看>>
springmvc常用注解标签详解
查看>>
游戏行业术语一览(1)--游戏分类
查看>>
【2053】整理音乐 SDUTOJ
查看>>
mongodb集合的增删
查看>>
LeetCode 161: One Edit Distance
查看>>
SPOJ220 Relevant Phrases of Annihilation
查看>>
python基础学习10----集合
查看>>
ETL工具—Kettle数据的导入导出—批量Excel表到数据库
查看>>
Android UI开发 View自绘控件
查看>>
知识思考
查看>>