Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use broadcasts in NumPy

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "how to use broadcast in NumPy". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to use broadcast in NumPy".

Basic broadcast

Normally, two arrays need to be operated, so the object of each array needs to have a corresponding value to calculate. For example, the following example:

A = np.array ([1.0,2.0,3.0]) b = np.array ([2.0,2.0,2.0]) a * barray ([2.0,4.6])

But if you use the broadcast feature of Numpy, you don't have to have an exact number of elements.

For example, we can talk about an array multiplied by a constant:

A = np.array ([1.0,2.0,3.0]) > > b = 2.0,3.0 > > a * barray ([2.,4.,6.])

The following example is equivalent to the above example, and Numpy automatically extends b.

NumPy is smart enough to use raw scalar values without actually making copies, so that broadcast operations save as much memory and improve computational efficiency as possible.

The code in the second example is more efficient than the code in the first example because the broadcast moves less memory during multiplication (b is a scalar rather than an array).

Broadcast rules

If two arrays operate, NumPy compares the objects of the two arrays. Starting from the last dimension, if the dimensions of the two arrays meet the following two conditions, we consider the two arrays to be compatible and can be operated:

The number of elements in the dimension is the same.

One of the dimensions is 1.

If the above two conditions are not met, an exception is thrown: ValueError: operands could not be broadcast together.

Just because the number of elements in a dimension is the same does not mean that two arrays are required to have the same number of dimensions.

For example, a 256x256x3 array representing colors can be multiplied by an one-dimensional array of three elements:

Image (3D array): 256 x 256 x 3Scale (1d array): 3Result (3D array): 256 x 256 x 3

When multiplying, the number of elements in a dimension that is 1 is stretched to match the number of elements in another dimension:

A (4d array): 8 x 1 x 6 x 1B (3D array): 7 x 1 x 5Result (4d array): 8 x 7 x 6 x 5

In the above example, 1 of the second dimension is stretched to 7, 1 of the third dimension is stretched to 6, and 1 of the fourth dimension is stretched to 5.

There are more examples:

B (1D array): 1Result (2d array): 5 x 4A (2d array): 5 x 4B (1D array): 4Result (2d array): 5 x 4A (3D array): 15 x 3 x 5B (3D array): 15 x 1 x 5Result (3D array): 15 x 3 x 5A (3D array): 15 x 3 x 5B (2d array): 3 X 5Result (3D array): 15 x 3 x 5A (3D array): 15 x 3 x 5B (2d array): 3 x 1Result (3D array): 15 x 3 x 5

Here is an example of a mismatch:

A (1D array): 3B (1D array): 4 # trailing dimensions do not matchA (2d array): 2 x 1B (3D array): 8 x 4 x 3 # second from last dimensions mismatched

Take another example of the actual code:

> > x = np.arange (4) > xx = x.reshape (4) > y = np.ones (5) > > z = np.ones ((3)) > x.shape (4,) > > y.shape (5,) > > x + yValueError: operands could not be broadcast together with shapes (4,) (5,) > > xx.shape (4) > y.shape (5,) > > (xx + y) .shape (4,5) > > xx + yarray ([[1.1,1.1,1.1,1. 1.], [2, 2, 2, 2.], [3, 3, 3, 3.], [4, 4, 4, 4]) > x.shape (4,) > > z.shape (3, 4) > (x + z) .shape (3, 4) > > x + zarray ([[1, 2, 3. 4.], [1, 2, 3, 4.], [1, 2, 3, 4.])

The broadcast also provides a very convenient operation for the external product of two 1-dimensional arrays:

> a = np.array ([0.0,10.0,20.0,30.0]) > > b = np.array ([1.0,2.0,3.0]) > a [:, np.newaxis] + barray ([[1.1,2.3,3.], [11.12.13.], [21.22.23], [31.32.33.])

Where a [:, np.newaxis] converts a 1-dimensional array into a 4-dimensional array:

In: a [:, np.newaxis] Out: array ([[0.], [10.], [20.], [30.]]) Thank you for your reading, the above is the content of "how to use broadcast in NumPy". After the study of this article, I believe you have a deeper understanding of how to use broadcast in NumPy, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report