numpy.broadcast_arrays#
- numpy.broadcast_arrays(*args,subok=False)[source]#
Broadcast any number of arrays against each other.
- Parameters:
- *argsarray_likes
The arrays to broadcast.
- subokbool, optional
If True, then sub-classes will be passed-through, otherwisethe returned arrays will be forced to be a base-class array (default).
- Returns:
- broadcastedtuple of arrays
These arrays are views on the original arrays. They are typicallynot contiguous. Furthermore, more than one element of abroadcasted array may refer to a single memory location. If you needto write to the arrays, make copies first. While you can set the
writableflag True, writing to a single output value may end upchanging more than one location in the output array.Deprecated since version 1.17:The output is currently marked so that if written to, a deprecationwarning will be emitted. A future version will set the
writableflag False so writing to it will raise an error.
Examples
>>>importnumpyasnp>>>x=np.array([[1,2,3]])>>>y=np.array([[4],[5]])>>>np.broadcast_arrays(x,y)(array([[1, 2, 3], [1, 2, 3]]), array([[4, 4, 4], [5, 5, 5]]))
Here is a useful idiom for getting contiguous copies instead ofnon-contiguous views.
>>>[np.array(a)forainnp.broadcast_arrays(x,y)][array([[1, 2, 3], [1, 2, 3]]), array([[4, 4, 4], [5, 5, 5]])]