当前位置:网站首页>Numpy array broadcast rule memory method array broadcast broadcast principle broadcast mechanism
Numpy array broadcast rule memory method array broadcast broadcast principle broadcast mechanism
2022-07-24 05:51:00 【Ml -- xiaoxiaobai】
This article focuses on the text description , Just look at the code , If my method is of little help to you , Please give me a little praise , If there is a better way , Or discover my mistakes , Please grant me your advice
First , In fact, the operation between array and scalar is actually a kind of broadcast first , after element-wise Arithmetic
import numpy as np
arr = np.arange(5)
arr
array([0, 1, 2, 3, 4])
arr * 4
array([ 0, 4, 8, 12, 16])
The rule of broadcasting is each end dimension , The shaft length matches or the length is 1, The broadcast will be on the lost axis , such as (4, 3) + (3,) The following array has the same axis length at the end of the compound , Will broadcast a missing axis ; perhaps , The broadcast has a shaft length of 1 On the axis , such as (4, 3) + (1, 3), The shaft 0 from 1 Broadcast for 4. For array and scalar operations , In fact, it also uses radio , such as (4, 3) + scale, among scale Of shape In fact, it can be considered that (1,), Then the end length is 1, On the radio , The dimension at the end is broadcast as 3, The missing axis is broadcast as 4.
Broadcast can be performed in both arrays , such as (4, 4) + (4, 1, 4), First, the shaft length at the end is consistent , Secondly, one of the inconsistent shaft lengths is 1, that 1 The broadcast became 4, In addition, the missing axis broadcast is 4.
Based on this rule , Sometimes I want to calculate (4, 3) + (4, 1), In fact, the latter is (4,) When , Because the end shaft length is not 1, and 3 And 4 Don't match , Therefore, it is not possible to broadcast , Must pass reshape, perhaps [:, None] The way to increase the coordinate axis , Or make use of np.newaxis
therefore , In fact, grasping the end axis length of two arrays is the key , At first glance, the shaft length is neither 1, It's not the same , Then don't think about broadcasting , Let's see how to write circular operation .
arr = np.random.randn(4, 3)
arr.mean(0)
array([ 0.27783846, 0.36009253, -0.1499029 ])
demeaned = arr - arr.mean(0)
demeaned
array([[-0.79969385, -1.6011334 , -0.00747013],
[-0.0381061 , 0.64865496, -0.97992594],
[ 1.13694786, 0.81091045, 0.73967573],
[-0.29914791, 0.14156799, 0.24772034]])
arr.shape, arr.mean(0).shape
((4, 3), (3,))
aaa = np.array([1])
aaa.shape
(1,)
ans = arr - aaa
arr - ans
array([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]])
arr.shape, aaa.shape
((4, 3), (1,))
arr.shape
(4, 3)
arr.mean(1).shape
(4,)
arr - arr.mean(1)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-18-8b8ada26fac0> in <module>
----> 1 arr - arr.mean(1)
ValueError: operands could not be broadcast together with shapes (4,3) (4,)
arr - arr.mean(1).reshape(-1, 1)
array([[ 0.11823437, -0.6009511 , 0.48271673],
[ 0.20018202, 0.96919716, -1.16937918],
[ 0.35626561, 0.11248227, -0.46874788],
[-0.21403229, 0.30893769, -0.0949054 ]])
arr - arr.mean(1)[:, None]
array([[ 0.11823437, -0.6009511 , 0.48271673],
[ 0.20018202, 0.96919716, -1.16937918],
[ 0.35626561, 0.11248227, -0.46874788],
[-0.21403229, 0.30893769, -0.0949054 ]])
arr - arr.mean(1)[:, np.newaxis]
array([[ 0.11823437, -0.6009511 , 0.48271673],
[ 0.20018202, 0.96919716, -1.16937918],
[ 0.35626561, 0.11248227, -0.46874788],
[-0.21403229, 0.30893769, -0.0949054 ]])
A three-dimensional example :
arr = np.ones((4, 4))
arr_3d = arr[:, np.newaxis, :]
arr.shape, arr_3d.shape
((4, 4), (4, 1, 4))
arr + arr_3d
array([[[2., 2., 2., 2.],
[2., 2., 2., 2.],
[2., 2., 2., 2.],
[2., 2., 2., 2.]],
[[2., 2., 2., 2.],
[2., 2., 2., 2.],
[2., 2., 2., 2.],
[2., 2., 2., 2.]],
[[2., 2., 2., 2.],
[2., 2., 2., 2.],
[2., 2., 2., 2.],
[2., 2., 2., 2.]],
[[2., 2., 2., 2.],
[2., 2., 2., 2.],
[2., 2., 2., 2.],
[2., 2., 2., 2.]]])
A common pattern , such as , Subtract / Remove the sum of certain axes / variance / Mean value or something :
arr = np.random.randn(3, 4, 5)
# Adding is called subtracting 1 The mean value of the axis
means = arr.mean(1)
means
array([[-0.95808939, -0.59395877, 0.44605451, 0.06325242, 0.14369531],
[ 0.2600657 , -0.92595688, -0.75528343, -0.2486933 , -0.02936524],
[-0.22052564, 0.14549496, -0.67660057, -0.10151047, 0.26275483]])
arr.shape, means.shape
((3, 4, 5), (3, 5))
demeaned = arr - means[:, np.newaxis, :]
demeaned.mean(1) < 1e-16
array([[ True, True, True, True, True],
[ True, True, True, True, True],
[ True, True, True, True, True]])
# It can be written as a function
def demean_axis(arr, axis=0):
means = arr.mean(axis)
indexer = [slice(None)] * arr.ndim
indexer[axis] = np.newaxis
return arr - means[indexer]
arr = np.random.randn(3, 4, 5)
demeaned = demean_axis(arr, axis=1)
demeaned.mean(1) < 1e-16
<ipython-input-45-8051ed80feee>:6: FutureWarning: Using a non-tuple sequence for multidimensional indexing is deprecated; use `arr[tuple(seq)]` instead of `arr[seq]`. In the future this will be interpreted as an array index, `arr[np.array(seq)]`, which will result either in an error or a different result.
return arr - means[(indexer)]
array([[ True, True, True, True, True],
[ True, True, True, True, True],
[ True, True, True, True, True]])
In fact, array assignment also uses broadcasting :
arr = np.zeros((4, 3))
col = np.array([1.28, 0, 33, 0.5])
arr[:] = col[:, np.newaxis]
arr
array([[ 1.28, 1.28, 1.28],
[ 0. , 0. , 0. ],
[33. , 33. , 33. ],
[ 0.5 , 0.5 , 0.5 ]])
arr[:2] = [[2], [3]]
arr
array([[ 2. , 2. , 2. ],
[ 3. , 3. , 3. ],
[33. , 33. , 33. ],
[ 0.5, 0.5, 0.5]])
边栏推荐
- 多商户商城系统功能拆解05讲-平台端商家主营类目
- 【activiti】个人任务
- Authorized access to MySQL database
- 【activiti】组任务
- 如何快速打通CRM系统和ERP系统,实现业务流程自动化流转
- 《统计学习方法(第2版)》李航 第16章 主成分分析 PCA 思维导图笔记 及 课后习题答案(步骤详细)PCA 矩阵奇异值 第十六章
- [activiti] activiti introduction
- Chapter IV decision tree summary
- 对ArrayList<ArrayList<Double>>排序
- 多商户商城系统功能拆解06讲-平台端商家入驻协议
猜你喜欢

