矩阵相乘

简介:
在R中矩阵相乘使用%*%运算符号.
矩阵相乘必须满足 : 左边的矩阵列数和右边的矩阵行数相等. 
例如m行,n列的矩阵和n行p列的矩阵可以相乘, 并且得到的是m行,p列的矩阵.
那么结果矩阵上的每个点的数据是怎么计算来的呢?
例如 : 
[2,3]  即第二行第三列这个点.
计算方法 : 左边矩阵的第2行和 右边矩阵的第3列 的每个元素相乘, 然后把所有乘积相加的结果,
例子 : 
> x <- matrix(data = c(1:6), nrow = 2, ncol = 3, byrow = TRUE)
> x
     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    4    5    6
> y <- matrix(data = c(1:12), nrow = 6, ncol = 2, byrow = TRUE)
> y
     [,1] [,2]
[1,]    1    2
[2,]    3    4
[3,]    5    6
[4,]    7    8
[5,]    9   10
[6,]   11   12

> y %*% x
     [,1] [,2] [,3]
[1,]    9   12   15
[2,]   19   26   33
[3,]   29   40   51
[4,]   39   54   69
[5,]   49   68   87
[6,]   59   82  105


验证 : 
> z <- y %*% x
> z[3,2]
[1] 40
> sum(y[3,] * x[,2])
[1] 40
> z[6,3]
[1] 105
> sum(y[6,] * x[,3])
[1] 105

> x[,3]
[1] 3 6
> y[6,]
[1] 11 12
3*11+6*12 = 105


向量也可以和矩阵相乘, 但是同样需要满足条件. 
例如 :   v是向量, a是矩阵,  
v %*% a, 那么v的长度必须等于a的行数 . 并且相乘时v转换为(1行多列)的矩阵
反过来, 
a %*% v,  那么a的列数必须等于v的长度. 并且相乘时v转换为(多行1列)的矩阵
例子 : 

> a <- c(17,19)
> x
     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    4    5    6
> a
[1] 17 19

如果左边矩阵的列宽和右边向量的长度不一致, 则无法计算.
反过来, 把向量放在左边, 矩阵放在右边, 那么必须满足左边的向量长度和右边矩阵的行宽要一致.
例子1 : 

> x %*% a
Error in x %*% a : non-conformable arguments

以下, 左边向量的长度 等于右边矩阵的行宽 (向量转换为1行2列矩阵)
相当于转换为1行2列乘以2行3列.  [1,2] %*% [2,3] ,  得到的是1行3列的矩阵.

> a %*% x
     [,1] [,2] [,3]
[1,]   93  129  165
验证
93 = 1*17 + 4*19
129 = 2*17 + 5*19
165 = 3*17 + 6*19


例子2 : 
> b <- c(100,200,300)
> b %*% x
Error in b %*% x : non-conformable arguments

以下, 左边矩阵的列宽等于右边向量的长度(向量转换为3行1列矩阵). 
相当于转换为2行3列乘以3行1列. [2,3] %*% [3,1] , 得到的是2行1列的矩阵.
> x %*% b
     [,1]
[1,] 1400
[2,] 3200
验证
1400 = 100*1 + 200*2 + 300*3
3200 = 100*4 + 200*5 + 300*6


R提供了crossprod和tcrossprod函数, 也是矩阵相乘, 但是速度比%*%更快.(帮助中提到的)
> help(crossprod)
crossprod                 package:base                 R Documentation

Matrix Crossproduct

Description:

     Given matrices ‘x’ and ‘y’ as arguments, return a matrix
     cross-product.  This is formally equivalent to (but usually
     slightly faster than) the call ‘t(x) %*% y’ (‘crossprod’) or ‘x
     %*% t(y)’ (‘tcrossprod’).

Usage:

     crossprod(x, y = NULL)
     
     tcrossprod(x, y = NULL)
     
Arguments:

    x, y: numeric or complex matrices: ‘y = NULL’ is taken to be the
          same matrix as ‘x’.  Vectors are promoted to single-column or
          single-row matrices, depending on the context.

