A Concise NumPy tutorial --- array 3 (combination)

Source: Internet
Author: User
This article discusses NumPy arrays in depth. First, we will introduce the array of the custom type, then the combination of arrays, and finally the issues concerning array replication. if you are interested, please take a look. The first two articles give a basic introduction to the NumPy array. This article discusses the NumPy array in depth. First, we will introduce the array of the custom type, then the combination of arrays, and finally the problems of array replication.

Custom structure array

NumPy can also be used to define structure types like C. The method for defining the structure in NumPy is as follows:

Define the structure type name, define the field name, and indicate the field data type.

student= dtype({'names':['name', 'age', 'weight'], 'formats':['S32', 'i','f']}, align = True)

Student is the name of the custom structure type, which is created using the dtype function. in the first parameter, 'names' and 'formats' cannot be changed, names lists the field names in the structure, and formats lists the data types of the corresponding fields. S32 represents a 32-byte string, I represents a 32-bit integer, and f represents a 32-bit floating point. If the last parameter is True, memory alignment is required.

The field uses the NumPy character encoding to represent the data type. For more detailed data types, see the table below.


Data type Character encoding
Integer I
Unsigned integer U
Single-precision floating point number F
Double-precision floating point number D
Boolean value B
Plural D
String S
Unicode U
Void V

After defining the structure type, you can define an array with this type as the element:

a= array([(“Zhang”, 32, 65.5), (“Wang”, 24, 55.2)], dtype =student)

In addition to listing the data of the corresponding field in sequence in each element, you also need to specify the corresponding data type in the last parameter of the array function.

Composite Functions

This section describes how to combine functions in different ways. First, create two arrays:

>>> a = arange(9).reshape(3,3) >>> a array([[0, 1, 2],    [3, 4, 5],    [6, 7, 8]]) >>> b = 2 * a >>> b array([[ 0, 2, 4],   [ 6, 8, 10],   [12, 14, 16]])

Horizontal combination

>>> hstack((a, b)) array([[ 0, 1, 2, 0, 2, 4],   [ 3, 4, 5, 6, 8, 10],   [ 6, 7, 8, 12, 14, 16]])

You can also use the concatenate function and specify the corresponding axis to achieve this effect:

>>> concatenate((a, b), axis=1) array([[ 0, 1, 2, 0, 2, 4],   [ 3, 4, 5, 6, 8, 10],   [ 6, 7, 8, 12, 14, 16]])

Vertical combination

>>> vstack((a, b)) array([[ 0, 1, 2],   [ 3, 4, 5],   [ 6, 7, 8],   [ 0, 2, 4],   [ 6, 8, 10],   [12, 14, 16]])

Similarly, you can use the concatenate function and specify the corresponding axis to achieve this effect.

>>> concatenate((a, b), axis=0) array([[ 0, 1, 2],   [ 3, 4, 5],   [ 6, 7, 8],   [ 0, 2, 4],   [ 6, 8, 10],   [12, 14, 16]])

Deep combination

In addition, there is a combined function dstack in depth. As the name implies, it is combined on the third axis (depth) of the array. As follows:

>>> dstack((a, b)) array([[[ 0, 0],   [ 1, 2],   [ 2, 4]],    [[ 3, 6],   [ 4, 8],   [ 5, 10]],    [[ 6, 12],   [ 7, 14],   [ 8, 16]]])

After careful observation, it is found that the corresponding elements are combined into a new list, which serves as the elements of the new array.

Row combination

Row combination can combine multiple one-dimensional arrays as each row of the new array:

>>> one = arange(2) >>> one array([0, 1]) >>> two = one + 2 >>> two array([2, 3]) >>> row_stack((one, two)) array([[0, 1],   [2, 3]])

For a 2-dimensional array, it acts like a vertical combination.

Column combination

The effect of column combination should be clear. As follows:

>>> column_stack((oned, twiceoned)) array([[0, 2],   [1, 3]])

For a 2-dimensional array, the function is like a horizontal combination.

Split array

In NumPy, the functions used to split an array include. You can divide an array into child arrays of the same size or specify the location of the original array.

Horizontal segmentation

>>> a = arange(9).reshape(3,3) >>> a array([[0, 1, 2],   [3, 4, 5],   [6, 7, 8]]) >>> hsplit(a, 3) [array([[0],   [3],   [6]]),  array([[1],   [4],   [7]]),  array([[2],   [5],   [8]])]

You can also call the split function and specify the Axis as 1 to achieve this effect:

split(a, 3, axis=1)

Vertical segmentation

Vertical split is to split the array along the vertical axis:

>>> vsplit(a, 3) >>> [array([[0, 1, 2]]), array([[3, 4, 5]]), array([[6, 7, 8]])]

Similarly, you can use the solit function and specify the Axis as 1 to get the effect:

>>> split(a, 3, axis=0)

Deep-oriented segmentation

The dspscanner function uses a depth-oriented splitting method:

>>> c = arange(27).reshape(3, 3, 3) >>> c 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]]]) >>> dsplit(c, 3) [array([[[ 0],   [ 3],   [ 6]],    [[ 9],   [12],   [15]],    [[18],   [21],   [24]]]),  array([[[ 1],   [ 4],   [ 7]],    [[10],   [13],   [16]],    [[19],   [22],   [25]]]),  array([[[ 2],   [ 5],   [ 8]],    [[11],   [14],   [17]],    [[20],   [23],   [26]]])]

Copying and mirroring (View)

When arrays are computed and processed, their data is sometimes copied to a new array. This is usually the source of confusion for new users. There are three cases:

No replication

Simple assignment without copying array objects or their data.

>>> A = arange (12) >>> B = a # Do not create a new object> B is a # a and B are the two names of the same array object True> B. shape = 3, 4 # also changed the shape of a>. shape (3, 4) Python transmits an indefinite object as reference 4, so function calls do not copy arrays. >>> Def f (x ):... print id (x)... >>> id (a) # id is the unique identifier of an object 148293216 >>> f (a) 148293216

View and shortest copy

Different array objects share the same data. The View method creates a new array object pointing to the same data.

>>> C =. view () >>> c is a False >>>> c. base is a # c is the image that a holds data. True >>> c. flags. owndata False >>>>>> c. shape = 2, 6 # The shape of a is not changed>. shape (3, 4) >>> c [1234] = # the data of a Changes >>> a array ([[0, 1, 2, 3], [1234, 5, 6, 7], [8, 9, 10, 11])

The slice array returns a view of it:

>>> S = a [:,] # obtain the elements at 1 and 2 in each row >>> s [:] = 10 # s [:] is a s image. Note the differences between s = 10 and s [:] = 10 >>> a array ([0, 10, 10, 3], [1234, 10, 10, 7], [8, 10, 10, 11])

Deep replication

This replication method completely copies the array and its data.

>>> D =. copy () # Create a new array object containing new data >>> d is a False >>> d. base is a # d and a do not have any relationship at present. False >>> d [9999] = >>> a array ([0, 10, 10, 3], [1234, 10, 10, 7], [8, 10, 10, 11])

The above is all the content of this article. I hope it will help you learn and support PHP.

For more details about NumPy, refer to array 3 (combination!

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.