转载自 https://blog.csdn.net/u012609509/article/details/70230204

Python中的几种矩阵乘法

1. 同线性代数中矩阵乘法的定义: np.dot()

np.dot(A, B):对于二维矩阵,计算真正意义上的矩阵乘积,同线性代数中矩阵乘法的定义。对于一维矩阵,计算两者的内积。见如下Python代码:

import numpy as np

# 2-D array: 2 x 3
two_dim_matrix_one = np.array([[1, 2, 3], [4, 5, 6]])
# 2-D array: 3 x 2
two_dim_matrix_two = np.array([[1, 2], [3, 4], [5, 6]]) two_multi_res = np.dot(two_dim_matrix_one, two_dim_matrix_two)
print('two_multi_res: %s' %(two_multi_res)) # 1-D array
one_dim_vec_one = np.array([1, 2, 3])
one_dim_vec_two = np.array([4, 5, 6])
one_result_res = np.dot(one_dim_vec_one, one_dim_vec_two)
print('one_result_res: %s' %(one_result_res))

结果如下:

two_multi_res: [[22 28]
[49 64]]
one_result_res: 32

2. 对应元素相乘 element-wise product: np.multiply(), 或 *

在Python中,实现对应元素相乘,有2种方式,一个是np.multiply(),另外一个是*,这种方式要求连个矩阵的的形状shape相同。见如下Python代码:

import numpy as np

# 2-D array: 2 x 3
two_dim_matrix_one = np.array([[1, 2, 3], [4, 5, 6]])
another_two_dim_matrix_one = np.array([[7, 8, 9], [4, 7, 1]]) # 对应元素相乘 element-wise product
element_wise = two_dim_matrix_one * another_two_dim_matrix_one
print('element wise product: %s' %(element_wise)) # 对应元素相乘 element-wise product
element_wise_2 = np.multiply(two_dim_matrix_one, another_two_dim_matrix_one)
print('element wise product: %s' % (element_wise_2))

结果如下:

element wise product: [[ 7 16 27]
[16 35 6]]
element wise product: [[ 7 16 27]
[16 35 6]]

矩阵乘法np.dot()及np.multiply()以及*的更多相关文章

  1. 矩阵乘法np.dot()及np.multipy()区别

    1. 线性代数中矩阵乘法: np.dot() import numpy as np ​ # 2 x 3 matrix1 = np.array([[1, 2, 3], [4, 5, 6]]) ​ # 3 ...

  2. Python 中的几种矩阵乘法 np.dot, np.multiply, *【转】

    本文转载自:https://blog.csdn.net/u012609509/article/details/70230204 Python中的几种矩阵乘法1. 同线性代数中矩阵乘法的定义: np.d ...

  3. Python 中的几种矩阵乘法 np.dot, np.multiply, *

    使用array时,运算符 * 用于计算数量积(点乘),函数 dot() 用于计算矢量积(叉乘).使用matrix时,运算符 * 用于计算矢量积,函数 multiply() 用于计算数量积. 下面是使用 ...

  4. 关于python中的矩阵乘法(array和mat类型)

    关于python中的矩阵乘法,我们一般有两种数据格式可以实现:np.array()类型和np.mat()类型: 对于这两种数据类型均有三种操作方式: (1)乘号 * (2)np.dot() (3)np ...

  5. Python中的几种矩阵乘法(转)

    一.  np.dot() 1.同线性代数中矩阵乘法的定义.np.dot(A, B)表示: 对二维矩阵,计算真正意义上的矩阵乘积. 对于一维矩阵,计算两者的内积. 2.代码 [code] import ...

  6. Py中的矩阵乘法【转载】

    转自:https://blog.csdn.net/cqk0100/article/details/76221749 1.总结 对于array对象,*和np.multiply函数代表的是数量积,如果希望 ...

  7. python中np.multiply()、np.dot()和星号(*)三种乘法运算的区别(转)

    为了区分三种乘法运算的规则,具体分析如下: import numpy as np 1. np.multiply()函数 函数作用 数组和矩阵对应位置相乘,输出与相乘数组/矩阵的大小一致 1.1数组场景 ...

  8. [转]python中np.multiply()、np.dot()和星号(*)三种乘法运算的区别

    转自https://blog.csdn.net/zenghaitao0128/article/details/78715140 为了区分三种乘法运算的规则,具体分析如下: import numpy a ...

  9. np.array()和np.dot()的区别

    1.生成数组的方式不同 2.乘法计算方式不同 array生成数组,np.dot()表示矩阵乘积,(*)号或np.multiply()表示点乘 mat生成数组,(*)和np.dot()表示矩阵相乘,点乘 ...

随机推荐

  1. Ztree小demo用于系统授权

    本示例只做到指定id用户的拥有的权限回显,并能动态获得ztree中重新选择的权限id.(至于权限的更新,就是后台人员对象和权限对象建立关系的过程,不做展示) 第一步:拼写jsp页面(下载ztree包, ...

  2. TCP和UDP Client 代码

    最近学习要求做网络编程,使用从网上找了一些资料,主要是网络协议的分层等通讯,你可以查看英文版的资料:CScharp网络编程英文版 下面直接给出代码吧,我想一看应该就懂. TCP Client 代码: ...

  3. 首页大屏广告效果 jquery轮播图淡入淡出

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. (判断)window.open()窗口被关闭后执行事件

    $(function() { // start ready var $article_share=$('#body .article').find('li.share'); // $article_s ...

  5. xhprof使用

    一.下载安装 wget http://pecl.php.net/get/xhprof-0.9.3.tgz tar zxvf xhprof-0.9.3.tgz cd xhprof-0.9.3/exten ...

  6. 【linux】less &&more

    命令 :         less [文件名]                                                                              ...

  7. H5页面获取openid,完成支付公众号(未关注公众号)支付

    一.页面授权 // 进入页面获取权限code function initAuthorizeCode() { var appid = $("#appid").val();//公众号a ...

  8. Bootstrap-CL:徽章

    ylbtech-Bootstrap-CL:徽章 1.返回顶部 1. Bootstrap 徽章(Badges) 本章将讲解 Bootstrap 徽章(Badges).徽章与标签相似,主要的区别在于徽章的 ...

  9. 10-28质量监控ELK

    监控业务范围 app崩溃监控(Bugly) 应用性能监控(APM) 业务监控(TalkingData.友盟) 质量监控(缺位) 质量监控平台ELK elk官网 数据构造 线上错误状态分布 故障影响范围 ...

  10. 使用pandas时遇到ValueError: numpy.dtype has the wrong size, try recompiling

    [问题]使用pandas时遇到ValueError: numpy.dtype has the wrong size, try recompiling [原因] 这是因为 Python 包的版本问题,例 ...