Python uses Numpy to process arrays

1 Introduction

Numpy is a commonly used Python science and technology library, through which you can quickly operate on arrays, including shape operations, sorting, selection, input and output, discrete Fourier transform, basic linear algebra, basic statistical operations and random simulation. Many Python libraries and scientific computing software packages use Numpy arrays as operation objects, or convert incoming Python arrays into Numpy arrays, so manipulating data in Python is inseparable from Numpy.

The core of Numpy is the ndarray object, which is encapsulated by Python ’s n-dimensional array, but it is pre-compiled by C language related array operations, so it has higher execution efficiency than native Python, but it still uses Python language encoding, so it has Simple code and efficient running speed. There are some differences between ndarry and arrays. It is worth noting that the elements in numpy arrays have the same type and have a fixed size when they are created, which is different from the dynamic growth of Python array objects.

2. Array objects

2.1, attributes

Numpy objects are in the form of isomorphic multi-dimensional arrays. The dimensions of the array are called axes, and the number of elements in each dimension is called the length of the axis. For example, the following is a 2 × 3 two-dimensional array arr, the length of the first axis is 3, the length of the second axis is 2

arr = [[ 1., 0., 0.],
       [ 0., 1., 2.]]

The commonly used attributes of the arr array object are as follows:

# 数组轴的个数
arr.ndim
# 数组维度及长度,例如2×3的数组其shape为(2, 3)
arr.shape
# 数组元素的总个数
arr.size
# 数组中元素的数据类型
arr.dtype
# 数组中元素所占字节数
arr.itemsize

2.2, create an array

You can use the array () method to wrap an ordinary Python array to convert it into a numpy array, and specify the data type of the element by dtype =. The array can be a two-dimensional equal-dimensional array or a tuple.

If you need to fill an array of known size, you can use the function zeros () to fill all elements with 0, or ones () to fill elements with 1, and empty () to fill elements with random numbers

The arange (a, b, c) function is used to generate an array element from a to b every c length. The linspace (a, b, c) function is used to generate c array elements between a and b

# 普通数组转化为numpy数组
a1 = np.array([2, 3, 4], dtype=float)
print(a1)                                    
# 将元组数组转化为二维numpy数组
a2 = np.array([(1, 2, 3), (3, 4, 5)])
print(a2)
# 将3×3的数组用1填充
a3 = np.ones((3, 3))
print(a3)
# 从1到10,每隔2生成一个元素
a4 = np.arange(1, 10, 2)
print(a4)
# 在1到12之间生成4个元素
a5 = np.linspace(1, 12, 4, dtype=int)
print(a5)

'''
普通数组转化为numpy对象:
[2. 3. 4.]
元组数组:
[[1 2 3]
 [3 4 5]]
用1填充数组:
[[1. 1. 1.]
 [1. 1. 1.]
 [1. 1. 1.]]
从1到10每隔2生成一个元素:
[1 3 5 7 9]
在1到12之间生成4个元素:
[ 1  4  8 12]
'''

2.3, array operations

Arithmetic operators can be directly applied to the matrix, and the result is to apply the operation to each element. For example, matrix A * B is the corresponding multiplication of each element. The multiplication of the matrix uses the @ symbol

A = np.array([[1, 1],
              [0, 1]])
B = np.array([[2, 0],
              [3, 4]])
print(A * B)
print(A @ B)

'''
矩阵元素对应相乘:
[[2 0]
 [0 4]]
矩阵的乘法:
[[5 4]
 [3 4]]
'''

Some functions in numpy apply to the entire array, such as summation, max, max, min. If an axis is specified in these parameters, it applies to the specified axis.

There are also some functions applied to specific elements in the array, such as finding sin, cos, exp, square root sqrt, etc. These functions are called general functions (ufunc)

a = np.array([[0, 1, 2, 3],
              [4, 5, 6, 7],
              [8, 9, 10, 11]])
print(a.max())  # 求整体的最大值,结果为11
print(a.sum(axis=0))  # 求每一列的和,结果为:[12 15 18 21]
print(np.sqrt(a))   # 数组每个元素求开方

