在使用spark MLlib時(shí),有時(shí)候需要使用到一些基礎(chǔ)的矩陣(向量),例如:全零矩陣,全一矩陣;以及矩陣之間的運(yùn)算操作。這里整理了一些常用的矩陣操作方法:
成都創(chuàng)新互聯(lián)是一家專注于網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè)與策劃設(shè)計(jì),烏達(dá)網(wǎng)站建設(shè)哪家好?成都創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設(shè)10余年,網(wǎng)設(shè)計(jì)領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:烏達(dá)等地區(qū)。烏達(dá)做網(wǎng)站價(jià)格咨詢:13518219792
矩陣:
package utils
import java.util.Random
/**
* 密集矩陣,用于封裝模型參數(shù)
*/
class DenseMatrix(rowNum: Int, columnNum: Int) extends Serializable{
var matrix = Array.ofDim[Double](rowNum, columnNum)
def rows(): Int = {
rowNum
}
def columns(): Int = {
columnNum
}
def apply(i: Int): Array[Double] = {
matrix(i)
}
/**
* 構(gòu)造0矩陣
*/
def zeros(): DenseMatrix = {
for (i <- 0 until rowNum) {
for (j <- 0 until columnNum) {
matrix(i)(j) = 0
}
}
this
}
/**
* 隨機(jī)初始化矩陣的值
*/
def rand(): DenseMatrix = {
val rand = new Random(42)
for (i <- 0 until rowNum) {
for (j <- 0 until columnNum) {
matrix(i)(j) = rand.nextDouble
}
}
this
}
def set(i: Int, j: Int, value: Double) {
matrix(i)(j) = value
}
def get(i: Int, j: Int): Double = {
matrix(i)(j)
}
def +(scalar: Double): DenseMatrix = {
for (i <- 0 until rowNum) yield {
for (j <- 0 until columnNum) yield {
matrix(i)(j) += scalar
}
}
this
}
def -(scalar: Double): DenseMatrix = {
this - scalar
}
def +(other: DenseMatrix): DenseMatrix = {
for (i <- 0 until rowNum) yield {
for (j <- 0 until columnNum) yield {
matrix(i)(j) += other(i)(j)
}
}
this
}
def -(other: DenseMatrix): DenseMatrix = {
this + (other * (-1))
}
def *(scalar: Double): DenseMatrix = {
for (i <- 0 until rowNum) yield {
for (j <- 0 until columnNum) yield {
matrix(i)(j) *= scalar
}
}
this
}
}
object DenseMatrix {
def main(args: Array[String]): Unit = {}
}
向量:
package utils
import scala.collection.mutable.HashMap
import org.apache.spark.util.Vector
/**
* 定義一個(gè)基于HashMap的稀疏向量
*/
class SparserVector(dimNum: Int) {
var elements = new HashMap[Int, Double]
def insert(index: Int, value: Double) {
elements += index -> value;
}
def *(scale: Double): Vector = {
var x = new Array[Double](dimNum)
elements.keySet.foreach(k => x(k) = scale * elements.get(k).get);
Vector(x)
}
}
object SparserVector {
def main(args: Array[String]): Unit = {}
}