当前位置:网站首页>[numpy] broadcast mechanism
[numpy] broadcast mechanism
2022-07-27 20:53:00 【When camellia flowers bloom.】

radio broadcast , Namely Numpy Yes Arrays of different dimensions Can be calculated numerically
Radio is Numpy The unique function of array , list 、 Tuples and other common data types do not have this function
If two arrays a and b The same shape , The meet a.shape == b.shape, that a * b The result of is an array a And array b Corresponding bit multiplication , They can directly do the corresponding operations , This requires the same dimension of the array , And the length of each dimension is the same
import numpy as np
a = np.array([1,2,3,4])
b = np.array([10,20,30,40])
res = a * b
print(res) # [ 10 40 90 160]When in operation 2 An array of Different shapes when ,Numpy Will automatically trigger the broadcast mechanism ( For arrays with smaller shapes , Repeat a certain number of times horizontally or vertically , Make it have the same dimension as the larger array )
import numpy as np
a = np.array([[ 0, 0, 0],
[10,10,10],
[20,20,20],
[30,30,30]])
b = np.array([0,1,2])
'''
[[ 0 1 2]
[10 11 12]
[20 21 22]
[30 31 32]]
'''
print(a + b)4 x 3 Two dimensional array of a And 1 x 3 One dimensional array of b Add up , In essence, it can be understood as an array b Expand vertically and downwards 3 Time ( Repeat the first line 3 Time ), So as to generate and array a An array of the same shape , Then with the array a Carry out operations

1. One dimensional array and scalar direct operation , Then every element of the one-dimensional array is different from the scalar , The final result is a one-dimensional array
import numpy as np
boradcast = np.array([1,2,3,4,5])
scalar = 1
boradcast - scalar # array([0, 1, 2, 3, 4])2. When a two-dimensional array and a one-dimensional array are operated , A one-dimensional array propagates on a two-dimensional array in units of its own size
import numpy as np
boradcast2 = np.array([[1,2,3,4,5],
[6,7,8,9,10]])
boradcast1 = np.array([1,2,3,4,5])
'''
array([[0, 0, 0, 0, 0],
[5, 5, 5, 5, 5]])
'''
boradcast2 - boradcast1 3. When a three-dimensional array is operated on with a scalar , Scalars propagate on three-dimensional arrays in units of their own size , So the final result is that each element of the three-dimensional array is subtracted 1
import numpy as np
boradcast3 = np.array([[[1,2,3,4,5],[6,7,8,9,10]],
[[11,12,13,14,15],[16,17,18,19,20]]])
scalar = 1
'''
array([[[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9]],
[[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19]]])
'''
boradcast3 - scalar4. When a three-dimensional array and a two-dimensional array are operated , A two-dimensional array propagates on a three-dimensional array in units of its own size , So the final result is the difference between the two two two-dimensional arrays under the three-dimensional array and the two-dimensional array for broadcasting
import numpy as np
boradcast2 = np.array([[1,2,3,4,5],
[6,7,8,9,10]])
boradcast3 = np.array([[[1,2,3,4,5],[6,7,8,9,10]],
[[11,12,13,14,15],[16,17,18,19,20]]])
'''
array([[[ 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0]],
[[10, 10, 10, 10, 10],
[10, 10, 10, 10, 10]]])
'''
boradcast3 - boradcast2边栏推荐
- After working for bytek for two years, he got 15 offers at one go
- LabVIEW学习笔记五:按钮按下后无法返回原状
- Lennix Lai, OKx financial market director: Web3 is a revolution
- Introduction to JVs Foundation
- openresty lua-resty-dns 域名解析
- Introduction to rk3399 platform introduction to proficient series (Introduction) 21 day learning challenge
- 走马灯案例
- adb shell ls /system/bin(索引表)
- 在字节干了两年离职后,一口气拿到15家Offer
- RK3399平台入门到精通系列讲解(导读篇)21天学习挑战介绍
猜你喜欢

Go --- automatic recompilation of air

Swiftui view onReceive method receives "redundant" event resolution

创新案例 | 本地生活服务数字化,高德品牌升级的增长战略

knife4j通过js动态刷新全局参数

基于ATX自动化测试解决方案

海康设备接入EasyCVR,出现告警信息缺失且不同步该如何解决?

人家这才叫软件测试工程师,你那只是混口饭吃(附HR面试宝典)

浅析即时通讯移动端 IM 开发中登录请求的优化

Babbitt | metauniverse daily must read: Tencent News suspended the sales service of digital collections, users left messages asking for a "refund", and phantom core also fell into the dilemma of "unsa

A layered management method of application layer and hardware layer in embedded system
随机推荐
【分层强化学习】HAC论文及代码
浅析即时通讯移动端 IM 开发中登录请求的优化
JVs official account login configuration
未定义变量 “Lattice“ 或类 “Lattice.latticeEasy“(Matlab)
Express WEB服务器的简单使用
JVs privatization deployment start failure handling scheme
MySQL string function
IM即时通讯开发如何提升移动网络下图片传输速度和成功率
Advanced SQL skills CTE and recursive query
MySQL log query log
Using dataX to realize efficient synchronization of MySQL data
好开不贵,舒适安全!深度体验比亚迪宋Pro DM-i
金仓数据库 KingbaseES异构数据库移植指南 (3. KingbaseES移植能力支撑体系)
认识网络模型OSI模型
EasyCVR平台添加RTSP设备时,出现均以TCP方式连接的现象是什么原因?
软件测试面试题:已知一个字符串为“hello_world_yoyo”, 如何得到一个队列 [“hello“,“world“,“yoyo“]
SRE相关问题答疑
十年测试老鸟聊聊移动端兼容性测试
力扣解法汇总592-分数加减运算
[Numpy] 广播机制(Broadcast)