SparkMLlib中会经常对RDD用到统计方法,其用法如下
2.1 基础加载包
//向量
import org.apache.spark.mllib.linalg.Vector
//向量集
import org.apache.spark.mllib.linalg.Vectors
//稠密向量
import org.apache.spark.mllib.linalg.DenseVector
//实例
import org.apache.spark.mllib.stat.{MultivariateStatisticalSummary, Statistics}
//矩阵
import org.apache.spark.mllib.linalg.{Matrix, Matrices}
//索引矩阵
import org.apache.spark.mllib.linalg.distributed.RowMatrix
//RDD
import org.apache.spark.rdd.RDD
2.2 标准构造
//创建一个RDD[Vector]
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 => Vectors.dense(f))
val rows: RDD[Vector] = rdd1
//或者创建一个矩阵将这个矩阵转化
def toRDD(m: Matrix): RDD[Vector] = {
val columns = m.toArray.grouped(m.numRows)
val rows = columns.toSeq.transpose
val vectors = rows.map(row => new DenseVector(row.toArray))
sc.parallelize(vectors)
}
//将这个RDD转变为MultivariateStatisticalSummary