The array in numpy can be indexed, sliced, and iterated like the list in python. The array a [x] represents accessing the elements of array a with index x, and the one-dimensional array a [x: y] represents accessing the elements of the array from x to y. If x is omitted, it means starting from the beginning, and y is omitted until the end. a [x: y: a] represents a value from x to y every a element, if a is negative, it means to take the value in reverse order.

a = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
print(a[1:3])  # 输出下标为1到3的元素:[1 2]
print(a[::-2])  # 逆序每隔两个元素选一个值:[9 7 5 3 1]

If it is a multi-dimensional array, the indexes are separated by commas. You can use ... to omit certain dimensions, if omitted, it will be regarded as all output of the dimension, for example, 如x[...,3]equivalent tox[:,:,:,:,3]。

可以通过for循环迭代多为数组,其内容为低一维度的子数组,如果希望遍历每一个子元素,可以使用flat属性。

a = np.array([[0, 1, 2, 3],
              [10, 11, 12, 13],
              [40, 41, 42, 43]])
# 输出a第一维(行)的前三个,第二维(列)下标的1~3
print(a[1:3, 0:3])
# 输出行的所有,下标为2的列
print(a[2, ...])
# 遍历数组
for row in a:
    print(row)
# 遍历每个子元素
for item in a.flat:
    print(item)

'''
后两行的1~3列:
[[10 11 12]
 [40 41 42]]
第三行的所有列:
[40 41 42 43]
遍历数组:
[0 1 2 3]
[10 11 12 13]
[40 41 42 43]
遍历每个元素:
0
1
2
......
41
42
43
'''

In addition to using specific numbers as indexes, you can also use numpy arrays as indexes. For example, use array i as the index of one-dimensional array a, and output a [i]. When the array i is a multi-dimensional array, select the elements from a and fill in the corresponding positions of the i array

a = np.arange(12) ** 2
print(a)
i = np.array([1, 3, 5])
print(a[i])
# 多维数组索引j
j = np.array([[3, 4], [9, 7]])
print(a[j])

'''
[  0   1   4   9  16  25  36  49  64  81 100 121]
数组a的1、3、5个元素
[ 1  9 25]
通过多为索引j取出a的数据填到对应位置
[[ 9 16]
 [81 49]]
'''

If a is a multidimensional array, a single element of the indexed array represents the first dimension of the selected array a

If the multi-dimensional array is indexed in multiple dimensions, multiple index arrays i, j are passed in and separated by commas

a = np.array(([[0, 1, 2, 3],
               [4, 5, 6, 7],
               [8, 9, 10, 11]]))
# 多维数组的单元素索引
i = np.array([0, 1])
print(a[i])
# 对多维数组提供多维度索引,同时提供i,j代表取出a的[0,2]、[1,3]两个元素
j = np.array([2, 3])
print(a[i, j])
'''
选择多维数组a的第0、1两行:
[[0 1 2 3]
 [4 5 6 7]]
a的[0,2]、[1,3]两个元素:
[2 7]
'''

2.4, change the dimension

The array's reshape () method can reconstruct the original array into an array of target dimensions, for example, reconstruct a 2 × 6 array into a 3 × 4 array,

The array does not modify the original array during reconstruction, but returns the modified result array

It is worth noting that the array is reconstructed and printed from the rightmost dimension to the left. For example, the following 3 × 4 array b, first arrange 4 rows, and then wrap, arrange such 3 rows. If it is multi-dimensional, continue to output according to such ranks. If the array dimension is -1, the size of the dimension will be calculated automatically, for example, an array with 12 elements, the second and third dimensions are 3 × 2, then the first dimension is 2

The ravel () function can expand the array into a one-dimensional array.

a=np.array([[1,2,3,4,5,6],
            [7,8,9,10,11,12]])
b=a.reshape(3,4)
print(b)
# 多维数组,自动计算
print(a.reshape(-1,3,2))
# 展开数组
flatted = b.ravel()
print(flatted, end=' ')
'''
[[ 1  2  3  4]
 [ 5  6  7  8]
 [ 9 10 11 12]]
2×3×2的多维数组:
[[[ 1  2]
  [ 3  4]
  [ 5  6]]

 [[ 7  8]
  [ 9 10]
  [11 12]]]
展开数组:
[ 1  2  3  4  5  6  7  8  9 10 11 12] 
'''

