Machine Learning On Spark——基础数据结构(二)
生活随笔
收集整理的這篇文章主要介紹了
Machine Learning On Spark——基础数据结构(二)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
本節(jié)主要內(nèi)容
1. IndexedRowMatrix的使用
IndexedRowMatrix,顧名思義就是帶索引的RowMatrix,它采用case class IndexedRow(index: Long, vector: Vector)類來表示矩陣的一行,index表示的就是它的索引,vector表示其要存儲(chǔ)的內(nèi)容。其使用方式如下:
package cn.ml.datastructimport org.apache.spark.SparkConf import org.apache.spark.SparkContext import org.apache.spark.mllib.linalg.Vectors import org.apache.spark.mllib.linalg.distributed.RowMatrix import org.apache.spark.mllib.linalg.distributed.CoordinateMatrix import org.apache.spark.mllib.stat.MultivariateStatisticalSummary import org.apache.spark.mllib.linalg.Matrix import org.apache.spark.mllib.linalg.SingularValueDecomposition import org.apache.spark.mllib.linalg.Matrices import org.apache.spark.mllib.linalg.distributed.IndexedRow import org.apache.spark.mllib.linalg.distributed.IndexedRowMatrixobject IndexRowMatrixDemo extends App {val sparkConf = new SparkConf().setAppName("IndexRowMatrixDemo ").setMaster("spark://sparkmaster:7077") val sc = new SparkContext(sparkConf)//定義一個(gè)隱式轉(zhuǎn)換函數(shù)implicit def double2long(x:Double)=x.toLong//數(shù)據(jù)中的第一個(gè)元素為IndexedRow中的index,剩余的映射到vector//f.take(1)(0)獲取到第一個(gè)元素并自動(dòng)進(jìn)行隱式轉(zhuǎn)換,轉(zhuǎn)換成Long類型val rdd1= sc.parallelize(Array(Array(1.0,2.0,3.0,4.0),Array(2.0,3.0,4.0,5.0),Array(3.0,4.0,5.0,6.0))).map(f => IndexedRow(f.take(1)(0),Vectors.dense(f.drop(1))))val indexRowMatrix = new IndexedRowMatrix(rdd1)//計(jì)算拉姆矩陣var gramianMatrix:Matrix=indexRowMatrix.computeGramianMatrix()//轉(zhuǎn)換成行矩陣RowMatrixvar rowMatrix:RowMatrix=indexRowMatrix.toRowMatrix()//其它方法例如computeSVD計(jì)算奇異值、multiply矩陣相乘等操作,方法使用與RowMaxtrix相同}2. BlockMatrix的使用
分塊矩陣將一個(gè)矩陣分成若干塊,例如:
可以將其分成四塊
從而矩陣P有如下形式
更多分塊矩陣的相關(guān)內(nèi)容包括分塊矩陣的轉(zhuǎn)置、分塊矩陣的相乘操作可以參見https://en.wikipedia.org/wiki/Block_matrix
總結(jié)
以上是生活随笔為你收集整理的Machine Learning On Spark——基础数据结构(二)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Machine Learning On
- 下一篇: Machine Learning on