第三章 spark2.3 mllib机器学习-mllib矩阵向量

本系列”spark mllib机器学习”,均以最新spark2.3.0版本为蓝本进行编写,参考书籍<< spark mllib机器学习 >>黄美灵版,转载请注明出处
GitHub地址:https://github.com/future-fu/learnsparkmllib

目录

  1. Breeze介绍
  2. BLAS介绍
  3. Mllib向量
  4. Mllib矩阵
  5. 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  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值