Spark MLlib之使用Breeze操作矩阵向量
在使用Breeze 库时,需要导入相关包:
import breeze.linalg._
import breeze.numerics._
Breeze创建函数
//全0矩阵
DenseMatrix.zeros[Double](,) res0: breeze.linalg.DenseMatrix[Double] =
0.0 0.0
0.0 0.0
0.0 0.0 //全0向量
DenseVector.zeros[Double]() res1: breeze.linalg.DenseVector[Double] = DenseVector(0.0, 0.0) //全1向量
DenseVector.ones[Double]() res2: breeze.linalg.DenseVector[Double] = DenseVector(1.0, 1.0) //按数值填充向量
DenseVector.fill[Double](, ) res3: breeze.linalg.DenseVector[Double] = DenseVector(2.0, 2.0, 2.0) //生成随机向量
DenseVector.range(, , )
DenseVector.rangeD(, , )
DenseVector.rangeF(, , ) res4: breeze.linalg.DenseVector[Int] = DenseVector(, , , )
res5: breeze.linalg.DenseVector[Double] = DenseVector(1.0, 3.0, 5.0, 7.0)
res6: breeze.linalg.DenseVector[Float] = DenseVector(1.0, 3.0, 5.0, 7.0) //单位矩阵
DenseMatrix.eye[Double]() res7: breeze.linalg.DenseMatrix[Double] =
1.0 0.0 0.0 0.0
0.0 1.0 0.0 0.0
0.0 0.0 1.0 0.0
0.0 0.0 0.0 1.0 //对角矩阵
diag(DenseVector(3.0, 4.0, 5.0)) res8: breeze.linalg.DenseMatrix[Double] =
3.0 0.0 0.0
0.0 4.0 0.0
0.0 0.0 5.0 //按照行创建矩阵
DenseMatrix((4.0, 5.0, 6.0), (7.0, 8.0, 9.0)) res9: breeze.linalg.DenseMatrix[Double] =
4.0 5.0 6.0
7.0 8.0 9.0 //按照行创建向量
DenseVector((4.0, 5.0, 6.0, 7.0, 8.0, 9.0)) res10: breeze.linalg.DenseVector[(Double, Double, Double, Double, Double, Double)] = DenseVector((4.0,5.0,6.0,7.0,8.0,9.0)) //向量转置
DenseVector((4.0, 5.0, 6.0, 7.0, 8.0, 9.0)).t res11: breeze.linalg.Transpose[breeze.linalg.DenseVector[(Double, Double, Double, Double, Double, Double)]] = Transpose(DenseVector((4.0,5.0,6.0,7.0,8.0,9.0))) //从函数创建向量
DenseVector.tabulate()(i => i*i)
DenseVector.tabulate( to )(i => i*i) res12: breeze.linalg.DenseVector[Int] = DenseVector(, , , , )
res13: breeze.linalg.DenseVector[Int] = DenseVector(, , , , , ) //从函数创建矩阵
DenseMatrix.tabulate(, ){ case (i, j) => i*i+j*j } res14: breeze.linalg.DenseMatrix[Int] = //从数组创建向量
new DenseVector[Double](Array(2.0, 5.0, 8.0)) res15: breeze.linalg.DenseVector[Double] = DenseVector(2.0, 5.0, 8.0) //从数组创建矩阵
new DenseMatrix[Double](, , Array(1.0, 4.0, 7.0, 3.0, 6.0, 9.0)) res16: breeze.linalg.DenseMatrix[Double] =
1.0 3.0
4.0 6.0
7.0 9.0 //0 到 1的随机向量
DenseVector.rand(, Rand.uniform)
DenseVector.rand(, Rand.gaussian) res17: breeze.linalg.DenseVector[Double] = DenseVector(0.30960687979350654, 0.5779984012083466, 0.4880956198283952, 0.1013947992922748, 0.19635570812305936, 0.8533170989347008, 0.6619843996111201, 0.03131533370356321, 0.5430592884856604)
res18: breeze.linalg.DenseVector[Double] = DenseVector(0.48361471134641176, -1.734778260551877, -0.7319505628964431, 0.19971267958211184, -1.033191008131693, -1.7961545888066046, 0.2364555601503527, 0.22843047924270285, 1.7288956723034343) //0 到 1的随机矩阵
DenseMatrix.rand(, , Rand.uniform)
DenseMatrix.rand(, , Rand.gaussian) res19: breeze.linalg.DenseMatrix[Double] = 0.11270960774886585 0.19871332589909851
0.5581898434134047 0.8295064603050235
0.8692650535288642 0.4015512971620494
res20: breeze.linalg.DenseMatrix[Double] = 0.712041684728872 2.7007736007506216
0.053520407807479485 0.19044772577405517
-0.7370909025873376 -1.024737052742153
Breeze元素访问
val a = new DenseVector[Int](Array( to : _*)) a: breeze.linalg.DenseVector[Int] = DenseVector(, , , , , , , , , , ) //指定位置
a() res21: Int = //向量子集
a( to ) res22: breeze.linalg.DenseVector[Int] = DenseVector(, , , ) //按照指定步长取子集
a( to by -) res23: breeze.linalg.DenseVector[Int] = DenseVector(, , , , , ) //指定开始位置至结尾
a( to -) res24: breeze.linalg.DenseVector[Int] = DenseVector(, , , , , , , , , ) //最后一个元素
a(-) res25: Int = val m = DenseMatrix((1.0, 2.0, 3.0), (4.0, 5.0, 6.0)) m: breeze.linalg.DenseMatrix[Double] =
1.0 2.0 3.0
4.0 5.0 6.0 //指定位置
m(, ) res26: Double = 2.0 //矩阵指定列
m(::, )
val a = new DenseVector[Int](Array(10 to 20: _*))
a: breeze.linalg.DenseVector[Int] = DenseVector(10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20)
//指定位置
a(0)
res21: Int = 10
//向量子集
a(1 to 4)
res22: breeze.linalg.DenseVector[Int] = DenseVector(11, 12, 13, 14)
//按照指定步长取子集
a(5 to 0 by -1)
res23: breeze.linalg.DenseVector[Int] = DenseVector(15, 14, 13, 12, 11, 10)
//指定开始位置至结尾
a(1 to -1)
res24: breeze.linalg.DenseVector[Int] = DenseVector(11, 12, 13, 14, 15, 16, 17, 18, 19, 20)
//最后一个元素
a(-1)
res25: Int = 20
val m = DenseMatrix((1.0, 2.0, 3.0), (4.0, 5.0, 6.0))
m: breeze.linalg.DenseMatrix[Double] =
1.0 2.0 3.0
4.0 5.0 6.0
//指定位置
m(0, 1)
res26: Double = 2.0
//矩阵指定列
m(::, 1)
res27: breeze.linalg.DenseVector[Double] = DenseVector(2.0, 5.0)
Breeze元素操作
//调整矩阵形状
m.reshape(, ) res28: breeze.linalg.DenseMatrix[Double] =
1.0 6.0 11.0
5.0 10.0 4.0
9.0 3.0 8.0
2.0 7.0 12.0 //矩阵转成向量
m.toDenseVector res29: breeze.linalg.DenseVector[Double] = DenseVector(1.0, 5.0, 9.0, 2.0, 6.0, 10.0, 3.0, 7.0, 11.0, 4.0, 8.0, 12.0) //复制下三角
lowerTriangular(m) res30: breeze.linalg.DenseMatrix[Double] =
1.0 0.0 0.0
5.0 6.0 0.0
9.0 10.0 11.0 //复制上三角
upperTriangular(m) res31: breeze.linalg.DenseMatrix[Double] =
1.0 2.0 3.0
0.0 6.0 7.0
0.0 0.0 11.0 //矩阵复制
m.copy res32: breeze.linalg.DenseMatrix[Double] =
1.0 2.0 3.0 4.0
5.0 6.0 7.0 8.0
9.0 10.0 11.0 12.0 //取对角线元素
diag(upperTriangular(m)) res33: breeze.linalg.DenseVector[Double] = DenseVector(1.0, 6.0, 11.0) //子集赋数值
a( to ) :=
a res34: breeze.linalg.DenseVector[Int] = DenseVector(, , , )
res35: breeze.linalg.DenseVector[Int] = DenseVector(, , , , , , , , , , ) //子集赋向量
a( to ) := DenseVector(, , , )
a res36: breeze.linalg.DenseVector[Int] = DenseVector(, , , )
res37: breeze.linalg.DenseVector[Int] = DenseVector(, , , , , , , , , , ) //矩阵赋值
m( to , to ) := 0.0
m res38: breeze.linalg.DenseMatrix[Double] =
0.0 0.0
0.0 0.0
res39: breeze.linalg.DenseMatrix[Double] =
1.0 2.0 3.0 4.0
5.0 0.0 0.0 8.0
9.0 0.0 0.0 12.0 //矩阵列赋值
m(::, ) := 5.0
m res40: breeze.linalg.DenseVector[Double] = DenseVector(5.0, 5.0, 5.0)
res41: breeze.linalg.DenseMatrix[Double] =
1.0 2.0 5.0 4.0
5.0 0.0 5.0 8.0
9.0 0.0 5.0 12.0 val a1 = DenseMatrix((1.0, 2.0, 3.0), (4.0, 5.0, 6.0))
val a2 = DenseMatrix((7.0, 8.0, 9.0), (10.0, 11.0, 12.0)) a1: breeze.linalg.DenseMatrix[Double] =
1.0 2.0 3.0
4.0 5.0 6.0
a2: breeze.linalg.DenseMatrix[Double] =
7.0 8.0 9.0
10.0 11.0 12.0 //垂直连接矩阵
DenseMatrix.vertcat(a1, a2) res42: breeze.linalg.DenseMatrix[Double] =
1.0 2.0 3.0
4.0 5.0 6.0
7.0 8.0 9.0
10.0 11.0 12.0 //横向连接矩阵
DenseMatrix.horzcat(a1, a2) res43: breeze.linalg.DenseMatrix[Double] =
1.0 2.0 3.0 7.0 8.0 9.0
4.0 5.0 6.0 10.0 11.0 12.0 //向量连接
DenseVector.vertcat(DenseVector(, , ), DenseVector(, , ))
DenseVector.horzcat(DenseVector(, , ), DenseVector(, , )) res44: breeze.linalg.DenseVector[Int] = DenseVector(, , , , , )
res45: breeze.linalg.DenseMatrix[Int] =
Breeze数值计算函数
//元素加法
a1 + a2 res46: breeze.linalg.DenseMatrix[Double] =
8.0 10.0 12.0
14.0 16.0 18.0 //元素乘法
a1 :* a2 res47: breeze.linalg.DenseMatrix[Double] =
7.0 16.0 27.0
40.0 55.0 72.0 //元素除法
a1 :/ a2 res48: breeze.linalg.DenseMatrix[Double] =
0.14285714285714285 0.25 0.3333333333333333
0.4 0.45454545454545453 0.5 //元素比较
a1 :< a2 res49: breeze.linalg.DenseMatrix[Boolean] =
true true true
true true true //元素相等
a1 :== a2 res50: breeze.linalg.DenseMatrix[Boolean] =
false false false
false false false //元素追加
a1 :+=2.0 res51: breeze.linalg.DenseMatrix[Double] =
3.0 4.0 5.0
6.0 7.0 8.0 //元素追乘
a1 :*=2.0 res52: breeze.linalg.DenseMatrix[Double] =
6.0 8.0 10.0
12.0 14.0 16.0 //向量点积
DenseVector(, , , ) dot DenseVector(, , , ) res53: Int = //元素最大值
max(a1) res54: Double = 16.0 //元素最小值
min(a1) res55: Double = 6.0 //元素最大值的位置
argmax(a1) res56: (Int, Int) = (,) //元素最小值的位置
argmin(a1) res57: (Int, Int) = (,)
Breeze求和函数
val m1 = DenseMatrix((1.0, 2.0, 3.0, 4.0), (5.0, 6.0, 7.0, 8.0), (9.0, 10.0, 11.0, 12.0)) m1: breeze.linalg.DenseMatrix[Double] =
1.0 2.0 3.0 4.0
5.0 6.0 7.0 8.0
9.0 10.0 11.0 12.0 //元素求和
sum(m1) res58: Double = 78.0 //每一列求和
sum(m1, Axis._0) res59: breeze.linalg.DenseMatrix[Double] = 15.0 18.0 21.0 24.0 //每一行求和
sum(m1, Axis._1) res60: breeze.linalg.DenseVector[Double] = DenseVector(10.0, 26.0, 42.0) //对角线元素和
trace(lowerTriangular(m1)) res61: Double = 18.0 //累积和
val a3 = new DenseVector[Int](Array( to : _*))
accumulate(a3) a3: breeze.linalg.DenseVector[Int] = DenseVector(, , , , , , , , , , )
res62: breeze.linalg.DenseVector[Int] = DenseVector(, , , , , , , , , , )
Breeze布尔函数
val c = DenseVector(true, false, true)
val d = DenseVector(false, true, true)
//元素与操作
c :& d res63: breeze.linalg.DenseVector[Boolean] = DenseVector(false, false, true) //元素或操作
c :| d res64: breeze.linalg.DenseVector[Boolean] = DenseVector(true, true, true) //元素非操作
!c res65: breeze.linalg.DenseVector[Boolean] = DenseVector(false, true, false) val e = DenseVector[Int](-, , ) e: breeze.linalg.DenseVector[Int] = DenseVector(-, , ) //存在非零元素
any(e) res66: Boolean = true //所有元素非零
all(e) res67: Boolean = false
Breeze线性代数函数
val f = DenseMatrix((1.0, 2.0, 3.0), (4.0, 5.0, 6.0), (7.0, 8.0, 9.0))
val g = DenseMatrix((1.0, 1.0, 1.0), (1.0, 1.0, 1.0), (1.0, 1.0, 1.0)) f: breeze.linalg.DenseMatrix[Double] =
1.0 2.0 3.0
4.0 5.0 6.0
7.0 8.0 9.0
g: breeze.linalg.DenseMatrix[Double] =
1.0 1.0 1.0
1.0 1.0 1.0
1.0 1.0 1.0 //线性求解,AX = B,求解X
f \ g res68: breeze.linalg.DenseMatrix[Double] =
-2.5 -2.5 -2.5
4.0 4.0 4.0
-1.5 -1.5 -1.5 //转置
f.t res69: breeze.linalg.DenseMatrix[Double] =
1.0 4.0 7.0
2.0 5.0 8.0
3.0 6.0 9.0 //求特征值
det(f) res70: Double = 6.661338147750939E-16 //求逆
inv(f) res71: breeze.linalg.DenseMatrix[Double] =
-4.503599627370499E15 9.007199254740992E15 -4.503599627370495E15
9.007199254740998E15 -1.8014398509481984E16 9.007199254740991E15
-4.503599627370498E15 9.007199254740992E15 -4.5035996273704955E15 //求伪逆
pinv(f) res72: breeze.linalg.DenseMatrix[Double] =
-3.7720834019330525E14 7.544166803866101E14 -3.77208340193305E14
7.544166803866094E14 -1.5088333607732208E15 7.544166803866108E14
-3.772083401933041E14 7.544166803866104E14 -3.772083401933055E14 //特征值和特征向量
eig(f) res73: breeze.linalg.eig.DenseEig =
Eig(DenseVector(16.116843969807043, -1.1168439698070427, -1.3036777264747022E-15),
DenseVector(0.0, 0.0, 0.0),
-0.23197068724628617 -0.7858302387420671 0.40824829046386363
-0.5253220933012336 -0.08675133925662833 -0.816496580927726
-0.8186734993561815 0.61232756022881 0.4082482904638625 ) //奇异值分解
val svd.SVD(u,s,v) = svd(g) u: breeze.linalg.DenseMatrix[Double] =
-0.5773502691896255 -0.5773502691896257 -0.5773502691896256
-0.5773502691896256 -0.2113248654051871 0.7886751345948126
-0.5773502691896256 0.7886751345948129 -0.21132486540518708
s: breeze.linalg.DenseVector[Double] = DenseVector(3.0000000000000004, 0.0, 0.0)
v: breeze.linalg.DenseMatrix[Double] =
-0.5773502691896256 -0.5773502691896257 -0.5773502691896256
0.0 -0.7071067811865474 0.7071067811865477
0.816496580927726 -0.4082482904638629 -0.4082482904638628 //求矩阵的秩
rank(f) res74: Int = //矩阵长度
f.size res75: Int = //矩阵行数
f.rows res76: Int = //矩阵列数
f.cols res77: Int =
Breeze取整函数
val h = DenseVector(-1.2, 0.7, 2.3) h: breeze.linalg.DenseVector[Double] = DenseVector(-1.2, 0.7, 2.3) //四舍五入
round(h) res78: breeze.linalg.DenseVector[Long] = DenseVector(-, , ) //大于它的最小整数
ceil(h) res79: breeze.linalg.DenseVector[Double] = DenseVector(-1.0, 1.0, 3.0) //小于它的最大整数
floor(h) res80: breeze.linalg.DenseVector[Double] = DenseVector(-2.0, 0.0, 2.0) //符号函数
signum(h) res81: breeze.linalg.DenseVector[Double] = DenseVector(-1.0, 1.0, 1.0) //取正数
abs(h) res82: breeze.linalg.DenseVector[Double] = DenseVector(1.2, 0.7, 2.3)
Spark MLlib之使用Breeze操作矩阵向量的更多相关文章
- spark MLlib矩阵四则运算,线性代数
1.导包请看我的上一篇博文,maven项目的包 https://www.cnblogs.com/wuzaipei/p/10965680.html 2.denseMatirx 矩阵四则运算如下 版本不同 ...
- Spark MLlib 机器学习
本章导读 机器学习(machine learning, ML)是一门涉及概率论.统计学.逼近论.凸分析.算法复杂度理论等多领域的交叉学科.ML专注于研究计算机模拟或实现人类的学习行为,以获取新知识.新 ...
- Spark 实践——基于 Spark MLlib 和 YFCC 100M 数据集的景点推荐系统
1.前言 上接 YFCC 100M数据集分析笔记 和 使用百度地图api可视化聚类结果, 在对 YFCC 100M 聚类出的景点信息的基础上,使用 Spark MLlib 提供的 ALS 算法构建推荐 ...
- Spark MLlib 之 Vector向量深入浅出
Spark MLlib里面提供了几种基本的数据类型,虽然大部分在调包的时候用不到,但是在自己写算法的时候,还是很需要了解的.MLlib支持单机版本的local vectors向量和martix矩阵,也 ...
- Spark Mllib里的分布式矩阵(行矩阵、带有行索引的行矩阵、坐标矩阵和块矩阵概念、构成)(图文详解)
不多说,直接上干货! Distributed matrix : 分布式矩阵 一般能采用分布式矩阵,说明这数据存储下来,量还是有一定的.在Spark Mllib里,提供了四种分布式矩阵存储形式,均由支 ...
- Spark Mllib里的向量标签概念、构成(图文详解)
不多说,直接上干货! Labeled point: 向量标签 向量标签用于对Spark Mllib中机器学习算法的不同值做标记. 例如分类问题中,可以将不同的数据集分成若干份,以整数0.1.2,... ...
- Spark Mllib里的本地矩阵概念、构成(图文详解)
不多说,直接上干货! Local matrix:本地矩阵 数组Array(1,2,3,4,5,6)被重组成一个新的2行3列的矩阵. testMatrix.scala package zhouls.bi ...
- Spark Mllib里的本地向量集(密集型数据集和稀疏型数据集概念、构成)(图文详解)
不多说,直接上干货! Local vector : 本地向量集 由两类构成:稀疏型数据集(spares)和密集型数据集(dense) (1).密集型数据集 例如一个向量数据(9,5,2,7),可以设 ...
- Spark Mllib里如何建立向量标签(图文详解)
不多说,直接上干货! 注意: val pos = LabeledPoint(1, vd) val neg = LabeledPoint(2, vs) 除了这两种建立向量标签.还可以从数据库中获取固定格 ...
随机推荐
- css世界的学习笔记
1.+选择器div+p:选择紧接在div后的所有p元素: 2.导航的样式:(注意:不给a设置width:100%;只是需要display:block;利用流布居:) <h4>无宽度,借助流 ...
- 下载Chrome商店和Youtube资源
下载chrome浏览器插件 站点:http://cooal.cn/crx.php 操作步骤: 1.打开扩展介绍页面 (在 三道杠图标>工具>扩展程序 里相应扩展的"访问网站&qu ...
- [转]用JAVA在读取EXCEL文件时如何判断列隐藏
原文地址:https://www.cnblogs.com/OwenWu/archive/2012/01/03/2310620.html org.apache.poi.hssf.usermodel.HS ...
- SSH下authorized_keys, id_rsa, id_rsa.pub, known_hosts作用
一.known_hsots ssh会把你每个你访问过计算机的公钥(public key)都记录在~/.ssh/known_hosts.当下次访问相同计算机时,OpenSSH会核对公钥.如果公钥不同,O ...
- windows ngix 安装 配置 使用
参考版本nginx-1.10.3 一.常用命令 start nginx.exe //开启服务 nginx.exe -s stop ...
- linux 测试网络url命令
1.curl 地址 或者 wget url地址 [hxxxx@iZ23vy2msooZ ~]$ curl www.baidu.com<!DOCTYPE html><!--STATUS ...
- Qt编写自定义控件4-旋转仪表盘
前言 旋转仪表盘,一般用在需要触摸调节设置值的场景中,其实Qt本身就提供了QDial控件具有类似的功能,本控件最大的难点不在于绘制刻度和指针等,而在于自动计算当前用户按下处的坐标转换为当前值,这个功能 ...
- Let's Encrypt泛域名SSL证书申请
操作系统:CentOS 7 github:https://github.com/Neilpang/acme.sh 有中文说明: https://github.com/Neilpang/acme.sh ...
- Linux下的awk文本分析命令实例(一)
1. 入门实例1.1 显示最近登录的5个帐号: [root@localhost ~]# | awk '{print $1}' root root root root reboot 1.2 如果只是显 ...
- E - Stones 优先队列
来源1896 Because of the wrong status of the bicycle, Sempr begin to walk east to west every morning an ...