1. 程式人生 > >spark | scala | 線性代數庫Breeze學習

spark | scala | 線性代數庫Breeze學習

最近在學習spark,對線性代數庫Breeze做了學習,介紹下常用的函式

前提,使用Breeze庫,必須匯入相關的包

import breeze.linalg._
import breeze.numerics._

最基礎的操作:矩陣,向量,陣列的轉換

1、DenseMatrix.zerosDouble

全為零的n*m的矩陣,Double型別

2、DenseVector.zerosDouble

全為零的n個數組成的向量,Double型別

3、DenseVector.onesDouble

全為1的n個數組成的向量,Double型別

4、DenseVector.fill(n){5.0}

產生向量,長度為n,用5.0來填充

5、DenseVector.range(start,stop,step)
DenseVector.rangeD(start,stop,step)

產生序列向量
6、DenseVector.linspace(start,stop,numvals)

產生向量,有numvals個數的向量

7、DenseMatrix.eyeDouble

產生n*n的矩陣,對角為1,Double型別

8、diag(DenseVector(1.0,2.0,3.0))

產生主對角元素為1.0,2.0,3.0的矩陣

9、DenseMatrix((1.0,2.0),(3.0,4.0))

產生矩陣

10、DenseVector(1,2,3,4)

產生向量

11、DenseVector(1,2,3,4).t

向量轉置

12、DenseVector.tabulate(3){i => 2*i}

scala> DenseVector.tabulate(3){i => 2*i}

res33: breeze.linalg.DenseVector[Int] = DenseVector(0, 2, 4)

結果為:0,2,4

13、DenseMatrix.tabulate(3,2){case(i,j) => i+j}

scala> DenseMatrix.tabulate(3
,2)
{case(i,j) =>
i+j} res34: breeze.linalg.DenseMatrix[Int] = 0 1 1 2 2 3

行列數相加

14、new DenseVector(Array(1,2,3,4))
從陣列建立向量

scala> new DenseVector(Array(1,2,3,4))
res35: breeze.linalg.DenseVector[Int] = DenseVector(1, 2, 3, 4)

15、new DenseMatrix(2,3,Array(11,12,13,21,22,23))
從陣列建立矩陣

scala> new DenseMatrix(2,3,Array(11,12,13,21,22,23))
res36: breeze.linalg.DenseMatrix[Int] =
11  13  22
12  21  23

16、DenseVector.rand(4)
得到0到1的隨機向量,長度為4

scala> DenseVector.rand(4)
res37: breeze.linalg.DenseVector[Double] = DenseVector(0.9838289972536518, 0.798555117073358, 0.30308183931925403, 0.7958095551517774)

17、DenseMatrix.rand(2,3)
得到0到1的隨機矩陣

scala> DenseMatrix.rand(2,3)
res38: breeze.linalg.DenseMatrix[Double] =
0.3891370890132193  0.06732600444704517  0.2136759825764527
0.587145241786718   0.8670050354290917   0.5494899108312414

Breeze元素訪問

1、指定位置

scala> val a = DenseVector(1,2,3,4,5)
a: breeze.linalg.DenseVector[Int] = DenseVector(1, 2, 3, 4, 5)

scala> a(2)
res44: Int = 3

2、向量子集

scala> a(1 to 2)
res40: breeze.linalg.DenseVector[Int] = DenseVector(2, 3)

scala> a(1 until 2)
res41: breeze.linalg.DenseVector[Int] = DenseVector(2)

scala> a.slice(1,2)
res42: breeze.linalg.DenseVector[Int] = DenseVector(2)

3、按照指定步長取子集

scala> a(3 to 1 by -1)
res45: breeze.linalg.DenseVector[Int] = DenseVector(4, 3, 2)

4、指定開始位置至結尾

scala> a(2 to -1 )
res48: breeze.linalg.DenseVector[Int] = DenseVector(3, 4, 5)

5、最後一個元素

scala> a(2 to -1 )
res48: breeze.linalg.DenseVector[Int] = DenseVector(3, 4, 5)

6、矩陣指定列

scala> val a = DenseMatrix((1,2,3,4,5),(3,4,5,6,7),(5,6,7,8,9))
a: breeze.linalg.DenseMatrix[Int] =
1  2  3  4  5
3  4  5  6  7
5  6  7  8  9

