Python 中的几种矩阵乘法 np.dot, np.multiply, *【转】
本文转载自: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))
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
结果如下:
two_multi_res: [[22 28]
[49 64]]
one_result_res: 32
1
2
3
2. 对应元素相乘 element-wise product: np.multiply(), 或 *
在Python中,实现对应元素相乘,有2种方式,一个是np.multiply(),另外一个是*。见如下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))
1
2
3
4
5
6
7
8
9
10
11
12
13
结果如下:
element wise product: [[ 7 16 27]
[16 35 6]]
element wise product: [[ 7 16 27]
[16 35 6]]
---------------------
作者:cltdevelop
来源:CSDN
原文:https://blog.csdn.net/u012609509/article/details/70230204?utm_source=copy
版权声明:本文为博主原创文章,转载请附上博文链接!
Python 中的几种矩阵乘法 np.dot, np.multiply, *【转】的更多相关文章
- Python 中的几种矩阵乘法 np.dot, np.multiply, *
使用array时,运算符 * 用于计算数量积(点乘),函数 dot() 用于计算矢量积(叉乘).使用matrix时,运算符 * 用于计算矢量积,函数 multiply() 用于计算数量积. 下面是使用 ...
- Python中的几种矩阵乘法(转)
一. np.dot() 1.同线性代数中矩阵乘法的定义.np.dot(A, B)表示: 对二维矩阵,计算真正意义上的矩阵乘积. 对于一维矩阵,计算两者的内积. 2.代码 [code] import ...
- Python中的三种数据结构
Python中,有3种内建的数据结构:列表.元组和字典.1.列表 list是处理一组有序项目的数据结构,即你可以在一个列表中存储一个序列的项目.列表中的项目.列表中的项目应该包括在方括号中,这 ...
- python中的三种输入方式
python中的三种输入方式 python2.X python2.x中以下三个函数都支持: raw_input() input() sys.stdin.readline() raw_input( )将 ...
- 简单谈谈Python中的几种常见的数据类型
简单谈谈Python中的几种常见的数据类型 计算机顾名思义就是可以做数学计算的机器,因此,计算机程序理所当然地可以处理各种数值.但是,计算机能处理的远不止数值,还可以处理文本.图形.音频.视频.网页等 ...
- Python中的几种数据类型
大体上把Python中的数据类型分为如下几类: Number(数字) 包括int,long,float,complex String(字符串) 例如:hello,"hello" ...
- Python中的两种结构dict和set
Python内置了字典:dict的支持,dict全称dictionary,在其他语言中也称为map,使用键-值(key-value)存储,具有极快的查找速度. 假设要根据同学的名字查找对应的成绩 如果 ...
- Python中的7种可调用对象
Python中有七种可调用对象,可调用对象可使用内置函数callable来检测 一.用户自定义的函数: 使用def语句或者lambda表达式创建的函数. 二.内置函数: 使用C语言实现的函数,如len ...
- Python中的两种路径
Java中有两种路径,一种是操作系统的路径path,另一种是类路径classpath. Python中也是如此,一种是操作系统环境变量中的path,另一种是PYTHONPATH. 当import xx ...
随机推荐
- Java bytesToHexString 解析
一.代码 /** * Convert byte[] to hex string * * @param src byte[] data * @return hex string */ public st ...
- android(二) SurfaceView
(一)常用类介绍 (1). View:显示视图,内置画布,提供图形绘制函数.触屏事件.按键事件函数等:必须在UI主线程内更新画面,被动更新画面,速度较慢. (2). SurfaceView:基于vie ...
- 【Python】【Web.py】详细解读Python的web.py框架下的application.py模块
详细解读Python的web.py框架下的application.py模块 这篇文章主要介绍了Python的web.py框架下的application.py模块,作者深入分析了web.py的源码, ...
- String.getBytes()未设置字符集导致打印的pdf乱码
如果不设置字符集会选择系统字符集,系统也没设置,会选iso-8859-1 导致汉字乱码,成为?
- logstash采集tomcat日志、mysql错误日志
input{ file { path => "/opt/Tomcat7.0.28/logs/*.txt" start_position => "beginni ...
- Bind()事件
<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- [LeetCode] 877. Stone Game == [LintCode] 396. Coins in a Line 3_hard tag: 区间Dynamic Programming, 博弈
Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, ...
- 手把手教你,C#.Net如何用Log4net把错误日志写入到SQLite数据库中
在项目中,我们往往会有把错误日志记录下来的习惯,这样有利于当网站发布后,能第一时间找到错误的所在地,以及错误的原因,以便于我们第一时间纠错.往往我们会把错误日志直接写到txt文本中,虽然操作简单,但是 ...
- RAC禁用DRM特性
查看"_gc"开头的隐藏参数值: set linesize 333 col name for a35 col description for a66 col value for a ...
- linux命令:帮助命令
帮助命令:man 命令名称:man 命令英文原意:manual 命令所在路径:/usr/bin/man 执行权限:所有用户 语法:man [命令或配置文件] 功能描述:获得帮助信息 范例:$man l ...