Python Scientific Computing _numpy_ broadcast and subscript

Source: Internet
Author: User

Multi-dimensional array subscript

The subscript of multidimensional array is to use tuple to implement each dimension, if the tuple length is larger than the dimension, it will make an error, if small, then the default tuple is followed by: all Access is indicated;

If a subscript is not a tuple, it is converted to a tuple, and the conversion of lists and arrays differs during the conversion process.

A list is converted to a tuple of elements and then accessed (if not all integers, the storage area is not shared because the storage structure cannot be changed by changing the stride size); lidx=[[0],[1],[2]]; If you use LIDX to access an array, is converted to: ([0],[1],[2]), which is the element on the access (0,1,2);

An array is made up: the operation is transferred to a consistent length tuple, such as a three-dimensional array A:

Array ([[[[[0], 1, 2, 3, 4],
[5, 6, 7, 8, 9],
[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19]],

[[20, 21, 22, 23, 24],
[25, 26, 27, 28, 29],
[30, 31, 32, 33, 34],
[35, 36, 37, 38, 39]],

[[40, 41, 42, 43, 44],
[45, 46, 47, 48, 49],
[50, 51, 52, 53, 54],
[55, 56, 57, 58, 59]])

If you use an array to access: Aidx=np.array ([[[0],[1],[2]]):

Array ([[0],
[1],
[2]])

will be complete as follows: (AIDX,:,:), the result of the access is:

Array ([[[[[[] [[0, 1, 2, 3, 4],
[5, 6, 7, 8, 9],
[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19]],


[[[20, 21, 22, 23, 24],
[25, 26, 27, 28, 29],
[30, 31, 32, 33, 34],
[35, 36, 37, 38, 39]],


[[[40, 41, 42, 43, 44],
[45, 46, 47, 48, 49],
[50, 51, 52, 53, 54],
[55, 56, 57, 58, 59]]]

The resulting shape is the combination of the shape that accesses the subscript array and the shape of the original array: The original array shape is (3,4,5), the subscript array shape is: (3,1), the resulting array shape is: (3,1,4,5); the access array replaces the original array shape in the 0-axis position;

Equivalent to A (AIDX,:,:), if Access: A (:, Aidx,:), then the shape of the access should be (3,3,1,5), that is, the 1-axis position instead of the original array shape;

Broadcasting

Review the radio again, detailed rules see Essay: http://www.cnblogs.com/lyon2014/p/4696989.html

As in the above example, an array of A.shape = (3,4,5), if accessed using an array, the shape of the original array is substituted at the corresponding position, and if there are multiple arrays in the subscript and the shapes are inconsistent, the broadcasts are made, such as:

I0 = Np.array ([[[1,2,1],[0,1,0]])?, i0.shape= (2,3)

I1 = Np.array ([[[[[[0]],[[1]]]), i1.shape= (2,1,1)

I2 = Np.array ([[[[[[2,3,2]]]), i2.shape= (1,1,3)

According to the broadcasting rules, the I0 has less dimension than the maximum Dimension 3, and the complement 1 operation gets the new shape: (x-y), then compares the length of each axis:

1 2 3
2 1 1
1 1 3?
--------
2 2 3

The shape of the final result is (2,2,3) and satisfies the third rule, which can be calculated; The three arrays after broadcast (recorded as: Ind0,ind1,ind2) are:

IND0:

Array ([[[[1, 2, 1],
[0, 1, 0]],

[[1, 2, 1],
[0, 1, 0]])

IND1:

Array ([[[[0, 0, 0],
[0, 0, 0]],

[[1, 1, 1],
[1, 1, 1]])

Ind2:

Array ([[[[2, 3, 2],
[2, 3, 2]],

[[2, 3, 2],
[2, 3, 2]])

At this point, using the broadcast three array to access array A (I0,I1,I2), the resulting array shape should only be related to the subscript array, that is, after the broadcast array shape: (2,2,3);

If a uses one of the two arrays to access: A (:, I0,i1), since I0 and I1 are broadcast after the shape is (2,2,3), in the 1, 2 axes replace the shape of a array, the last obtained shape is: (3,2,2,3);

If a is accessed with two arrays, and two arrays are not contiguous: a (I0,:,I1), the second axis is the last dimension, that is, the shape should be the shape of the post-broadcast shape plus the slice axis: (2,2,3,4)

Boolean array as Subscript

When a Boolean array is used as subscript, it is equivalent to using nonzero () as the subscript after processing the tuple;

such as: B2 = Np.array ([[[True,false,true],[true,false,false]]) as subscript, equivalent to:

Np.nonzero (b2) =? (Array ([0, 0, 1]), array ([0, 2, 0])

A[B2] = A[np.nonzero (b2)] = a[Array ([0, 0, 1]), array ([0, 2, 0]),:?], the first two axes are accessed as tuples, the resulting shape is (3,), and the last accessed shape is: (3,5)

Python Scientific Computing _numpy_ broadcast and subscript

Related Article

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.