scala sparseVetor, SprseMatrix 实现
Posted 江枫1
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了scala sparseVetor, SprseMatrix 实现相关的知识,希望对你有一定的参考价值。
def rand(seed:Int):Double={
val rand=new Random(seed)
rand.nextDouble()
}
def rand2(size:Int,seed:Int):SparseVector={
val rand=new Random(seed)
val ret_indices=Array.range(0,size)
val ret_values=Array.tabulate[Double](size)(i=>rand.nextDouble()) //从函数创建数组
Vectors.sparse(size, ret_indices, ret_values).asInstanceOf[SparseVector]
}
调用:val w=rand2(4,3)
返回,SparseVector有3个要素函数,size,indices,values.
取某一部分就调用那3个函数。比如:w.size=4,w.indices=(1,2,3,4)
def rand3(numRows:Int,numCols:Int,seed:Int):SparseMatrix={
val rand=new Random(seed)
val ret_rowIndices=new ArrayBuffer[Int]()
for (i <-0 to numCols-1) {
ret_rowIndices ++= Array.range(0,numRows)
}
val ret_colPtrs=Array.range(0,numCols+1).map(x=>x * numRows)
val ret_values=Array.tabulate[Double](numRows*numCols)(i=>rand.nextDouble())
Matrices.sparse(numRows, numCols, ret_colPtrs.toArray,ret_rowIndices.toArray, ret_values.toArray).asInstanceOf[SparseMatrix]
}
以上是关于scala sparseVetor, SprseMatrix 实现的主要内容,如果未能解决你的问题,请参考以下文章