An in depth understanding of NumPy array 3 (composition)

  • 2020-05-19 05:06:07
  • OfStack

The first two articles gave a basic introduction to the NumPy array, while this article makes an in-depth discussion of the NumPy array. You'll first look at arrays of custom types, then at array combinations, and finally at array replication.

Custom structured array

NumPy also allows you to define structural types like the C language. The structure is defined in NumPy 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) 

Here student is the name of the custom structure type, created using the dtype function. In the first parameter, 'names' and 'formats' cannot be changed. The name of the field in the structure is listed in names, and the data type of the corresponding field is listed in formats. S32 represents a 32-byte string, i represents a 32-bit integer, and f represents a 32-bit floating point number. When the last parameter is True, memory alignment is required.

The character encoding of NumPy is used in the field to represent the data type. More detailed data types are shown in the table below.

数据类型 字符编码
整数 i
无符号整数 u
单精度浮点数 f
双精度浮点数 d
布尔值 b
复数 D
字符串 S
Unicode U
Void V

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


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

In addition to listing the data for each field in turn in each element, the last parameter in the array function needs to specify its corresponding data type.

Note: examples are taken from zhang ruoyu's Python, page 29 of the art of scientific computing. For more on dtype, see chapter 2 of NumPy for Beginner 1.

Combination function

Here's 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]]) 

The level of combination


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

This effect can also be obtained by using the concatenate function and specifying the corresponding axis:


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

The vertical combination


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

Again, you can get this effect by using the concatenate function and specifying the appropriate axis.


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

The depth of the portfolio

In addition, there is a combined function dstack for 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]]]) 

When you look closely, you can see that the corresponding elements are combined into a new list that ACTS as the elements of the new array.

Combination of

Row combination can combine multiple 1-dimensional arrays as each row of a 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 of 1.

Column combinations

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


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

For a 2-dimensional array, it ACTS like a horizontal combination of 1.

Split array

In NumPy, the functions that split the array are hsplit, vsplit, dsplit, and split. You can split an array into subarrays of the same size, or specify the location where the original array was split.

Horizontal partitioning


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

Also call the split function and specify axis 1 to get this effect:


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

Vertical segmentation

Vertical segmentation is to slice the array along the vertical axis:


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

Similarly, the solit function can be used to obtain this effect by specifying the axis as 1:


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

Depth-oriented segmentation

The dsplit function USES a depth-oriented segmentation method:


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

Copy and mirror (View)

When manipulating and manipulating arrays, their data is sometimes copied to new arrays and sometimes not. This is often a source of confusion for beginners. There are three scenarios:

Not copying at all

Simply assign values without copying array objects or their data.


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

View (view) and shallow copy

Different array objects share the same data. The view method creates a new array object that points to the same data.


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

The slice array returns one of its views:


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

Deep copy

This copy method copies the array and its data exactly.


 >>> d = a.copy()  # To create the 1 A new array object with new data  
>>> d is a 
False 
>>> d.base is a  #d and a It doesn't matter now  
False 
>>> d[0,0] = 9999 
>>> a 
array([[ 0, 10, 10, 3], 
  [1234, 10, 10, 7], 
  [ 8, 10, 10, 11]]) 


Related articles: