关于broadcast,官方文档描述如下:

Each universal function takes array inputs and produces array outputs by performing the core function element-wise
on the inputs. Standard broadcasting rules are applied so that inputs not sharing exactly the same shapes can still be
usefully operated on. Broadcasting can be understood by four rules:
1. All input arrays with ndim smaller than the input array of largest ndim, have 1’s prepended to their shapes.
2. The size in each dimension of the output shape is the maximum of all the input sizes in that dimension.
3. An input can be used in the calculation if its size in a particular dimension either matches the output size in that
dimension, or has value exactly 1.
4. If an input has a dimension size of 1 in its shape, the first data entry in that dimension will be used for all
calculations along that dimension. In other words, the stepping machinery of the ufunc will simply not step
along that dimension (the stride will be 0 for that dimension).
Broadcasting is used throughout NumPy to decide how to handle disparately shaped arrays; for example, all arith-
metic operations (+, -, * , ...) between ndarrays broadcast the arrays before operation. A set of arrays is called
“broadcastable” to the same shape if the above rules produce a valid result, i.e., one of the following is true:
1. The arrays all have exactly the same shape.
2. The arrays all have the same number of dimensions and the length of each dimensions is either a common length
or 1.
3. The arrays that have too few dimensions can have their shapes prepended with a dimension of length 1 to satisfy
property 2.

Example
If a.shape is (5,1), b.shape is (1,6), c.shape is (6,) and d.shape is () so that d is a scalar, then a, b, c, and d
are all broadcastable to dimension (5,6); and
• a acts like a (5,6) array where a[:,0] is broadcast to the other columns,
• b acts like a (5,6) array where b[0,:] is broadcast to the other rows,
• c acts like a (1,6) array and therefore like a (5,6) array where c[:] is broadcast to every row, and finally,

这里面对于形状的描述都是很完整的,但是有时候我们也见到这样的定义
a = np.zeros((2,))

print(a)

array([0.,0.0])

注意只有一个中括号,但是我们定义

a = np.zeros((2,1))的时候

print(a)

array([[0,],[0.]])

默认情况下,a = np.zeros((2,))定义的是一个向量,它的形状跟(2,1)是不一样的,要转型的话,默认是转成(1,2)的!!!

numpy的数组存储默认是跟C 语言一样,行优先的,所以向量默认是行向量,也可以修改成FORTRAN那种列优先的方式!

转载于:https://www.cnblogs.com/hustxujinkang/p/4631936.html

numpy中的broadcast相关推荐

  1. numpy中矩阵运算的特点

    简 介: 在numpy中的一维和二维数组与线性代数中的矩阵和向量的概念有区别,也有联系.恰当掌握numpy中的矩阵运算特点可以大大提高程序的编写的效率.这其中需要不断的做斗争的就是区分一维向量与一维矩 ...

  2. numpy中where函数的用法

    numpy中where函数的用法 numpy.where(condition,x,y)--若满足condition,输出x,否则输出y. 举例: 例1:一维数组 >>> a = np ...

  3. 总结numpy中的ndarray,非常齐全

    公众号后台回复"图书",了解更多号主新书内容 来源:Python碎片 作者:binn.wong numpy(Numerical Python)是一个开源的Python数据科学计算库 ...

  4. python花式索引_初探Numpy中的花式索引

    Numpy中对数组索引的方式有很多(为了方便介绍文中的数组如不加特殊说明指的都是Numpy中的ndarry数组),比如:基本索引:通过单个整数值来索引数组 import numpy as np arr ...

  5. python创建列向量_关于Numpy中的行向量和列向量详解

    关于Numpy中的行向量和列向量详解 行向量 方式1 import numpy as np b=np.array([1,2,3]).reshape((1,-1)) print(b,b.shape) 结 ...

  6. Numpy中矩阵运算

    Numpy中矩阵运算 1 矩阵和向量 1.1 矩阵 矩阵,英文matrix,和array的区别矩阵必须是2维的,但是array可以是多维的. 如图:这个是 3×2 矩阵,即 3 行 2 列,如 m 为 ...

  7. Numpy中数组间运算

    Numpy中数组间运算 1 数组与数的运算     [可以直接进行运算] arr = np.array([[1, 2, 3, 2, 1, 4], [5, 6, 1, 2, 3, 1]]) arr + ...

  8. Numpy 中的 arange 函数

    1. 概述 Numpy 中 arange() 主要是用于生成数组,具体用法如下: 2. arange() 2.1 语法 numpy.arange(start, stop, step, dtype = ...

  9. python使用numpy中的flatten函数将2D numpy数组拉平为1Dnumpy数组、使用np.linalg.matrix_rank函数计算2D numpy数组的秩(rank)

    python使用numpy中的flatten函数将2D numpy数组拉平为1Dnumpy数组.使用np.linalg.matrix_rank函数计算2D numpy数组的秩(rank) 目录

  10. python使用numpy中的np.mean函数计算数组的均值、np.var函数计算数据的方差、np.std函数计算数组的标准差

    python使用numpy中的np.mean函数计算数组的均值.np.var函数计算数据的方差.np.std函数计算数组的标准差 目录

最新文章

  1. 子分类账知识学习(汇总网上比较有用的资料)
  2. java学习笔记—校验码的实现(15)
  3. sas univariate 结果解释_PROC UNIVARIATE过程
  4. linux安装joomla,安装Joomla
  5. [Leetcode] 第306题 累加数
  6. mysql 表连接 on_MysQL中表连接中“using”和“on”之间的区别是什么?
  7. 二 关键词---关键词的选择(二)
  8. BZOJ 1146 网络管理Network(树链剖分+BST)
  9. MSI Afterburner 官网链接指南
  10. X4扭曲字体或图形 coreldraw_cdrx4精简版下载|coreldraw x4 sp2 精简版下载增强版 15.2.3.1614 - 系统天堂...
  11. CCS导入工程时报错“overlaps the location of another project”解决办法
  12. 2017C语言程序设计预备作业
  13. 盒模型——快递比喻法
  14. CSS颜色属性、文本文字属性、属性继承
  15. opengl绘制位图字体c语言源代码,使用OpenGL位图字体将文本放到屏幕上
  16. 使用阿里云服务器发送邮件
  17. 软件工程知识点总结——第三、四部分
  18. RRDTool和mrtg的比较
  19. 重置SMC和NVRAM解决MacBookPro卡顿问题
  20. c#样条曲线命令_C# chart控件绘制曲线

热门文章

  1. Using Delegates with Data Readers to Control DAL Responsibility[转]
  2. Asp.net Web控件自定义类属性(经验篇)
  3. 图解自监督学习,从入门到专家必读的九篇论文
  4. 【领域综述】NLP领域,你推荐哪些综述性的文章?
  5. 【图神经网络】从源头探讨 GCN 的行文思路
  6. “不怕的人的面前才有路”!一名双非硕士的985攻博历程
  7. 【干货】python正则表达式应用笔记
  8. 搭建Hadoop集群(二)
  9. 使用UE去除复制文本中的空格、换行符和TAB
  10. 获取Element UI中button组件的ID值