scala> a(::,2)
res1: breeze.linalg.DenseVector[Int] = DenseVector(3, 5, 7)

Breeze元素操作

1、a.reshape(3,2)
調整矩陣形狀

scala> val a = DenseMatrix((2,3),(3,4),(6,7))
a: breeze.linalg.DenseMatrix[Int] =
2  3
3  4
6  7


scala> val a = DenseMatrix((2,3),(3,4),(6,7))
a: breeze.linalg.DenseMatrix[Int] =
2  3
3  4
6  7

2、a.toDenseVector
矩陣轉成向量

scala> val a = DenseMatrix((2,3),(3,4),(6,7))
a: breeze.linalg.DenseMatrix[Int] =
2  3
3  4
6  7

scala> a.toDenseVector
res3: breeze.linalg.DenseVector[Int] = DenseVector(2, 3, 6, 3, 4, 7)

3、lowerTriangular
下三角矩陣

scala> val b = DenseMatrix((1,2,3,4,5,6),(2,3,4,5,6,7),(3,4,5,6,7,8))
b: breeze.linalg.DenseMatrix[Int] =
1  2  3  4  5  6
2  3  4  5  6  7
3  4  5  6  7  8

scala> lowerTriangular(b)
res7: breeze.linalg.DenseMatrix[Int] =
1  0  0
2  3  0
3  4  5

4、upperTriangular
上三角矩陣

scala> upperTriangular(b)
res8: breeze.linalg.DenseMatrix[Int] =
1  2  3
0  3  4
0  0  5

5、b.copy
複製矩陣

scala> b.copy
res9: breeze.linalg.DenseMatrix[Int] =
1  2  3  4  5  6
2  3  4  5  6  7
3  4  5  6  7  8

6、diag(a)
取對角線元素

scala> val c = DenseMatrix((1,2,3),(2,3,4),(3,4,5))
c: breeze.linalg.DenseMatrix[Int] =
1  2  3
2  3  4
3  4  5

scala> diag(c)
res12: breeze.linalg.DenseVector[Int] = DenseVector(1, 3, 5)

7、c(1 to 4 ) := 5.0
子集賦值,將c中的第2個數到第五個數賦值為5.0

scala> val d = DenseVector(1,2,3,4,5,6,7,8,8)
d: breeze.linalg.DenseVector[Int] = DenseVector(1, 2, 3, 4, 5, 6, 7, 8, 8)

scala> d(1 to 4) :=  5
res18: breeze.linalg.DenseVector[Int] = DenseVector(5, 5, 5, 5)

scala> d
res21: breeze.linalg.DenseVector[Int] = DenseVector(1, 5, 5, 5, 5, 6, 7, 8, 8)

8、d(1 to 4) := DenseVector(1,2,3)
子集賦向量

scala> d(1 to 4):=DenseVector(1,2,3,4)
res26: breeze.linalg.DenseVector[Int] = DenseVector(1, 2, 3, 4)

scala> d
res27: breeze.linalg.DenseVector[Int] = DenseVector(1, 1, 2, 3, 4, 6, 7, 8, 8)

9、a(1 to 3,1 to 3) :=5
矩陣賦值

scala> b(1 to 2,1 to 3):= 6
res30: breeze.linalg.DenseMatrix[Int] =
6  6  6
6  6  6

scala> b
res31: breeze.linalg.DenseMatrix[Int] =
1  2  3  4  5  6
2  6  6  6  6  7
3  6  6  6  7  8

10、a(::,2) := 5
矩陣列賦值

scala> b(::,2) := 7
res32: breeze.linalg.DenseVector[Int] = DenseVector(7, 7, 7)

scala> b
res33: breeze.linalg.DenseMatrix[Int] =
1  2  7  4  5  6
2  6  7  6  6  7
3  6  7  6  7  8

11、DenseMatrix.vertcat(a,b)
垂直合併

12、DenseMatrix.horzcat(d,e)
水平合併

13、DenseVector.vertvat(a,b)
向量連線

數值計算

圖

求和函式

這裡寫圖片描述

布林函式

這裡寫圖片描述

線性代數

這裡寫圖片描述

一些很基礎的函式,很實用。