Value:

     A double or complex matrix, with appropriate ‘dimnames’ taken from
     ‘x’ and ‘y’.

Note:

     When ‘x’ or ‘y’ are not matrices, they are treated as column or
     row matrices, but their ‘names’ are usually *not* promoted to
     ‘dimnames’.  Hence, currently, the last example has empty
     dimnames.

See Also:

     ‘%*%’ and outer product ‘%o%’.

Examples:

     (z <- crossprod(1:4))    # = sum(1 + 2^2 + 3^2 + 4^2)
     drop(z)                  # scalar
     x <- 1:4; names(x) <- letters[1:4]; x
     tcrossprod(as.matrix(x)) # is
     identical(tcrossprod(as.matrix(x)),
               crossprod(t(x)))
     tcrossprod(x)            # no dimnames
     
     m <- matrix(1:6, 2,3) ; v <- 1:3; v2 <- 2:1
     stopifnot(identical(tcrossprod(v, m), v %*% t(m)),
               identical(tcrossprod(v, m), crossprod(v, t(m))),
               identical(crossprod(m, v2), t(m) %*% v2))


行列变换使用 t() 函数.
> x <- matrix(1:12,3,4)
> x
     [,1] [,2] [,3] [,4]
[1,]    1    4    7   10
[2,]    2    5    8   11
[3,]    3    6    9   12
> t(x)
     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    4    5    6
[3,]    7    8    9
[4,]   10   11   12

> x %*% t(x)
     [,1] [,2] [,3]
[1,]  166  188  210
[2,]  188  214  240
[3,]  210  240  270
> t(x) %*% x
     [,1] [,2] [,3] [,4]
[1,]   14   32   50   68
[2,]   32   77  122  167
[3,]   50  122  194  266
[4,]   68  167  266  365

> crossprod(x,x)
     [,1] [,2] [,3] [,4]
[1,]   14   32   50   68
[2,]   32   77  122  167
[3,]   50  122  194  266
[4,]   68  167  266  365
> tcrossprod(x,x)
     [,1] [,2] [,3]
[1,]  166  188  210
[2,]  188  214  240
[3,]  210  240  270

crossprod, tcrossprod的y可以不输入.
> crossprod(x)
     [,1] [,2] [,3] [,4]
[1,]   14   32   50   68
[2,]   32   77  122  167
[3,]   50  122  194  266
[4,]   68  167  266  365
> tcrossprod(x)
     [,1] [,2] [,3]
[1,]  166  188  210
[2,]  188  214  240
[3,]  210  240  270


可以看到: 
> crossprod(x,x) == t(x) %*% x
     [,1] [,2] [,3] [,4]
[1,] TRUE TRUE TRUE TRUE
[2,] TRUE TRUE TRUE TRUE
[3,] TRUE TRUE TRUE TRUE
[4,] TRUE TRUE TRUE TRUE
> tcrossprod(x,x) == x %*% t(x)
     [,1] [,2] [,3]
[1,] TRUE TRUE TRUE
[2,] TRUE TRUE TRUE
[3,] TRUE TRUE TRUE


> help(t)
t                     package:base                     R Documentation

Matrix Transpose

Description:

     Given a matrix or ‘data.frame’ ‘x’, ‘t’ returns the transpose of
     ‘x’.

Usage:

     t(x)
     
Arguments:

       x: a matrix or data frame, typically.

Details:

     This is a generic function for which methods can be written.  The
     description here applies to the default and ‘"data.frame"’
     methods.

     A data frame is first coerced to a matrix: see ‘as.matrix’.  When
     ‘x’ is a vector, it is treated as a column, i.e., the result is a
     1-row matrix.

Value:

     A matrix, with ‘dim’ and ‘dimnames’ constructed appropriately from
     those of ‘x’, and other attributes except names copied across.


[参考]
1.  http://blog.csdn.net/myan/article/details/647511 4.  http://f.dataguru.cn/thread-367824-1-1.html
5. R

> help("%*%")
matmult                  package:base                  R Documentation

Matrix Multiplication

