本文整理汇总了Scala中org.apache.spark.mllib.linalg.DenseMatrix类的典型用法代码示例。如果您正苦于以下问题:Scala DenseMatrix类的具体用法?Scala DenseMatrix怎么用?Scala DenseMatrix使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了DenseMatrix类的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: SamplePCA
//设置package包名称以及导入依赖的类
package org.broadinstitute.hail.methods
import org.apache.spark.mllib.linalg.{Matrix, DenseMatrix}
import org.apache.spark.rdd.RDD
import org.broadinstitute.hail.variant.Variant
import org.broadinstitute.hail.variant.VariantDataset
class SamplePCA(k: Int, computeLoadings: Boolean, computeEigenvalues: Boolean) {
def name = "SamplePCA"
def apply(vds: VariantDataset): (Matrix, Option[RDD[(Variant, Array[Double])]], Option[Array[Double]]) = {
val (variants, mat) = ToStandardizedIndexedRowMatrix(vds)
val sc = vds.sparkContext
val variantsBc = sc.broadcast(variants)
val svd = mat.computeSVD(k, computeU = computeLoadings)
val scores =
svd.V.multiply(DenseMatrix.diag(svd.s))
val loadings =
if (computeLoadings)
Some(svd.U.rows.map(ir =>
(variantsBc.value(ir.index.toInt), ir.vector.toArray)))
else
None
val eigenvalues =
if (computeEigenvalues)
Some(svd.s.toArray.map(x => x * x))
else
None
(scores, loadings, eigenvalues)
}
}