转载自:https://vimsky.com/article/3403.html

Spark中ml和mllib的主要区别和联系如下:

  • ml和mllib都是Spark中的机器学习库,目前常用的机器学习功能2个库都能满足需求。
  • spark官方推荐使用ml, 因为ml功能更全面更灵活,未来会主要支持ml,mllib很有可能会被废弃(据说可能是在spark3.0中deprecated)。
  • ml主要操作的是DataFrame, 而mllib操作的是RDD,也就是说二者面向的数据集不一样。相比于mllib在RDD提供的基础操作,ml在DataFrame上的抽象级别更高,数据和操作耦合度更低。
    • DataFrame和RDD什么关系?DataFrame是Dataset的子集,也就是Dataset[Row], 而DataSet是对RDD的封装,对SQL之类的操作做了很多优化。
  • 相比于mllib在RDD提供的基础操作,ml在DataFrame上的抽象级别更高,数据和操作耦合度更低。
  • ml中的操作可以使用pipeline, 跟sklearn一样,可以把很多操作(算法/特征提取/特征转换)以管道的形式串起来,然后让数据在这个管道中流动。大家可以脑补一下Linux管道在做任务组合时有多么方便。
  • ml中无论是什么模型,都提供了统一的算法操作接口,比如模型训练都是fit;不像mllib中不同模型会有各种各样的trainXXX
  • mllib在spark2.0之后进入维护状态, 这个状态通常只修复BUG不增加新功能。

以上就是ml和mllib的主要异同点。下面是ml和mllib逻辑回归的例子,可以对比看一下, 虽然都是模型训练和预测,但是画风很不一样。

mllib中逻辑回归的例子

    sparse_data = [
LabeledPoint(0.0, SparseVector(, {: 0.0})),
LabeledPoint(1.0, SparseVector(, {: 1.0})),
LabeledPoint(0.0, SparseVector(, {: 1.0})),
LabeledPoint(1.0, SparseVector(, {: 2.0}))
]
lrm = LogisticRegressionWithSGD.train(sc.parallelize(sparse_data), iterations=)
lrm.predict(array([0.0, 1.0])) lrm.predict(array([1.0, 0.0])) lrm.predict(SparseVector(, {: 1.0})) lrm.predict(SparseVector(, {: 1.0})) import os, tempfile
path = tempfile.mkdtemp()
lrm.save(sc, path)
sameModel = LogisticRegressionModel.load(sc, path)
sameModel.predict(array([0.0, 1.0])) sameModel.predict(SparseVector(, {: 1.0})) from shutil import rmtree
try:
rmtree(path)
except:
pass
multi_class_data = [
LabeledPoint(0.0, [0.0, 1.0, 0.0]),
LabeledPoint(1.0, [1.0, 0.0, 0.0]),
LabeledPoint(2.0, [0.0, 0.0, 1.0])
]
data = sc.parallelize(multi_class_data)
mcm = LogisticRegressionWithLBFGS.train(data, iterations=, numClasses=)
mcm.predict([0.0, 0.5, 0.0]) mcm.predict([0.8, 0.0, 0.0]) mcm.predict([0.0, 0.0, 0.3])

ml中的逻辑回归的例子

 from pyspark.sql import Row
from pyspark.ml.linalg import Vectors
bdf = sc.parallelize([
Row(label=1.0, weight=2.0, features=Vectors.dense(1.0)),
Row(label=0.0, weight=2.0, features=Vectors.sparse(, [], []))]).toDF()
blor = LogisticRegression(maxIter=, regParam=0.01, weightCol="weight")
blorModel = blor.fit(bdf)
blorModel.coefficients
DenseVector([5.5 ])
blorModel.intercept
-2.68
mdf = sc.parallelize([
Row(label=1.0, weight=2.0, features=Vectors.dense(1.0)),
Row(label=0.0, weight=2.0, features=Vectors.sparse(, [], [])),
Row(label=2.0, weight=2.0, features=Vectors.dense(3.0))]).toDF()
mlor = LogisticRegression(maxIter=, regParam=0.01, weightCol="weight",
family="multinomial")
mlorModel = mlor.fit(mdf)
print(mlorModel.coefficientMatrix)
DenseMatrix([[-2.3 ],
[ 0.2 ],
[ 2.1 ]])
mlorModel.interceptVector
DenseVector([2.0 , 0.8 , -2.8 ])
test0 = sc.parallelize([Row(features=Vectors.dense(-1.0))]).toDF()
result = blorModel.transform(test0).head()
result.prediction
0.0
result.probability
DenseVector([0.99 , 0.00 ])
result.rawPrediction
DenseVector([8.22 , -8.22 ])
test1 = sc.parallelize([Row(features=Vectors.sparse(, [], [1.0]))]).toDF()
blorModel.transform(test1).head().prediction
1.0
blor.setParams("vector")
Traceback (most recent call last): TypeError: Method setParams forces keyword arguments.
lr_path = temp_path + "/lr"
blor.save(lr_path)
lr2 = LogisticRegression.load(lr_path)
lr2.getMaxIter() model_path = temp_path + "/lr_model"
blorModel.save(model_path)
model2 = LogisticRegressionModel.load(model_path)
blorModel.coefficients[] == model2.coefficients[]
True
blorModel.intercept == model2.intercept
True

