本系列”spark mllib机器学习”,均以最新spark2.3.0版本为蓝本进行编写,参考书籍<< spark mllib机器学习 >>黄美灵版,转载请注明出处
GitHub地址:https://github.com/future-fu/learnsparkmllib
目录
- Breeze介绍
- BLAS介绍
- Mllib向量
- Mllib矩阵
- MLlib分布式矩阵
明细
1.Breeze介绍
Breeze包括线性代数,数值计算及优化;在Spark Mllib 机器学习的底层采用Breeze库.
举几个Breeze的实例如下,更多细节可以参考书籍
package ai.lifecode.breeze
import breeze.linalg.{DenseMatrix, DenseVector, diag}
import org.apache.spark.sql.SparkSession
/**
* Created by future_fu on 2018/9/4 14:18.
*/
object BreezeDemo {
def main(args: Array[String]): Unit = {
val spark=SparkSession.builder()
.master("local[3]")
.appName("rdd handle")
.getOrCreate()
val sc=spark.sparkContext
//1.创建Breeze
val m1: DenseMatrix[Double] =DenseMatrix.zeros[Double](2,3)
val m2=DenseMatrix.eye[Double](3)
val v1: DenseVector[Double] =DenseVector.zeros[Double](3)
val v2: DenseVector[Double] =DenseVector.ones[Double](4)
val v3: DenseVector[Int] =DenseVector.fill(3){5}
val v4=DenseVector.range(1,10,2)
val v5=diag(DenseVector(1.0,2.0,3.0))
println("---m1---")
println(m1)
println("---m2---")
println(m2)
println("---v1---")
println(v1)
println("---v2---")
println(v2)
println("---v3---")
println(v3)
println("---v4---")
println(v4)
println("---v5---")
println(v5)
}
}
打印结果如下:
---m1---
0.0 0.0 0.0
0.0 0.0 0.0
---m2---
1.0 0.0 0.0
0.0 1.0 0.0
0.0 0.0 1.0
---v1---
DenseVector(0.0, 0.0, 0.0)
---v2---
DenseVector(1.0, 1.0, 1.0, 1.0)
---v3---
DenseVector(5, 5, 5)
---v4---
DenseVector(1, 3, 5, 7, 9)
---v5---
1.0 0.0 0.0
0.0 2.0 0.0
0.0 0.0 3.0