本文整理汇总了Scala中org.apache.spark.mllib.optimization.NNLS类的典型用法代码示例。如果您正苦于以下问题:Scala NNLS类的具体用法?Scala NNLS怎么用?Scala NNLS使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了NNLS类的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Scala代码示例。
示例1: xray
//设置package包名称以及导入依赖的类
package org.apache.spark.mllib
import breeze.linalg.{DenseMatrix => BDM, DenseVector => BDV, diag, norm}
import org.apache.spark.mllib.optimization.NNLS
import math.sqrt
object xray {
def findWeights(X : BDM[Double], XA : BDM[Double]) : BDM[Double] = {
val H = BDM.zeros[Double](XA.cols, X.cols)
val ws = NNLS.createWorkspace(XA.cols)
val ata = (XA.t*XA).toArray
for(colidx <- 0 until X.cols) {
val atb = (XA.t*X(::,colidx)).toArray
val h = NNLS.solve(ata, atb, ws)
H(::, colidx) := BDV(h)
}
H
}
def computeXray( X : BDM[Double], r : Int) : (Array[Int], BDM[Double]) = {
val C = X.t*X
val selColIndices = Array.fill[Int](r)(-1)
def posPartNorm(x : BDV[Double]) : Double = {
norm(x.map( x => if (x >= 0) x else 0 ))
}
// select the first column
val objVals = (0 until X.cols).map( j => posPartNorm(C(::, j)) / sqrt(C(j, j)) ).toList
selColIndices(0) = objVals.indexOf(objVals.max)
var curXA = X(::, selColIndices(0)).asDenseMatrix.t
var curH = findWeights(X, curXA)
// select remaining columns
for (curiter <- 1 until r) {
val potentialCols = (0 until X.cols).toArray.filter( ! (x => selColIndices contains x)(_) )
val objVals = potentialCols.map( j => posPartNorm( C(::, j) - curH.t*(curXA.t*X(::, j)) ) / sqrt(C(j, j)) )
selColIndices(curiter) = potentialCols(objVals.zipWithIndex.sortBy(_._1).last._2)
curXA = BDM.zeros[Double](X.rows, curiter + 1)
for(colidx <- 0 to curiter) {
curXA(::, colidx) := X(::, selColIndices(colidx))
}
curH = findWeights(X, curXA)
}
return (selColIndices, curH)
}
}
开发者ID:alexgittens,项目名称:SparkAndMPIFactorizations,代码行数:54,代码来源:xray.scala
注:本文中的org.apache.spark.mllib.optimization.NNLS类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论