Spark中ml和mllib的区别的更多相关文章

  1. spark:ML和MLlib的区别

    ML和MLlib的区别如下: ML是升级版的MLlib,最新的Spark版本优先支持ML. ML支持DataFrame数据结构和Pipelines,而MLlib仅支持RDD数据结构. ML明确区分了分 ...

  2. Spark机器学习中ml和mllib中矩阵、向量

    1:Spark ML与Spark MLLIB区别? Spark MLlib是面向RDD数据抽象的编程工具类库,现在已经逐渐不再被Spark团队支持,逐渐转向Spark ML库,Spark ML是面向D ...

  3. spark的ML和MLLib两个包区别和联系?

    原文链接:https://www.zhihu.com/question/35225203/answer/123986969 1. 技术角度上,面向的数据集类型不一样:ML的API是面向Dataset的 ...

  4. spark中map与flatMap的区别

    作为spark初学者对,一直对map与flatMap两个函数比较难以理解,这几天看了和写了不少例子,终于把它们搞清楚了 两者的区别主要在于action后得到的值 例子: import org.apac ...

  5. Spark中cache和persist的区别

    cache和persist都是用于将一个RDD进行缓存的,这样在之后使用的过程中就不需要重新计算了,可以大大节省程序运行时间. cache和persist的区别 基于Spark 1.6.1 的源码,可 ...

  6. Spark中repartition和partitionBy的区别

    repartition 和 partitionBy 都是对数据进行重新分区,默认都是使用 HashPartitioner,区别在于partitionBy 只能用于 PairRDD,但是当它们同时都用于 ...

  7. Spark中groupBy groupByKey reduceByKey的区别

    groupBy 和SQL中groupby一样,只是后面必须结合聚合函数使用才可以. 例如: hour.filter($"version".isin(version: _*)).gr ...

  8. spark中map与mapPartitions区别

    在spark中,map与mapPartitions两个函数都是比较常用,这里使用代码来解释一下两者区别 import org.apache.spark.{SparkConf, SparkContext ...

  9. 大数据学习day19-----spark02-------0 零碎知识点(分区,分区和分区器的区别) 1. RDD的使用(RDD的概念,特点,创建rdd的方式以及常见rdd的算子) 2.Spark中的一些重要概念

    0. 零碎概念 (1) 这个有点疑惑,有可能是错误的. (2) 此处就算地址写错了也不会报错,因为此操作只是读取数据的操作(元数据),表示从此地址读取数据但并没有进行读取数据的操作 (3)分区(有时间 ...

随机推荐

  1. NopCommerce的定时任务分析和应用

    NOP的定时任务也是群里听群友听说,我很少在WEB端做定时任务,所以尝鲜下,看看效果怎么样. 主要涉及到下面几个类和配置文件配置: web.config <configSections> ...

  2. unity 打包apk安装失败

    Unity 打包Apk安装失败,错误提示:安卓未完成. 解决方案:检查BundleID是否一致

  3. oracle数据备份

    from:http://www.docin.com/p-728428621.html

  4. MyException--org.apache.ibatis.exceptions.PersistenceException: ### Error building SqlSession. ###

    org.apache.ibatis.exceptions.PersistenceException:  ### Error building SqlSession. ### The error may ...

  5. python2.0 s12 day8 _ socketserver学习

    Socket 概念 一个socket就是一个点对点的链接.当今,大多数的通信都是基于Internet Protocl,因此大多数的网络Socket都是Internet Protocl(互联网)的通信( ...

  6. ionic跳转(一)

    在ionic中可以用两个办法写中转链接(写模版地址或路由地址) 1)a 标签的 href <a class="button button-icon icon ion-android-h ...

  7. 经典 MapReduce框架(MRv1)

    在 MapReduce 框架中,作业执行受两种类型的进程控制: 一个称为 JobTracker 的主要进程,它协调在集群上运行的所有作业,分配要在 TaskTracker 上运行的 map 和 red ...

  8. C语言的基本构成

    C语言的基本构成 知识点:C语言的注释:关键字:书写风格:常量和变量 重要程度:★★★★ 1.C语言的良好风格 用C语言编写的程序,称为C语言源程序,简称C程序. 本节将通过一个简单的C程序例子,向大 ...

  9. WPS之替换样式

    以前写文档需要颜色设置什么的时候,都是遇到的时候,就进行设置,挺烦的,要一直切换. 今天突然想到,既然有替换应该可能也有样式替换,就查了一下,试了试果然可以,以后就这么干了

  10. 【ecshop】使用sql 清除测试数据

    操作方式:后台->数据库->sql查询   输入以下你想进行的操作 -- -- 清空会员有关数据: -- TRUNCATE TABLE `ecs_users` ; TRUNCATE TAB ...