关于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那种列优先的方式!

numpy中的broadcast的更多相关文章

  1. numpy 中的broadcast 机制

    https://www.cnblogs.com/jiaxin359/p/9021726.html

  2. numpy 中的 broadcasting 理解

    broadcast 是 numpy 中 array 的一个重要操作. 首先,broadcast 只适用于加减. 然后,broadcast 执行的时候,如果两个 array 的 shape 不一样,会先 ...

  3. numpy中matrix的特殊属性

    一.matrix特殊属性解释 numpy中matrix有下列的特殊属性,使得矩阵计算更加容易 摘自 NumPy Reference Release 1.8.1 1.1 The N-dimensiona ...

  4. 在python&numpy中切片(slice)

     在python&numpy中切片(slice) 上文说到了,词频的统计在数据挖掘中使用的频率很高,而切片的操作同样是如此.在从文本文件或数据库中读取数据后,需要对数据进行预处理的操作.此时就 ...

  5. Numpy中Meshgrid函数介绍及2种应用场景

    近期在好几个地方都看到meshgrid的使用,虽然之前也注意到meshgrid的用法.但总觉得印象不深刻,不是太了解meshgrid的应用场景.所以,本文将进一步介绍Numpy中meshgrid的用法 ...

  6. [开发技巧]·Numpy中对axis的理解与应用

    [开发技巧]·Numpy中对axis的理解与应用 1.问题描述 在使用Numpy时我们经常要对Array进行操作,如果需要针对Array的某一个纬度进行操作时,就会用到axis参数. 一般的教程都是针 ...

  7. numpy中的随机数模块

    https://www.cnblogs.com/td15980891505/p/6198036.html numpy.random模块中提供啦大量的随机数相关的函数. 1 numpy中产生随机数的方法 ...

  8. Python numpy中矩阵的用法总结

    关于Python Numpy库基础知识请参考博文:https://www.cnblogs.com/wj-1314/p/9722794.html Python矩阵的基本用法 mat()函数将目标数据的类 ...

  9. numpy 中的reshape,flatten,ravel 数据平展,多维数组变成一维数组

    numpy 中的reshape,flatten,ravel 数据平展,多维数组变成一维数组 import numpy as np 使用array对象 arr1=np.arange(12).reshap ...

随机推荐

  1. js正则--验证6-12位至少包含数字、小写字母和大些字母中至少两种字符,

    var reg=/^((([a-z])+([0-9])+)|(([0-9])+([a-z])+)|(([A-Z])+([0-9])+)|(([0-9])+([A-Z])+)|(([a-z])+([A- ...

  2. python 练习 3

    #!/usr/bin/python # -*- coding: utf-8 -*- def z94(): #斐波那契数列 def filie(x): a,b,t=1,1,0 if x==1 or x= ...

  3. PHP 高并发、抢票、秒杀 解决方案

    对于抢票.秒杀这种业务,我说说自己对这种高并发的理解吧,这里提出个人认为比较可行的几个方案:方案一:使用队列来实现可以基于例如MemcacheQ等这样的消息队列,具体的实现方案这么表述吧比如有100张 ...

  4. CF---(452)A. Eevee

    A. Eevee time limit per test 1 second memory limit per test 256 megabytes input standard input outpu ...

  5. hdu5876 Sparse Graph(补图最短路 bfs)

    题目链接:hdu5876 Sparse Graph 详见代码.. #include<cstdio> #include<cstring> #include<algorith ...

  6. syslog简介——系统日志写入API

    一.简介 syslog是Linux系统默认的日志守护进程.默认的主配置文件和辅助配置文件分别是/etc/syslog.conf和/etc/sysconfig/syslog文件.通常,syslog 接受 ...

  7. php中PCRE正则表达式分隔符的使用

    转自:http://www.baiwar.com/post/the-use-of-php-pcre-regex-delimiter.html 在php5.3.0以前,PHP可使用两套正则表达式规则,一 ...

  8. js在mootools框架下的new Class

    首先,在HTML文件中引入mootools.js. mootools-more.js.mootools-core.js,然后就能使用mootools封装的一些特性. 几乎类似于面向对象. mootoo ...

  9. 注册并启动 Reporting Services SharePoint 服务

    在安装 SharePoint 之前已安装 Reporting Services SharePoint 模式.所以Reporting Services SharePoint 是不能正常使用的. 安装完S ...

  10. 存储过程Oracle学习(一)

    一.简介 存储过程:就是在数据库中创建的一段程序,供别人调用 .其实我感觉跟定义一个方法相似 二.无参存储过程 如下,经典的输出"Hello World"来入门存储过程 创建一个存 ...