- Machine Learning with Spark(Second Edition)
- Rajdeep Dua Manpreet Singh Ghotra Nick Pentreath
- 447字
- 2021-07-09 21:07:49
Matrix operations
There are different kinds of operations which can be performed on matrices.
- Elementwise addition: Given two matrices, a and b, addition of the two (a + b) means adding each element of two matrices.
Breeze
val a = DenseMatrix((1,2),(3,4))
val b = DenseMatrix((2,2),(2,2))
val c = a + b
println("a: n" + a)
println("b: n" + b)
println("a + b : n" + c)
The output of the last code is given as follows:
a:
1 2
3 4
b:
2 2
2 2
a + b :
3 4
5 6
- Elementwise multiplication: In this operation, each element of the matrix a is multiplied by matrix
Breeze
a :* b
val d = a*b
println("Dot product a*b : n" + d)
The output of the preceding code is given as follows:
Dot product a*b :
6 6
14 14
- Elementwise comparison: In this operation, each element of a is compared with b. The code in Breeze is given as follows:
Breeze
a :< b
The output for the preceding code is give as follows:
a :< b
false false
false false
- Inplace addition: This implies adding each element of a by 1.
Breeze
The following is the output of the preceding code:
Inplace Addition : a :+= 1
2 3
4 5
value = a :+= 1
println("Inplace Addition : a :+= 1n" + e)
- Element-wise sum: This is used to add all the elements of a matrix. The code in Breeze is given as follows:
Breeze
val sumA = sum(a)
println("sum(a):n" + sumA)
The following is the output for the preceding code:
sum(a):
14
- Element-wise max: To find the maximum value out of all the elements in a matrix, we use
a.max
Breeze
The code in Breeze can be written as follows:
println("a.max:n" + a.max)
- Element-wise argmax: This is used to get the position of the element with the maximum value.
Breeze
Code:
println("argmax(a):n" + argmax(a))
The output of the preceding command is as follows:
argmax(a):
(1,1)
- Ceiling: This rounds off each element of the matrix to the next integer.
Breeze
Code:
val g = DenseMatrix((1.1, 1.2), (3.9, 3.5))
println("g: n" + g)
val gCeil =ceil(g)
println("ceil(g)n " + gCeil)
The output of the preceding code is as follows:
g:
1.1 1.2
3.9 3.5
ceil(g)
2.0 2.0
4.0 4.0
- Floor: Floor rounds off the value of each element to the nearest integer of a lower value.
Breeze
Code:
val gFloor =floor(g)
println("floor(g)n" + gFloor)
The output will be as follows:
floor(g)
1.0 1.0
3.0 3.0
推薦閱讀
- 高效能辦公必修課:Word圖文處理
- Mastering Spark for Data Science
- PowerShell 3.0 Advanced Administration Handbook
- 網絡服務器架設(Windows Server+Linux Server)
- Hands-On Machine Learning on Google Cloud Platform
- 控制與決策系統仿真
- 3D Printing for Architects with MakerBot
- 觸控顯示技術
- Visual FoxPro程序設計
- INSTANT Heat Maps in R:How-to
- INSTANT Puppet 3 Starter
- The DevOps 2.1 Toolkit:Docker Swarm
- 生物3D打印:從醫療輔具制造到細胞打印
- 簡明學中文版Photoshop
- 教育創新與創新人才:信息技術人才培養改革之路(四)