The first two articles made a basic introduction to the NumPy array, and this paper discusses the NumPy array in depth. First, we introduce the array of custom types, then the combination of arrays, and finally the problem of array replication.
Custom structure Array
You can also define struct types like the C language by NumPy. The methods for defining structs in NumPy are as follows:
Define the structure type name, define the field name, and indicate the field data type.
Here student is the name of the custom struct type, created using the Dtype function, in the first argument, ' 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 length string, I represents a 32-bit integer, and F represents a 32-bit floating-point number. When the last argument is true, it indicates that the memory alignment is required.
The NumPy character encoding is used in the field to represent the data type. More detailed data types are shown in the following table.
Data type |
Character encoding |
Integer |
I |
unsigned integer |
U |
Single-precision floating-point numbers |
F |
Double-precision floating point numbers |
D |
Boolean value |
B |
Plural |
D |
String |
S |
Unicode |
U |
Void |
V |
After you have defined the structure type, you can define an array with the type as an element:
In addition to listing the corresponding field's data sequentially in each element, the last parameter in the array function is required to specify its corresponding data type.
Note: The example comes from the 29 pages of Zhang Jo's Python scientific computing art. For more information on Dtype, please refer to chapter II of the book "NumPy for Beginner".
Combining functions
This describes the combination of 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, ten], [12, 14,
16]]
Horizontal combination
>>> Hstack ((A, B))
array ([[0, 1, 2, 0, 2, 4],
[3, 4, 5, 6, 8, ten],
[6, 7, 8, 12, 14, 16]]
You can also obtain this effect by concatenate the function and specifying the appropriate axis:
>>> concatenate ((A, B), Axis=1)
Array ([[0, 1, 2, 0, 2, 4],
[3, 4, 5, 6, 8, ten],
[6, 7, 8, 12, 1 4, 16]])
Vertical combination
>>> Vstack ((A, B))
array ([[0, 1, 2],
[3, 4, 5], [
6, 7
, 8], [0, 2, 4], [6 (8,], 1 2, 14, 16]]
Similarly, you can get this effect by concatenate the function and specifying the corresponding axis.
>>> concatenate ((A, B), axis=0)
Array ([[0, 1, 2],
[3, 4, 5], [6, 7, 8], [0, 2, 4]
,
[ 6, 8, ten],
[12, 14, 16]]
Depth combination
In addition, there is the depth of the combination function Dstack. As the name suggests, it is a combination of the third axis (that is, the depth) of the array. As follows:
>>> Dstack ((A, B))
array ([[[0, 0],
[1, 2],
[2, 4]],
[[
3, 6], [4, 8], [5, 10]] ,
[[6],
[7],
[8, 16]]]
Careful observation shows that the corresponding elements are grouped together into a new list, which acts as an element of the new array.
Row combination
Row combinations 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-d array, it acts like a vertical combination.
Column combinations
The effect of a column combination should be clear. As follows:
>>> Column_stack ((oned, twiceoned))
Array ([[0, 2],
For a 2-d array, it acts like a horizontal combination.
Split array
In NumPy, the functions of the split array are hsplit, Vsplit, Dsplit, and split. You can split an array into a child array of the same size, or specify the location of the original array split.
Horizontal split
>>> 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]]]
The split function is also called and the axis is specified to achieve this effect:
Vertical split
A vertical split is a group of tangent fractions along a vertical axis:
Similarly, you can get this effect by solit the function and specifying an axis:
Depth-oriented segmentation
The Dsplit function uses a depth-oriented segmentation method:
>>> C = arange. Reshape (3, 3, 3)
>>> c Array ([[[[
0, 1, 2],
[3, 4, 5],
[6, 7], 8]],
[[9, one], [A, M], [[A], [a], [[A], [A, G], [a]]
)
> >> Dsplit (c, 3)
[Array ([[[0],
[3],
[6]],
[[9],
[a],
[m]],
[[18],
[A],
[]]], Array ([[[[
1],
[4],
[7]]], [[Ten], [a], []], [[], [
22],
[]]], Array ([[[
2],
[5],
[8]],
[[one], [M
],
[]],
[[],
[23] ,
[26]]]]
Copy and Mirror (View)
When computing and processing arrays, their data is sometimes copied to the new array sometimes not. This is usually the source of novice confusion. There are three kinds of situations:
Do not replicate at all
Simple assignment, without copying the array objects or their data.
>>> a = Arange (a)
>>> B = a #不创建新对象
>>> B is a # A and B are two names of the same array object
true< c6/>>>> b.shape = 3,4 #也改变了a的形状
>>> a.shape
(3, 4)
Python passes an indeterminate object as reference 4, so the function call does not copy the array.
>>> def f (x):
... Print ID (x) ...
>>> ID (a) #id是一个对象的唯一标识
148293216
>>> F (a)
148293216
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.
>>> C = A.view ()
>>> c is a
False
>>> c.base is a #c是a持有数据的镜像
true< c27/>>>> c.flags.owndata
False
>>>
>>> c.shape = 2,6 # A has not changed in shape
> >> A.shape
(3, 4)
>>> c[0,4] = 1234 #a的数据改变了
>>> a
array ([[0, 1, 2, 3] ,
[1234, 5, 6, 7],
[8, 9, 10, 11]]
The slice array returns one of its views:
>>> s = a[:, 1:3] # Get the elements >>> s[for each row 1, 2
:] = # s[:] is a mirror image of S. Note the difference between s=10 and s[:]=10
>>> a
array ([[0, Ten, 3],
[1234, ten, 7],
[8, 10, 10, 11]])
Deep copy
This replication method completely duplicates the array and its data.
>>> d = a.copy () #创建了一个含有新数据的新数组对象
>>> D is a
False
>>> D.base is a #d和a现在没有任何关系
False
>>> d[0,0] = 9999
>>> a
array ([[0, Ten, 3],
[1234, ten, 7],
[8, 1 0, 10, 11]]
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.