The broadcasting mechanism of numpy two-yuan operation

Source: Internet
Author: User

NumPy has a very convenient feature: broadcasting. When we do binary computations (such as add and multiply) of two different lengths of numpy arrays, broadcasting work silently behind their backs. This article we will introduce the next NumPy broadcasting.

What is broadcasting

Let's use a simple example to get to know broadcasting, and consider the following code

Import NumPy as NP

A = Np.array ([0, 1, 2])

b = Np.array ([5, 5, 5])

c = A + b

The a+b is actually adding each pair of elements in the same position in array A and group B. Here A and B are arrays of the same length.

What if it's an array of different lengths? Consider the following situation

D = A + 5

This is where the broadcasting is used. Broadcasting will extend 5 to [5, 5, 5], and the code above will be added to the two-length array. Drawing out, is a process like this (the translucent squares represent the values that are extended)

It should be noted that broadcasting does not allocate additional memory to access the replicated data, which is simplified to describe convenience.

Let's expand the example above to look at the multidimensional array

E = Np.ones ((3, 3))

# E is

# Array (

# [[1., 1., 1.],

# [1., 1., 1.],

# [1., 1., 1.]]

E + A

# Array ([

# [1., 2., 3.],

# [1., 2., 3.],

# [1., 2., 3.]]

Here one-dimensional array A is extended to a two-dimensional array, the same as the shape of E. In the form of graphs, this is the case

Let's consider a more complex case where we need to do a broadcasting of two arrays.

b = Np.arange (3). Reshape ((3, 1))

# b is

# Array ([

# [0],

# [1],

# [2]]

B + A

# Array ([

# [0, 1, 2],

# [1, 2, 3],

# [2, 3, 4]]

Here A and B are expanded into two-dimensional arrays of the same shape. This process is represented in the form of a graph, as follows

The rules of the broadcasting

For binary calculations between two numpy arrays, the broadcasting must follow the rules:

1. If two array dimensions are not equal, the shape of an array with a lower dimension will fill 1 from the left, until the dimension of the high-dimensional array is matched to 2, if the two array dimensions are the same, but the lengths of some of the dimensions are different, then the dimension of 1 is extended to match the length of the same dimension of the other array 3, If the two-array dimension is the same, but the length of any dimension is different and not 1, the error

Let's illustrate the rules above.

Example 1

A = Np.arange (3)

b = Np.ones ((2, 3))

The shape of these two arrays is

A.shape = (3,)

B.shape = (2, 3)

The two arrays are calculated as binary, and according to Rule 1, the array is filled

A.shape-> (1, 3)

B.shape-> (2, 3)

According to Rule 2, the first dimension is unequal, so we extend the dimension

A.shape-> (2, 3)

B.shape-> (2, 3)

Now the shape of the two arrays is identical and can be added to the following results

A + b

# Array ([

# [1., 2., 3.],

# [1., 2., 3.]]

Example 2

A = Np.arange (3). Reshape ((3, 1))

b = Np.arange (3)

The shape of the two arrays is

A.shape = (3, 1)

B.shape = (3,)

According to the rule 1,b shape is to be filled

A.shape-> (3, 1)

B.shape-> (1, 3)

According to rule 2, the dimensions are equal, but the dimensions are unequal in length, so further expansion is required

A.shape-> (3, 3)

B.shape-> (3, 3)

Now that the shape of the two is identical, the result of the addition calculation can be as follows

A + b

# Array ([

# [0, 1, 2],

# [1, 2, 3],

# [2, 3, 4]]

Example 3

Let's look at an example of a broadcasting error.

b = Np.ones ((3, 2))

A = Np.arange (3)

The shape of the two arrays is

B.shape = (3, 2)

A.shape = (3,)

According to the rule, the shape of the 1,a will be filled

B.shape-> (3, 2)

A.shape-> (1, 3)

According to Rule 2, the first dimension of array A is extended

B.shape-> (3, 2)

A.shape-> (3, 3)

Here we meet the conditions of rule 3, the number of dimensions is equal, but the second dimension is different in length and not 1, so the two arrays add an error, as follows

B + A

# output

ValueError Traceback (most recent call last)

<ipython-input-30-15a3d2288d92> in <module> ()

----> 1 B + A

Valueerror:operands could not to broadcast together with Shapes (3,2) (3,)

Summarize

Broadcasting is ubiquitous in the computation of numpy arrays, and the ufunc of any binary operation realizes the broadcasting mechanism. Broadcasting is also very convenient, many times we can not even perceive its existence, but the deep understanding of the working mechanism behind it will help us avoid some traps.

Commend the author


The author of this article

Brother Keung, author of the Python Chinese community columnist, worked for Morgan Stanley and ebay. Knowledge Column: Python and data analysis. ❈

Click to read the text to join the Codinggo programming community,

Learn programming knowledge and gain Internet connections.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.