当前位置:网站首页>Notes | numpy-09 Broadcast
Notes | numpy-09 Broadcast
2022-07-03 04:49:00 【CY3761】
#%%
# radio broadcast (Broadcast) yes numpy For different shapes (shape) How to do the numerical calculation of the array , Arithmetic operations on arrays are usually performed on the corresponding elements
# If two arrays a and b The same shape , The meet a.shape == b.shape, that a*b The result is that a And b Array corresponding bit multiplication . This requires the same dimension , And the length of each dimension is the same
#%%
import numpy as np
a = np.array([1, 2, 3, 4]) # 4r1c
a
#%%
b = np.array([10, 20, 30, 40]) # 4r1c
b
#%%
c = a * b
c
#%%
# When in operation 2 The shapes of arrays are different ,numpy Will automatically trigger the broadcast mechanism
import numpy as np
a = np.array([
[0, 0, 0],
[10, 10, 10],
[20, 20, 20],
[30, 30, 30]
])
a # 4r3c
#%%
b = np.array([1, 2, 3])
b # 1r3c
#%%
""" a Row index 0 0+1=1 0+2=2 0+3=3 a Row index 1 10+1=11 10+2=12 10+3=13 a Row index 2 20+1=21 20+2=22 20+3=23 a Row index 3 30+1=31 30+2=32 30+3=33 """
a + b # take b Bring in the a | a Column correspondence of b The line of | When calculating, it will be calculated in sequence with the same number of edges
#%%
bb = np.tile(b, (4, 1)) # repeat b Dimensions of | 1 Repeat in the vertical direction 2 Repeat horizontally | 4 repeat 4 Time
bb
#%%
a + bb # Equate to a + b
#%%
""" The rules of broadcasting : Let all the input arrays look to the array with the longest shape , The insufficient parts in the shape are all preceded by 1 A filling . The shape of the output array is the maximum value on each dimension of the shape of the input array . If a dimension of the input array has the same length as the corresponding dimension of the output array, or its length is 1 when , This array can be used to calculate , Otherwise mistakes . When the length of a dimension of the input array is 1 when , The first set of values on this dimension is used when calculating along this dimension Simple understanding : For two arrays , Compare each of their dimensions ( Ignore if one of the arrays has no current dimension ), Satisfy : Arrays have the same shape . The value of the current dimension is equal . One of the values of the current dimension is 1. If the conditions are not met , Throw out "ValueError: frames are not aligned" abnormal """
边栏推荐
- MC Layer Target
- Market status and development prospect prediction of global colorimetric cup cover industry in 2022
- Priv app permission exception
- Learn to use the idea breakpoint debugging tool
- [BMZCTF-pwn] 18-RCTF-2017-Recho
- Priv-app permission异常
- 【SQL注入点】注入点出现位置、判断
- FISCO bcos zero knowledge proof Fiat Shamir instance source code
- Market status and development prospect forecast of global heat curing adhesive industry in 2022
- On typescript and grammar
猜你喜欢

《牛客刷verilog》Part II Verilog进阶挑战

Retirement plan fails, 64 year old programmer starts work again

Why should programmers learn microservice architecture if they want to enter a large factory?
![[luatos sensor] 1 light sensing bh1750](/img/70/07f29e072c0b8630f92ec837fc12d5.jpg)
[luatos sensor] 1 light sensing bh1750

ZABBIX monitoring of lamp architecture (3): zabbix+mysql (to be continued)

Learning practice: comprehensive application of cycle and branch structure (I)

The simple problem of leetcode: dismantling bombs

Employee attendance management system based on SSM

Pyqt control part (II)

Auman Galaxy new year of the tiger appreciation meeting was held in Beijing - won the double certification of "intelligent safety" and "efficient performance" of China Automotive Research Institute
随机推荐
Number of uniform strings of leetcode simple problem
[set theory] binary relationship (binary relationship notation | binary relationship from a to B | number of binary relationships | example of binary relationship)
Market status and development prospects of the global IOT active infrared sensor industry in 2022
stm32逆向入门
2022 t elevator repair simulation examination question bank and t elevator repair simulation examination question bank
带有注意力RPN和多关系检测器的小样本目标检测网络(提供源码和数据及下载)...
I stepped on a foundation pit today
Market status and development prospect prediction of global fermentation acid industry in 2022
Market status and development prospect prediction of the global fire alarm sensor industry in 2022
Web security - CSRF (token)
Shell script -- condition judgment
Market status and development prospect prediction of the global forward fluorescent microscope industry in 2022
Analysis of proxy usage of ES6 new feature
雇佣收银员(差分约束)
The programmer resigned and was sentenced to 10 months for deleting the code. JD came home and said that it took 30000 to restore the database. Netizen: This is really a revenge
Shuttle + Alluxio 加速内存Shuffle起飞
Market status and development prospect prediction of global neutral silicone sealant industry in 2022
ZABBIX monitoring of lamp architecture (2): ZABBIX basic operation
FISCO bcos zero knowledge proof Fiat Shamir instance source code
Thesis reading_ Tsinghua Ernie