机器学习库MLlib、SparseVector、dense vector、labelpoint
- 机器学习库MLlib、SparseVector、dense vector、labelpoint
- 什么是“ Spark ML”?
- 功能简介
- 版本差异说明:
- *为什么MLlib切换到基于DataFrame的API?*
- 数据类型 - RDD-based API
- 局部向量
- scala:
- python:
- 记录label数据,Labeled point
- libsvm格式
机器学习库MLlib、SparseVector、dense vector、labelpoint
author:
注意:
spark.mllib
和spark.ml
是两个东西。
-
spark.ml
是基于dataframe的。 -
spark.mllib
是基于RDD的。
什么是“ Spark ML”?
- “ Spark ML”不是正式名称,但有时用于指代基于MLlib DataFrame的API。这主要归因于
org.apache.spark.ml
基于DataFrame的API使用的Scala软件包名称,以及我们最初用来强调管道概念的“ Spark ML Pipelines”一词。
功能简介
MLlib是Spark的机器学习(ML)库。它的目标是使实用的机器学习可扩展且容易。在较高级别,它提供了以下工具:
- ML算法:常见的学习算法,例如分类,回归,聚类和协作过滤
- 特征化:特征提取,变换,降维和选择
- 管道:用于构建,评估和调整ML管道的工具
- 持久性:保存和加载算法,模型和管道
- 实用程序:线性代数,统计信息,数据处理等。
版本差异说明:
- MLlib仍将
spark.mllib
通过错误修复支持基于RDD的API 。 - MLlib不会向基于RDD的API添加新功能。
- 在Spark 2.x发行版中,MLlib将功能添加到基于DataFrames的API中,以与基于RDD的API达到功能奇偶性。
- 达到功能奇偶性(大致针对Spark 2.3估算)后,将不推荐使用基于RDD的API。
- 预计将在Spark 3.0中删除基于RDD的API。
为什么MLlib切换到基于DataFrame的API?
- 与RDD相比,DataFrames提供了更加用户友好的API。DataFrames的许多好处包括Spark数据源,SQL / DataFrame查询,Tungsten和Catalyst优化以及跨语言的统一API。
- 用于MLlib的基于DataFrame的API为ML算法和多种语言提供了统一的API。
- DataFrame有助于实际的ML管道,特别是功能转换。有关详细信息,请参见管道指南。
数据类型 - RDD-based API
MLlib supports local vectors and matrices stored on a single machine, as well as distributed matrices backed by one or more RDDs. Local vectors and local matrices are simple data models that serve as public interfaces.
局部向量
- MLlib 支持两种 local vectors: dense and sparse;
- 密集向量由表示其输入值的双精度数组支持,而稀疏向量由两个并行数组支持:索引和值。
- 例如,向量
(1.0, 0.0, 3.0)
可以以密格式表示为[1.0, 0.0, 3.0]
或以稀疏格式表示为(3, [0, 2], [1.0, 3.0])
,其中3
是向量的大小。
scala:
局部向量的基类是 Vector,并且我们提供了两种实现:DenseVector和 SparseVector。我们建议使用中实现的工厂方法 Vectors来创建局部向量。
import org.apache.spark.mllib.linalg.{Vector, Vectors}
// Create a dense vector (1.0, 0.0, 3.0).
val dv: Vector = Vectors.dense(1.0, 0.0, 3.0)
// Create a sparse vector (1.0, 0.0, 3.0) by specifying its indices and values corresponding to nonzero entries.
val sv1: Vector = Vectors.sparse(3, Array(0, 2), Array(1.0, 3.0))
// Create a sparse vector (1.0, 0.0, 3.0) by specifying its nonzero entries.
val sv2: Vector = Vectors.sparse(3, Seq((0, 1.0), (2, 3.0)))
python:
- python里面可以直接用array,或者list表示dense vector。
import numpy as np
import scipy.sparse as sps
from pyspark.mllib.linalg import Vectors
# Use a NumPy array as a dense vector.
dv1 = np.array([1.0, 0.0, 3.0])
# Use a Python list as a dense vector.
dv2 = [1.0, 0.0, 3.0]
# Create a SparseVector.
sv1 = Vectors.sparse(3, [0, 2], [1.0, 3.0])
# Use a single-column SciPy csc_matrix as a sparse vector.
sv2 = sps.csc_matrix((np.array([1.0, 3.0]), np.array([0, 2]), np.array([0, 2])), shape=(3, 1))
记录label数据,Labeled point
其实就是label的稀疏表示方法。
scala
import org.apache.spark.mllib.linalg.Vectors
import org.apache.spark.mllib.regression.LabeledPoint
// Create a labeled point with a positive label and a dense feature vector.
val pos = LabeledPoint(1.0, Vectors.dense(1.0, 0.0, 3.0))
// Create a labeled point with a negative label and a sparse feature vector.
val neg = LabeledPoint(0.0, Vectors.sparse(3, Array(0, 2), Array(1.0, 3.0)))
python
from pyspark.mllib.linalg import SparseVector
from pyspark.mllib.regression import LabeledPoint
# Create a labeled point with a positive label and a dense feature vector.
pos = LabeledPoint(1.0, [1.0, 0.0, 3.0]) # 正样本,python里面可以直接用list表示dense vector
# Create a labeled point with a negative label and a sparse feature vector.
neg = LabeledPoint(0.0, SparseVector(3, [0, 2], [1.0, 3.0])) # 负样本,
neg = LabeledPoint(0.0, SparseVector(3, [0, 2], [1.0, 3.0]))
,这个代码可以把sparsevector变成densevector理解。就变成了neg = LabeledPoint(0.0, [1.0, 0.0, 3.0]))
。
libsvm格式
label index1:value1 index2:value2 ...