当前位置:网站首页>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 """
边栏推荐
- Why should programmers learn microservice architecture if they want to enter a large factory?
- First + only! Alibaba cloud's real-time computing version of Flink passed the stability test of big data products of the Institute of ICT
- C language self-made Games: Sanzi (tic tac toe chess) intelligent chess supplement
- Market status and development prospects of the global IOT active infrared sensor industry in 2022
- [BMZCTF-pwn] 18-RCTF-2017-Recho
- After job hopping at the end of the year, I interviewed more than 30 companies in two weeks and finally landed
- 2022 registration of G2 utility boiler stoker examination and G2 utility boiler stoker reexamination examination
- [USACO 2009 Dec S]Music Notes
- 【工具跑SQL盲注】
- [set theory] binary relationship (binary relationship notation | binary relationship from a to B | number of binary relationships | example of binary relationship)
猜你喜欢

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

Leetcode simple problem delete an element to strictly increment the array

C language self-made Games: Sanzi (tic tac toe chess) intelligent chess supplement

Analysis of proxy usage of ES6 new feature

2022 a special equipment related management (elevator) analysis and a special equipment related management (elevator) simulation test

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

论文阅读_中文医疗模型_ eHealth

Internationalization and localization, dark mode and dark mode in compose
![[XSS bypass - protection strategy] understand the protection strategy and better bypass](/img/72/d3e46a820796a48b458cd2d0a18f8f.png)
[XSS bypass - protection strategy] understand the protection strategy and better bypass

Leetcode simple question: check whether the array is sorted and rotated
随机推荐
MediaTek 2023 IC written examination approved in advance (topic)
Internationalization and localization, dark mode and dark mode in compose
[PHP vulnerability weak type] basic knowledge, PHP weak equality, error reporting and bypassing
[USACO 2009 Dec S]Music Notes
[PCL self study: filtering] introduction and use of various filters in PCL (continuously updated)
STM32 reverse entry
2022 chemical automation control instrument examination summary and chemical automation control instrument certificate examination
Current market situation and development prospect forecast of the global fire boots industry in 2022
Day 51 - tree problem
First + only! Alibaba cloud's real-time computing version of Flink passed the stability test of big data products of the Institute of ICT
[SQL injection] joint query (the simplest injection method)
论文阅读_中文医疗模型_ eHealth
Number of 1 in binary (simple difficulty)
ZABBIX monitoring of lamp architecture (3): zabbix+mysql (to be continued)
JDBC database operation
stm32逆向入门
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
I've been in software testing for 8 years and worked as a test leader for 3 years. I can also be a programmer if I'm not a professional
Number of uniform strings of leetcode simple problem
2022 registration examination for safety production management personnel of hazardous chemical production units and examination skills for safety production management personnel of hazardous chemical