numpy's hstack () function can merge multiple arrays in the horizontal direction, vstack () function can merge multiple arrays in the vertical direction

Conversely, hsplit () and vsplit () can be split into a specified number of arrays

a=np.array([1,2,3])
b=np.array([4,5,6])
# 垂直方向合并
c=np.vstack((a,b))
print(c)
# 水平方向合并
print(np.hstack((a,b)))
# 水平方向拆分
print(np.hsplit(c,3))
'''
垂直堆叠
[[1 2 3]
 [4 5 6]]
水平合并
[1 2 3 4 5 6]
水平拆分为三个1×2的:
[array([[1],
       [4]]), 
array([[2],
       [5]]), 
array([[3],
       [6]])]
'''

The transpose () function can realize the function of array transposition . For example, the following array a is a 4 × 3 × 2 three-dimensional array. Through transpose (2,0,1), 0, 1, and 2 respectively represent the original dimensions of the array In the first, second, and third dimensions, the position in parentheses represents the transposed dimension, so the third dimension of the array is placed in the first dimension, the first dimension is placed in the second dimension, and the second dimension is placed in the third dimension, that is Transpose into a 3 × 2 × 4 array.

a = np.array([
    [[1, 2, 3, 4],
     [5, 6, 7, 8],
     [9, 10, 11, 12]],
    [[13, 14, 15, 16],
     [17, 18, 19, 20],
     [21, 22, 23, 24]]
])
b = a.transpose(2, 0, 1)
print(b)

'''
----输出结果------
[[[ 1  5  9]
  [13 17 21]]
 [[ 2  6 10]
  [14 18 22]]
 [[ 3  7 11]
  [15 19 23]]
 [[ 4  8 12]
  [16 20 24]]]
'''

2.5, copy of the array

When an array object is assigned to a new variable, does it open up a new storage space or just pass a reference? The answer is quotation.

For example, executing the statement b = a just passes a reference to b, and the operation performed on b directly affects a. The node id of the two objects a and b is the same

a = np.array([1, 2, 3])
b = a
# 修改b
b[0] = 0
print(a)
# 输出a、b对象的id
print(id(a), id(b))

'''
修改b,a也发生了变化
[0 2 3]
查看二者的id
2290013812656 2290013812656
'''

By slicing back to the view of the array, modifying the shape of the view will not affect the original array, but modifying the data on the view will also change the original array. After executing del a, since c references a, a will still exist in memory and will not be deleted

c = a[:]
# 修改视图的形状
c.shape = 3, 1
print(c, a)
# 修改视图c的数据
c[0] = 1
print(a[0])

'''
对视图c的形状做修改,a不会受到影响
[[0]
 [2]
 [3]] [0 2 3]
修改c的数据,a也会随之改变:
1

The copy () method can generate a copy of the data, so the operation of the copy will not affect the original array at all

d= a.copy()
d[0]=5
# 修改数组的副本d,a不受影响,输出a:[1 2 3]
print(a)

3. Save the file

numpy saves an array arr as a .npy file through save (), and can read the file through np.load ()

np.save("output.npy",arr)

arr = np.load("output.npy")

If you need to store multiple arrays, you can save it as a .npz file through np.savez (), and its variable name is used as the key value of the array. Similarly, you can use load () to read

np.savez('array_save.npz',arr1,arr2,arr3)

Arr=np.load('arr_save.npz')
arr1=Arr['arr1']        # 通过key值取到不同数组

The .npy and .npz files cannot be viewed manually. If you need to view the format, you can save it as txt text, save it with savetxt (), and read it with loadtxt ()

np.savetxt('data.txt',arr)

data=np.loadtxt('data.txt')

 When saving the file, you can set the format of the output array through the fmt attribute. Set the data to be saved as an integer as follows, followed by a tab

np.savetxt("./output.txt", data, fmt='%d	')

 

Published 124 original articles · Like 65 · Visit 130,000+

Guess you like

Origin blog.csdn.net/theVicTory/article/details/103011188