当前位置:网站首页>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]])
边栏推荐
- 第五章神经网络
- 【activiti】组任务
- OSError: [WinError 127] 找不到指定的程序。Error loading “caffe2_detectron_ops.dll“ or one of its dependencies
- Are you still trying to limit MySQL paging?
- "Statistical learning methods (2nd Edition)" Li Hang Chapter 13 introduction to unsupervised learning mind map notes
- 《统计学习方法(第2版)》李航 第十三章 无监督学习概论 思维导图笔记
- jupyter notebook一直自动重启(The kernel appears to have died. It will restart automatically.)
- [activiti] Introduction to activiti
- How to quickly connect CRM system and ERP system to realize the automatic flow of business processes
- Delete the weight of the head part of the classification network pre training weight and modify the weight name
猜你喜欢

"Statistical learning methods (2nd Edition)" Li Hang Chapter 13 introduction to unsupervised learning mind map notes

【activiti】activiti介绍
![[activiti] activiti introduction](/img/17/bd8f6fd8dd8918a984ca0a3793ec0b.jpg)
[activiti] activiti introduction

Chapter IV decision tree summary

Inventory of well-known source code mall systems at home and abroad

解决ModularNotFoundError: No module named “cv2.aruco“

学习率优化策略

Watermelon book / Pumpkin book -- Chapter 1 and 2 Summary

多商户商城系统功能拆解06讲-平台端商家入驻协议

What do programmers often mean by API? What are the API types?
随机推荐
多商户商城系统功能拆解12讲-平台端商品评价
Points for attention in adding spp module to the network
jupyter notebook一直自动重启(The kernel appears to have died. It will restart automatically.)
Inventory of well-known source code mall systems at home and abroad
第四章 决策树总结
Likeshop | single merchant mall system code open source no encryption -php
世界坐标系、相机坐标系和图像坐标系的转换
Canal+kafka实战(监听mysql binlog实现数据同步)
《信号与系统》(吴京)部分课后习题答案与解析
详谈数据同步工具ETL、ELT,反向ETL
Use streaming media to transfer RSTP to the Web terminal for playback (II) [review]
ERP+RPA 打通企业信息孤岛,企业效益加倍提升
tensorflow和pytorch框架的安装以及cuda踩坑记录
测试数据增强后标签和数据集是否对应
删除分类网络预训练权重的的head部分的权重以及修改权重名称
[activiti] activiti environment configuration
《统计学习方法(第2版)》李航 第17章 潜在语义分析 LSA LSI 思维导图笔记 及 课后习题答案(步骤详细)第十七章
【activiti】activiti介绍
"Statistical learning methods (2nd Edition)" Li Hang Chapter 13 introduction to unsupervised learning mind map notes
Likeshop100%开源无加密-B2B2C多商户商城系统