转载自 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. TypeScript学习笔记(八) - 声明文件

    本篇将介绍TypeScript的声明文件,并简单演示一下如何编写和使用声明文件.本篇也是这个系列的最后一篇. 一.声明文件简介 TypeScript作为JavaScript的超集,在开发过程中不可避免 ...

  2. ambassador 学习一基本试用

    安装使用docker for mac Without RBAC 安装ambassador 安装 kubectl apply -f https://getambassador.io/yaml/ambas ...

  3. CentOS6.6安装(转)

    2015-3-6CentOS6.6安装 环境:工控机 1.选择安装software development workstation,其他参考以下文档过程 一.安装CentOS 6.6,安装结束重新启动 ...

  4. usbip install

    # README for usbip-utils## Copyright (C) 2011 matt mooney <mfm@muteddisk.com>#               2 ...

  5. 集群(heartbeat)搭建

    HA 即(high available cluster)高可用集群,又称双机热备,保证关键性业务的不间断提供服务. 如:两台机器A和B,正常情况A提供服务,B待命闲置:一但A宕机或服务宕掉,自动切换至 ...

  6. PHP分多步骤填写发布信息的简单方法实例代码

    1.php 复制代码 代码如下: <form name=form1 id=form1 method=post action=2.php> 基本信息1:<input type=text ...

  7. 总结开发中使用到的npm 库

    1.Swiper  https://github.com/nolimits4web/Swiper 移动端slides插件 2.fetch https://github.com/whatwg/fetch ...

  8. mybatis Dynamic SQL

    reference: http://www.mybatis.org/mybatis-3/dynamic-sql.html Dynamic SQL One of the most powerful fe ...

  9. 关于phpmailer邮件发送

    今天有个需求,要把phpmailer集成到框架里面 所以我去官方下载了 phpmail5.2.6 地址在 https://github.com/PHPMailer/PHPMailer/releases ...

  10. leetcode257

    /** * Definition for a binary tree node. * public class TreeNode { * public int val; * public TreeNo ...