Spark机器学习之垃圾邮件分类
步骤概述
通过HashingTF构建文本的特征向量,然后使用随机梯度下降算法实现逻辑回归,进而对邮件进行分类
垃圾邮件分类代码
导入相关的包
import org.apache.spark.mllib.regression.LabeledPoint
import org.apache.spark.mllib.feature.HashingTF
import org.apache.spark.mllib.classification.LogisticRegressionWithSGD
加载文件
val spam = sc.textFile("file://media/hadoop/Ubuntu/spam.txt")
val normal = sc.textFile("file:///media/hadoop/Ubuntu/normal.txt")
其中spam.txt和normal.txt文件如下
spam.txt
normal.txt
创建一个HashingTF实例把邮件文本映射到包含1000个特征的向量
val tf = new HashingTF(numFeatures = 10000)