Description:

     Multiplies two matrices, if they are conformable.  If one argument
     is a vector, it will be promoted to either a row or column matrix
     to make the two arguments conformable.  If both are vectors it
     will return the inner product (as a matrix).

Usage:

     x %*% y
     
Arguments:

    x, y: numeric or complex matrices or vectors.

Details:

     When a vector is promoted to a matrix, its names are not promoted
     to row or column names, unlike ‘as.matrix’.

     This operator is S4 generic but not S3 generic.  S4 methods need
     to be written for a function of two arguments named ‘x’ and ‘y’.

Value:

     A double or complex matrix product.  Use ‘drop’ to remove
     dimensions which have only one level.

References:

     Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) _The New S
     Language_.  Wadsworth & Brooks/Cole.

See Also:

     ‘matrix’, ‘Arithmetic’, ‘diag’.

Examples:

     x <- 1:4
     (z <- x %*% x)    # scalar ("inner") product (1 x 1 matrix)
     drop(z)             # as scalar
     
     y <- diag(x)
     z <- matrix(1:12, ncol = 3, nrow = 4)
     y %*% z
     y %*% x
     x %*% z

相关文章
|
10月前
|
机器学习/深度学习 资源调度 搜索推荐
协同过滤算法深入解析:构建智能推荐系统的核心技术
一、前言 随着互联网的高速发展,我们每天面临着海量信息的冲击,从而使得我们无法有效地筛选出感兴趣的信息。在这种背景下,推荐系统应运而生,成为帮助用户过滤信息,找到自己感兴趣内容的有效工具。协同过滤算法作为推荐系统中的一种核心技术,广泛应用于电商、社交媒体、音乐、电影等多个领域,极大地改善了用户体验。本文将对协同过滤算法进行深入解析,让我们一起探讨这一神奇的技术。
461 0
|
自然语言处理 搜索推荐 算法
推荐系统:基于内容的过滤
此图像包含用户喜欢的电影的描述。根据用户喜欢的电影向用户推荐电影,需要使用这些描述得到一个数学形式,即文本应该是可测量的,然后通过与其他电影进行比较来找到相似的描述。 我们有各种电影和关于这些电影的数据。为了能够比较这些电影数据,需要对数据进行矢量化。在向量化这些描述时,必须创建所有电影描述(假设 n)和所有电影(假设 m)中的唯一词矩阵。列中有所有唯一的单词,行中有所有电影,每个单词在交叉点的电影中使用了多少。这样,文本就可以被矢量化。
216 0
|
机器学习/深度学习 数据采集 搜索推荐
推荐系统!基于tensorflow搭建混合神经网络精准推荐! ⛵
本文从常见的推荐系统方法(基于内容、协同过滤等近邻算法、基于知识等)讲起,一直覆盖到前沿的新式推荐系统,不仅详细讲解原理,还手把手教大家如何用代码实现。
4909 5
推荐系统!基于tensorflow搭建混合神经网络精准推荐! ⛵
|
存储 流计算 Windows
Flink窗口(Flink Window)
Flink窗口(Flink Window)
Flink窗口(Flink Window)
|
NoSQL MongoDB 索引
MongoDB:16-MongoDB-索引数组字段和索引子文档字段
MongoDB:16-MongoDB-索引数组字段和索引子文档字段
841 0
DATEADD() 函数
DATEADD() 函数
205 0
|
资源调度 分布式计算 Hadoop
CDH 搭建_ Hadoop _ Yarn 搭建|学习笔记
快速学习 CDH 搭建_ Hadoop _ Yarn 搭建
169 0
CDH 搭建_ Hadoop _ Yarn 搭建|学习笔记
|
机器学习/深度学习 SQL 数据采集
如何使用 PAI-Studio 可视化构建模型到部署|学习笔记
快速学习如何使用 PAI-Studio 可视化构建模型到部署。
815 0
如何使用 PAI-Studio 可视化构建模型到部署|学习笔记
|
存储 XML 分布式计算
小六六学大数据之 Spark(Scala)(上)
前言 文本已收录至我的GitHub仓库,欢迎Star:github.com/bin39232820… 种一棵树最好的时间是十年前,其次是现在
203 0