spark 基础八 MLlib

MLlib是spark中提供机器学习函数的库。它是专为在集群上并行运行的情况而设计的,设计理念非常简单:把数据以RDD形式表示,然后在分布式数据集上调用各种算法。可以将其看作RDD上一系列可供调用的函数的集合。MLlib中只包含能够在集群上运行良好的并行算法。MLlib的算法适用于大规模数据集,如果要在许多小规模数据集上训练各机器学习模型,最好还是在各节点上使用单节点的机器学习算法库实现,比如spark的map操作在各节点上并行使用。

#python 垃圾邮件分类器

from pyspark.mllib.regression import LabeledPoint
from pyspark.mllib.feature import HashingTF
from pyspark.mllib.classification import LogisticRegressionWithSGD

spam=sc.textFile("spam.txt")
normal = sc.textFile("normal.txt")
                 
#create a HahsingTF instance to map the text to a 10000 vector
tf=HashingTF(numFeatures=10000)

#split the text to words and transform to feature vector
spamFeatures=spam.map(lambda email : tf.transform(email.split("")))
normalFeatures=normal.map(lambda email : tf.transfrom(email.split("")))

#create dataset to save the positive and negati

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值