Spark机器学习MLlib系列1(for python)--数据类型,向量,分布式矩阵,API
Spark機(jī)器學(xué)習(xí)MLlib系列1(for python)--數(shù)據(jù)類(lèi)型,向量,分布式矩陣,API
關(guān)鍵詞:Local vector,Labeled point,Local matrix,Distributed matrix,RowMatrix,IndexedRowMatrix,CoordinateMatrix,BlockMatrix。
前言:MLlib支持本地向量和存儲(chǔ)在單機(jī)上的矩陣,當(dāng)然也支持被存儲(chǔ)為RDD的分布式矩陣。一個(gè)有監(jiān)督的機(jī)器學(xué)習(xí)的例子在MLlib里面叫做標(biāo)簽點(diǎn)。
1. 本地向量
一個(gè)本地向量是由整數(shù)類(lèi)型和從0開(kāi)始的索引存儲(chǔ)在單機(jī)上
。MLlib支持兩種本地向量,稠密向量和稀疏向量。稠密向量由一個(gè)浮點(diǎn)數(shù)組表示它的的所有值,而一個(gè)稀疏矩陣由兩個(gè)平行的數(shù)組組成,索引和值。舉個(gè)例子,一個(gè)向量,(1.0,0.0,3.0)能個(gè)用稠密表現(xiàn)為[1.0,0.0,3.0] 或者以稀疏的形式表現(xiàn)為(3,[0,2],[1.0,3.0]),3是這個(gè)向量的大小。(本人注解:3為長(zhǎng)度,即是元素個(gè)數(shù),[0,2]為索引,[1.0,3.0],為值)
1.1MLlib認(rèn)為如下數(shù)據(jù)類(lèi)型是稠密向量:
~NumPys array
~Python list
1.2MLlib認(rèn)為如下數(shù)據(jù)類(lèi)型是稀疏向量:
~MLlib’s SparseVector.
~SciPy’s csc_matrix with a single colum
為了效率,我們推薦使用Numpy arrays ,并使用工廠方法繼承Vectors 來(lái)創(chuàng)建稀疏矩陣。
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))- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
2.標(biāo)簽點(diǎn)
標(biāo)簽點(diǎn)可以是一個(gè)本地的向量,可以是稀疏的也可以是稠密的,總之他們是帶有標(biāo)簽的。在MLlib中,標(biāo)簽點(diǎn)用來(lái)進(jìn)行有監(jiān)督的學(xué)習(xí)算法。我們使用雙精度數(shù)來(lái)存儲(chǔ)一個(gè)標(biāo)簽,這樣我們既可以用標(biāo)簽點(diǎn)做分類(lèi),也可以用來(lái)做回歸了。對(duì)于二分法,一個(gè)標(biāo)簽應(yīng)該不是0就是1。對(duì)于多種分類(lèi),標(biāo)簽應(yīng)該是索引從0,1,2,3….
一個(gè)標(biāo)簽點(diǎn)用LabelPoint來(lái)表示。
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]) # 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]))- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
稀疏數(shù)據(jù)
在訓(xùn)練中,有一個(gè)稀疏訓(xùn)練數(shù)據(jù)是有一件很平常的事情。MLlib支持讀取一個(gè)以LIBSVM格式存儲(chǔ)訓(xùn)練例子。LIBSVM是LIBSVM和LIBLINEAR默認(rèn)的格式。這是一種每一行帶有一個(gè)標(biāo)簽的的稀疏向量格式如下:
- 1
索引從1開(kāi)始的升序排列的。當(dāng)讀取完成之后,這些特征索引轉(zhuǎn)化為從0開(kāi)始。
MLUtils.loadLibSVMFile 讀取存儲(chǔ)LIBSVM格式的訓(xùn)練模型
from pyspark.mllib.util import MLUtilsexamples = MLUtils.loadLibSVMFile(sc, "data/mllib/sample_libsvm_data.txt")- 1
- 2
- 3
3.本地矩陣
一個(gè)本地矩陣有整數(shù)型的行,和雙精度的列索引,并且存儲(chǔ)在單機(jī)上。MLlib支持將所有數(shù)據(jù)存儲(chǔ)在一個(gè)單獨(dú)的數(shù)組上并且以列為順序的稠密矩陣,也支持稀疏矩陣。舉個(gè)例子,比如像下面的稠密矩陣:
這個(gè)矩陣是一個(gè)存儲(chǔ)在一維數(shù)組 [1.0, 3.0, 5.0, 2.0, 4.0, 6.0]上的大小為(3,2)的矩陣。
本地矩陣的基礎(chǔ)類(lèi)是Matrix,我們提供兩個(gè)實(shí)現(xiàn)函數(shù):DenseMatrix和SparseMatrix。我們推薦Matrices里面的工廠實(shí)現(xiàn)方法來(lái)創(chuàng)造本地矩陣。記住,MLlib的本地矩陣是列為序存儲(chǔ)。
from pyspark.mllib.linalg import Matrix, Matrices# Create a dense matrix ((1.0, 2.0), (3.0, 4.0), (5.0, 6.0)) dm2 = Matrices.dense(3, 2, [1, 2, 3, 4, 5, 6]) # Create a sparse matrix ((9.0, 0.0), (0.0, 8.0), (0.0, 6.0)) sm = Matrices.sparse(3, 2, [0, 1, 3], [0, 2, 1], [9, 6, 8])- 1
- 2
- 3
- 4
- 5
- 6
- 7
4.分布式矩陣
一個(gè)分布式矩陣有l(wèi)ong類(lèi)型的行和列,還有double類(lèi)型的值,并且被分布式存儲(chǔ)在一個(gè)或者更多的RDDs中。選擇正確的格式來(lái)存儲(chǔ)巨大的分布式矩陣是很重要的事情。將一個(gè)分布式矩陣轉(zhuǎn)換可能需要一個(gè)全局的清洗,這是代價(jià)非常昂貴的。直到現(xiàn)在,四種類(lèi)型的分布式矩陣已經(jīng)實(shí)現(xiàn)了。
這四種中基礎(chǔ)類(lèi)型叫做 RowMatrix。這種 RowMatrix一種面向行的分布式矩陣,沒(méi)有所謂的行索引。比如:一個(gè)特征向量的集合。它依賴(lài)與RDD自己的行,并且RDD的每個(gè)行是一個(gè)本地向量。對(duì)于一個(gè)RowMatrix我們假設(shè)列的數(shù)量不是非常巨大,以便一個(gè)單獨(dú)的本地向量能夠合理正確的與驅(qū)動(dòng)交流并且能夠存儲(chǔ)操作在一個(gè)正在使用它的節(jié)點(diǎn)上。
IndexedRowMatrix與 RowMatrix除了能被用來(lái)區(qū)分行和執(zhí)行合并的行索引不同之外,其他都非常相似。CoordinateMatrix是一種以coordinate list (COO) 格式存儲(chǔ)在RDDs條目中的分布式矩陣。
BlockMatrix 是一種被RDDMatrixBlock支持的分布式矩陣,MatrixBlock是元祖(Int, Int, Matrix).
NOTE
潛在的分布式矩陣RDD必須是確定的,因?yàn)槲覀兙彺媪司仃嚨拇笮?#xff0c;一般來(lái)說(shuō)使用一個(gè)非確定性的RDD會(huì)導(dǎo)致錯(cuò)誤。
RowMatrix
RowMatrix是一個(gè)面向行的分布式矩陣,沒(méi)有所謂的行索引,可以使用RDD的行,這些行是本地向量。既然每個(gè)行都被本地向量表示,列的數(shù)目被整數(shù)范圍限制,但是列數(shù)目在實(shí)際情況應(yīng)該是比行小的多的。
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
IndexedRowMatrix
IndexedRowMatrix與 RowMatrix除了有意義的行索引外,其他都非常相似。它使用RDD索引行,以便每一行代表它的索引和本地向量。
一個(gè) indexedRowMatrix可以被 indexedRowMatrix 創(chuàng)造出來(lái),一個(gè) indexedRowMatrix以能夠被轉(zhuǎn)化為RowMatrix通過(guò)去掉行索引。
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
CoordinateMatrix
CoordinateMatrix是一個(gè)分布式矩陣,
并且由RDD的條目支持的。每一個(gè)條目就是一個(gè)元祖(i: Long, j: Long, value: Double),i是行索引,j 是列索引,value是條目值。CoordinateMatrix應(yīng)該僅僅使用當(dāng)矩陣規(guī)模特別大并且矩陣很稀疏的時(shí)候。
CoordinateMatrix 能夠被MatrixEntry條目創(chuàng)建, CoordinateMatrix能被轉(zhuǎn)化為 RowMatrix通過(guò)使用toRowMatrix,或者一個(gè)稀疏行IndexedRowMatrix通過(guò)使用 toIndexedRowMatrix.
from pyspark.mllib.linalg.distributed import CoordinateMatrix, MatrixEntry# Create an RDD of coordinate entries. # - This can be done explicitly with the MatrixEntry class: entries = sc.parallelize([MatrixEntry(0, 0, 1.2), MatrixEntry(1, 0, 2.1), MatrixEntry(6, 1, 3.7)]) # - or using (long, long, float) tuples: entries = sc.parallelize([(0, 0, 1.2), (1, 0, 2.1), (2, 1, 3.7)]) # Create an CoordinateMatrix from an RDD of MatrixEntries. mat = CoordinateMatrix(entries) # Get its size. m = mat.numRows() # 3 n = mat.numCols() # 2 # Get the entries as an RDD of MatrixEntries. entriesRDD = mat.entries # Convert to a RowMatrix. rowMat = mat.toRowMatrix() # Convert to an IndexedRowMatrix. indexedRowMat = mat.toIndexedRowMatrix() # Convert to a BlockMatrix. blockMat = mat.toBlockMatrix()- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
BlockMatrix
BlockMatrix是一個(gè)分布式矩陣,并且被 MatrixBlocks支持, MatrixBlocks是一個(gè)元祖, ((Int, Int), Matrix),(Int, Int)是塊索引,Matrix是rowsPerBlock x colsPerBlock的形狀。
from pyspark.mllib.linalg import Matrices from pyspark.mllib.linalg.distributed import BlockMatrix # Create an RDD of sub-matrix blocks. blocks = sc.parallelize([((0, 0), Matrices.dense(3, 2, [1, 2, 3, 4, 5, 6])), ((1, 0), Matrices.dense(3, 2, [7, 8, 9, 10, 11, 12]))]) # Create a BlockMatrix from an RDD of sub-matrix blocks. mat = BlockMatrix(blocks, 3, 2) # Get its size. m = mat.numRows() # 6 n = mat.numCols() # 2 # Get the blocks as an RDD of sub-matrix blocks. blocksRDD = mat.blocks # Convert to a LocalMatrix. localMat = mat.toLocalMatrix() # Convert to an IndexedRowMatrix. indexedRowMat = mat.toIndexedRowMatrix() # Convert to a CoordinateMatrix. coordinateMat = mat.toCoordinateMatrix()原文轉(zhuǎn)自 http://blog.csdn.net/qq_30115765/article/details/52594421
轉(zhuǎn)載于:https://www.cnblogs.com/honey01/p/8043201.html
總結(jié)
以上是生活随笔為你收集整理的Spark机器学习MLlib系列1(for python)--数据类型,向量,分布式矩阵,API的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 编译Python2.7.10
- 下一篇: windows 环境下python 安装