Breeze是scala用的數(shù)值處理庫歹鱼,spark的底層的向量和矩陣是基于Breeze的泣栈。本文對(duì)矩陣的基本操作做簡(jiǎn)單介紹,代碼主要參考博文弥姻,根據(jù)自己的習(xí)慣做了一些修改南片。
import breeze.linalg._
import breeze.numerics._
import org.apache.log4j.{Level, Logger}
import org.apache.spark.{SparkConf, SparkContext}
object ArrayAndMetrics {
def main(args: Array[String]): Unit = {
val conf = new SparkConf().setAppName("Kmeans").setMaster("local")
val sc = new SparkContext(conf)
Logger.getRootLogger.setLevel(Level.WARN)
println("---------------------------Breeze 創(chuàng)建函數(shù)-------------------------------")
val m1 = DenseMatrix.zeros[Double](2,3) // 2行3列的0矩陣
val v1 = DenseVector.zeros[Double](3) // 長(zhǎng)度為3的0向量
val v2 = DenseVector.ones[Double](3) // 長(zhǎng)度為3的1向量
val v3 = DenseVector.fill(3)(5.0) // 創(chuàng)建指定元素的向量,長(zhǎng)度為3庭敦,元素為5
val v4 = DenseVector.range(1,10,2) // 根據(jù)范圍創(chuàng)建向量參數(shù)(start疼进,end,step)秧廉,output: 1 3 5 7 9
val m2 = DenseMatrix.eye[Double](3) // 創(chuàng)建對(duì)角線為1的矩陣颠悬,3行3列的單元矩陣
val m7 = diag(DenseVector(1.0,2.0,3.0)) // 創(chuàng)建指定對(duì)角線元素的矩陣,3行3列定血,對(duì)角元素分別為1 2 3
val m3 = DenseMatrix((1.0,2.0),(3.0,4.0)) // 根據(jù)向量創(chuàng)建矩陣赔癌,每個(gè)數(shù)組就是一行
val v8 = DenseVector(1,2,3,4) // 根據(jù)元素創(chuàng)建向量
// 轉(zhuǎn)置
val m8 = m3.t // output: ((1 3) (2 4))
println("m8: " + m8)
// 根據(jù)下標(biāo)創(chuàng)建向量和矩陣
val v10 = DenseVector.tabulate(3){i=>2*i} // output: 0 2 4
val m4 = DenseMatrix.tabulate(3,2){case(i,j) => i+j}
println("m4: " + m4)
// 根據(jù)數(shù)組創(chuàng)建向量和矩陣
val v11 = new DenseVector(Array(1,2,3,4)) // 結(jié)果同v8
val m5 = new DenseMatrix(2,3,Array(11,12,12,21,21,11)) // 2行3列,元素分別為Array的元素
// 創(chuàng)建隨機(jī)向量和矩陣
val v12 = DenseVector.rand(4)
val m6 = DenseMatrix.rand(2,3)
println("----------------------元素訪問------------------------")
val a = DenseVector(1,2,3,4,5,6,7,8,9) //元素訪問
val ao1 = a(0) //訪問指定的元素
val ao2 = a(1 to 4) //訪問子元素向量, output: 2 3 4 5
println("ao2: " + ao2)
val ao3 = a(5 to 1 by -1) //指定起始和終止位置和補(bǔ)償, output: DenseVector(6, 5, 4, 3, 2)
println("ao3: " + ao3)
val ao4 = a(1 to -1) // -1代表索引最后的元素澜沟,output: DenseVector(2, 3, 4, 5, 6, 7, 8, 9)
println("ao4: " + ao4)
val ao5 = a(-1) // 訪問最后元素, output: 9
val m = DenseMatrix((1.0,2.0,3.0),(4.0,5.0,6.0))
val mo1 = m(0,1) // 訪問0行1列的元素灾票,output:2
println("mo1: ", mo1)
val mo2 = m(::,1) // 訪問第1列元素,output: DenseVector(2.0, 5.0)
println("mo2: " + mo2)
val mo21 = m.cols // 取矩陣列數(shù)茫虽,output:3
val mo3 = m(1,::) // 訪問第1行刊苍,output: Transpose(DenseVector(4.0, 5.0, 6.0))
println("mo3: " + mo3)
val mo31 = m.rows // 取矩陣行數(shù),output:2
// 元素操作
val m_1 = DenseMatrix((1.0,2.0,3.0),(4.0,5.0,6.0))
val m_1o1 = m_1.reshape(3,2) // 變成3行2列的矩陣
val m_1o2 = m_1.toDenseVector // 轉(zhuǎn)換成向量, output: DenseVector(1.0, 4.0, 2.0, 5.0, 3.0, 6.0)
println("m_1o2: " + m_1o2)
val m_3 = DenseMatrix((1,2,3),(4,5,6),(7,8,9))
val m_3o1 = lowerTriangular(m_3)// 取下三角濒析,output:((1 0 0) (4 5 0) (7 8 9))
println("m_3o1: " + m_3o1)
val m_3o2 = upperTriangular(m_3)// 取上三角正什,output: ((1 2 3) (0 5 6) (0 0 9))
println("m_3o2: " + m_3o2)
val m_3o3 = m_3.copy // copy生成一個(gè)新的矩陣
val m_3o4 = diag(m_3) // 對(duì)角線生成一個(gè)向量,output: DenseVector(1, 5, 9)
println("m_3o4: " + m_3o4)
m_3(::,2) := 5 // 將第2列的元素全部改成5
println("m_3: " + m_3)
m_3(1 to 2,1 to 2) := 5
println("m_3: " + m_3)
// 矩陣的連接(向量的連接和矩陣類似)
val a1 = DenseMatrix((1,2,3),(4,5,6))
val a2 = DenseMatrix((1,1,1),(2,2,2))
val a12V = DenseMatrix.vertcat(a1,a2) // 豎直的連接, output: ((1 2 3) (4 5 6) (1 1 1) (2 2 2))
println("a12V: " + a12V)
val a12H = DenseMatrix.horzcat(a1,a2) // 水平連接号杏,output: ((1 2 3 1 1 1) (4 5 6 2 2 2))
println("a12H: " + a12H)
println("-------------------數(shù)值計(jì)算-----------------------")
val a_3 = DenseMatrix((1,2,3),(4,5,6))
val b_3 = DenseMatrix((1,1,1),(2,2,2))
val ab31 = a_3 + b_3 // 對(duì)應(yīng)元素相加
println("ab31: " + ab31)
val ab32 = a_3 :* b_3 // 對(duì)應(yīng)元素相乘
println("ab32: " + ab32)
val ab33 = a_3 :/ b_3 // 對(duì)應(yīng)元素取商婴氮,注意輸出為整數(shù)商
println("ab33: " + ab33)
val ab34 = a_3 :< b_3 // 對(duì)應(yīng)元素進(jìn)行判斷
println("ab34: " + ab34)
val ab35 = a_3 :== b_3 // 對(duì)應(yīng)元素是否相等
println("ab35: " + ab35)
a_3 :+= 1 // 所有元素+1,注意此時(shí)a_3已經(jīng)改變
println("a_3: " + a_3)
a_3 :*= 2 // 所有元素*2
println("a_3: " + a_3)
val aMax = max(a_3) // 求最大值盾致,output: 14
println("aMax: " + aMax)
val aMaxIndex = argmax(a_3) // 最大值位置的索引, output: (1, 2)
println("aMaxIndex: " + aMaxIndex)
val ab38 = DenseVector(1,2,3,4) dot DenseVector(1,1,1,1) // 內(nèi)積主经,output: 10
println("ab38: " + ab38)
//------------------求和函數(shù)-----------------
val a_4 = DenseMatrix((1,2,3),(4,5,6),(7,8,9))
val a41 = sum(a_4) // 所有元素求和,output: 45
val a42 = sum(a_4,Axis._0) // 每一列進(jìn)行求和, output: DenseVector(12, 15, 18)
val a43 = sum(a_4,Axis._1) // 每一行進(jìn)行求和, output: DenseVector(6, 15, 24)
val a44 = trace(a_4) // 對(duì)角線求和, output: 15
val a45 = accumulate(DenseVector(1,2,3,4)) // 分別求前1,2,3,4個(gè)元素的和庭惜,output: DenseVector(1, 3, 6, 10)
//------------------布爾函數(shù)--------------------
val a_5 = DenseVector(true,false,true)
val b_5 = DenseVector(false,true,true)
val ab51 = a_5 :& b_5 // 對(duì)應(yīng)元素“與”
val ab52 = a_5 :| b_5 // 對(duì)應(yīng)元素“或”
val ab53 = !a_5 // 取“非”
val a_5_2 = DenseVector(7, 8, 9)
val ab54 = any(a_5_2)//任意一個(gè)元素 > 0 即為true
val ab55 = all(a_5_2)//所有元素 > 0 則為true
}
}
輸出結(jié)果:
---------------------------Breeze 創(chuàng)建函數(shù)-------------------------------
m8: 1.0 3.0
2.0 4.0
m4: 0 1
1 2
2 3
----------------------元素訪問------------------------
ao2: DenseVector(2, 3, 4, 5)
ao3: DenseVector(6, 5, 4, 3, 2)
ao4: DenseVector(2, 3, 4, 5, 6, 7, 8, 9)
(mo1: ,2.0)
mo2: DenseVector(2.0, 5.0)
mo3: Transpose(DenseVector(4.0, 5.0, 6.0))
m_1o2: DenseVector(1.0, 4.0, 2.0, 5.0, 3.0, 6.0)
m_3o1: 1 0 0
4 5 0
7 8 9
m_3o2: 1 2 3
0 5 6
0 0 9
m_3o4: DenseVector(1, 5, 9)
m_3: 1 2 5
4 5 5
7 8 5
m_3: 1 2 5
4 5 5
7 5 5
a12V: 1 2 3
4 5 6
1 1 1
2 2 2
a12H: 1 2 3 1 1 1
4 5 6 2 2 2
-------------------數(shù)值計(jì)算-----------------------
ab31: 2 3 4
6 7 8
ab32: 1 2 3
8 10 12
ab33: 1 2 3
2 2 3
ab34: false false false
false false false
ab35: true false false
false false false
a_3: 2 3 4
5 6 7
a_3: 4 6 8
10 12 14
aMax: 14
aMaxIndex: (1,2)
ab38: 10