Delete the weight of the head part of the classification network pre training weight and modify the weight name

删除分类网络预训练权重的的head部分的权重以及修改权重名称

多商户商城系统功能拆解14讲-平台端会员等级

多商户商城系统功能拆解13讲-平台端会员管理

likeshop单商户商城系统搭建,代码开源无加密

LSTM神经网络

"Statistical learning methods (2nd Edition)" Li Hang Chapter 16 principal component analysis PCA mind map notes and after-school exercise answers (detailed steps) PCA matrix singular value Chapter 16

程序员常说的API是什么意思?API类型有什么呢?

Multi merchant mall system function disassembly lecture 09 - platform end commodity brands

The way to attack the first poca hackson project "Manta network"
随机推荐
对接CRM系统和效果类广告,助力企业精准营销助力企业精准营销
Recommend a fully open source, feature rich, beautiful interface mall system
Sqlserver completely deleted
【activiti】activiti环境配置
"Statistical learning methods (2nd Edition)" Li Hang Chapter 13 introduction to unsupervised learning mind map notes
Multi merchant mall system function disassembly lesson 03 - platform side merchant management
对ArrayList<ArrayList<Double>>排序
多商户商城系统功能拆解04讲-平台端商家入驻
多商户商城系统功能拆解05讲-平台端商家主营类目
Delete the weight of the head part of the classification network pre training weight and modify the weight name
Multi merchant mall system function disassembly lecture 08 - platform end commodity classification
Use streaming media to transfer RSTP to the Web terminal for playback (II) [review]
Loss after cosine annealing decay of learning rate
多商户商城系统功能拆解10讲-平台端商品单位
LSTM神经网络
[activiti] activiti system table description
多商户商城系统功能拆解03讲-平台端商家管理
多商户商城系统功能拆解11讲-平台端商品栏目
使用bat命令快速创建系统还原点的方法
传统的k-means实现