---
title: SVD
description: "SVD分为RowMatrix SVD API和IndexedRowMatrix SVD API两大类模型接口。"
url: https://www.hikunpeng.com/document/detail/zh/kunpengboostkithistory/2200/bds/kunpengbdssparkml_16_0024.html
sourcePath: /source/zh/kunpengboostkithistory/2200/bds/kunpengbdssparkml_16_0024.html
indexId: 5cd90fc00f7cde6915ea5109dfeb72d4177be95b8147921accf56807cebb72a073
---
# SVD

SVD分为RowMatrix SVD API和IndexedRowMatrix SVD API两大类模型接口。

| 模型接口类别 | 函数接口 |
| --- | --- |
| MLlib RowMatrix API | def computeSVD( k: Int, computeU: Boolean = false, rCond: Double = 1e\-9): SingularValueDecomposition[RowMatrix,Matrix] |
| MLlib IndexedRowMatrix API | def computeSVD( k: Int, computeU: Boolean = false, rCond: Double = 1e\-9):SingularValueDecomposition[IndexedRowMatrix, Matrix] |


#### MLlib RowMatrix API

- 功能描述
  输入RDD[Vector]形式的矩阵，输出它的奇异值分解结果。


- 输入输出

  1. 包名：package org.apache.spark.mllib.linalg.distributed
  2. 类名：RowMatrix
  3. 方法名：computeSVD
  4. 输入：RowMatrix，输入矩阵| 参数名称 | 取值类型 | 描述 |
| --- | --- | --- |
| rows | RDD[Vector] | 矩阵，以行为单位进行存储 |
| nRows | Long | 行数 |
| nCols | Int | 列数 |


  5. 算法参数| 参数名称 | 取值类型 | 缺省值 | 描述 |
| --- | --- | --- | --- |
| k | Int | \- | 要求的奇异值个数，取值范围[1,n] |
| computeU | Boolean | false | 是否计算左奇异矩阵 |
| rCond | Double | 1e\-9 | 矩阵条件数倒数，超过rCond\*s[0]的值被当做0 |


    参数及RowMatrix代码接口示例： 1 2 3 4 val matrix = new RowMatrix(trainingData, params.numRows, params.numCols) //行矩阵实例 // 调用行矩阵的computeSVD接口 val svd = matrix.computeSVD(params.k, computeU = true)

  6. 输出：SVD的分解结果SingularValueDecomposition[RowMatrix, Matrix]，SingularValueDecomposition是一个case class，包含三个变量U、s、V| 参数名称 | 取值类型 | 描述 |
| --- | --- | --- |
| U | RowMatrix | 左奇异矩阵，矩阵大小是m×k。 |
| s | Vector | 奇异值向量，向量长度为k。 |
| V | Matrix | 右奇异矩阵，矩阵大小是n×k。 |


- 使用样例
  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 import org.apache.spark.mllib.linalg.Matrix import org.apache.spark.mllib.linalg.SingularValueDecomposition import org.apache.spark.mllib.linalg.Vector import org.apache.spark.mllib.linalg.Vectors import org.apache.spark.mllib.linalg.distributed.RowMatrix val data = Array( Vectors.sparse(5, Seq((1, 1.0), (3, 7.0))), Vectors.dense(2.0, 0.0, 3.0, 4.0, 5.0), Vectors.dense(4.0, 0.0, 0.0, 6.0, 7.0)) val rows = sc.parallelize(data) val mat: RowMatrix = new RowMatrix(rows) // Compute the top 5 singular values and corresponding singular vectors. val svd: SingularValueDecomposition[RowMatrix, Matrix] = mat.computeSVD(5, computeU = true) val U: RowMatrix = svd.U // The U factor is a RowMatrix. val s: Vector = svd.s // The singular values are stored in a local dense vector. val V: Matrix = svd.V // The V factor is a local dense matrix.


#### MLlib IndexedRowMatrix API

- 功能描述
  输入RDD[Vector]形式的矩阵，输出它的奇异值分解结果。


- 输入输出

  1. 包名：package org.apache.spark.mllib.linalg.distributed
  2. 类名：IndexedRowMatrix
  3. 方法名：computeSVD
  4. 输入：RowMatrix，输入矩阵| 参数名称 | 取值类型 | 描述 |
| --- | --- | --- |
| rows | RDD[IndexedRow] | 矩阵，以行为单位进行存储。 IndexedRow(index: Long, vector: Vector) |
| nRows | Long | 行数。 |
| nCols | Int | 列数。 |


  5. 算法参数| 参数名称 | 取值类型 | 缺省值 | 描述 |
| --- | --- | --- | --- |
| k | Int | \- | 要求的奇异值个数，取值范围[1,n]。 |
| computeU | Boolean | false | 是否计算左奇异矩阵。 |
| rCond | Double | 1e\-9 | 矩阵条件数倒数，超过rCond\*s[0]的值被当做0。 |


    参数及IndexedRowMatrix代码接口示例： 1 2 3 4 val indexedMatrix = new IndexedRowMatrix(trainingData, params.numRows, params.numCols) //索引行矩阵实例 // 调用索引行矩阵的computeSVD接口 val svd = indexedMatrix.computeSVD(params.k, computeU = true)

  6. 输出：SVD的分解结果SingularValueDecomposition[IndexedRowMatrix, Matrix]| 参数名称 | 取值类型 | 描述 |
| --- | --- | --- |
| U | IndexedRowMatrix | 左奇异矩阵，矩阵大小是m×k |
| s | Vector | 奇异值向量，向量长度为k |
| V | Matrix | 右奇异矩阵，矩阵大小是n×k |


- 使用样例
  1 val svdRes = distMatrix.computeSVD(k)
