python numpy的一些基本使用

import numpy as np

'''the 1st part: the build of array'''

array1_1 = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
'''[[1 2 3]
 [4 5 6]
 [7 8 9]]
 '''
array1_2 = np.zeros([3, 3])
'''[[0. 0. 0.]
 [0. 0. 0.]
 [0. 0. 0.]]
 '''
array1_3 = np.ones([3, 3])
'''[[1. 1. 1.]
 [1. 1. 1.]
 [1. 1. 1.]]
 '''
array1_4 = np.empty([3, 3])
'''[[0.000e+000 0.000e+000 0.000e+000]
 [0.000e+000 0.000e+000 3.617e-321]
 [0.000e+000 0.000e+000 0.000e+000]]
 it is random
 '''
array1_5 = np.full([3, 3], 6)
'''[[6 6 6]
 [6 6 6]
 [6 6 6]]
'''
array1_6 = np.eye(3)
'''[[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]
 '''
array1_7 = np.arange(10, 20, 2)
'''[10 12 14 16 18]'''
array1_8 = np.linspace(10, 20, 5)
'''[10.  12.5 15.  17.5 20. ]'''
array1_9 = np.logspace(10, 20, 5)
'''[1.00000000e+10 3.16227766e+12 1.00000000e+15 3.16227766e+17
 1.00000000e+20]
'''
array1_10 = np.diag([3, 4, 5])
'''[[3 0 0]
 [0 4 0]
 [0 0 5]]
'''
array1_11 = np.tri(3)
'''[[1. 0. 0.]
 [1. 1. 0.]
 [1. 1. 1.]]
'''
array1_12 = np.vander([1, 2, 3])
'''[[1 1 1]
 [4 2 1]
 [9 3 1]]
'''


'''the 2nd part: the property of array'''
array2_1 = np.vander([1, 2, 3])
'''
[[1 1 1]
 [4 2 1]
 [9 3 1]]
'''
s1 = array2_1.shape
'''(3, 3)'''
s2 = array2_1.size
'''9'''
s3 = array2_1.T
'''
[[1 4 9]
 [1 2 3]
 [1 1 1]]
'''
s4 = array2_1.real
'''[[1 1 1]
 [4 2 1]
 [9 3 1]]
 the real part of array
'''
s5 = array2_1.imag
'''[[0 0 0]
 [0 0 0]
 [0 0 0]]
the imaginary part of array
'''


'''the 3rd part:the operation of array'''
'''array.copy'''
array3_1 = np.vander([1, 2, 3])
cc = array3_1
#cc[0, 0] = 0
'''
array3_1:
[[0 1 1]
 [4 2 1]
 [9 3 1]] 
 cc:
 [[0 1 1]
 [4 2 1]
 [9 3 1]]
 It means that the content of the original array will be changed with the change in cc while you use "=".
 We can use another method if you won't want to change the original array
 '''
cc1 = array3_1.copy()
cc1[0, 0] = 0
'''
array:
[[1 1 1]
 [4 2 1]
 [9 3 1]] 
 cc1:
 [[0 1 1]
 [4 2 1]
 [9 3 1]]'''

cc2 = array3_1.reshape(1, 9)
'''array:
[[1 1 1]
 [4 2 1]
 [9 3 1]] 
cc2:
[[0 1 1 4 2 1 9 3 1]]'''
cc2.resize(3, 3)
'''
[[1 1 1]
 [4 2 1]
 [9 3 1]]'''
d_1 = cc.flatten()
''' [1 1 1 4 2 1 9 3 1]'''
d_2 = cc2.max()
'''9'''


'''the fourth part: indexes of array'''
f_1 = array3_1[1:3, 1:3]
'''
 [[2 1]
 [3 1]]
'''
f_2 = array3_1[1:3, 1:3][0, 0]
'''2'''
f_3 = array3_1[[1, 2], [0, 1]]
'''
 [4 3]
 [1,2] is the row
 [0,1] is the list
 So (1,0) is 4 and (2,1) is 3
'''
f_4 = array3_1[np.ix_([1, 2], [0, 1])]
'''
[[4 2]
 [9 3]]
'''
for i in array3_1:
    print(i)
'''
[1 1 1]
[4 2 1]
[9 3 1]
'''
for i in np.nditer(array3_1):
    print(i)
'''
1
1
1
4
2
1
9
3
1
'''

'''the fifth part: the separation and combination'''
array4_1 = np.vander([1, 2, 3])
array4_2 = np.eye(3)
gg_1 = np.vstack([array4_1, array4_2])
'''
 [[1. 1. 1.]
 [4. 2. 1.]
 [9. 3. 1.]
 [1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]
'''
gg_2 = np.hstack([array4_1, array4_2])
'''
 [[1. 1. 1. 1. 0. 0.]
 [4. 2. 1. 0. 1. 0.]
 [9. 3. 1. 0. 0. 1.]]

'''
gg_3 = np.stack([array4_1, array4_2])
'''
 [[[1. 1. 1.]
  [4. 2. 1.]
  [9. 3. 1.]]

 [[1. 0. 0.]
  [0. 1. 0.]
  [0. 0. 1.]]]
'''
array4_3 = np.vander([1, 2, 3, 4])

np.vsplit(array4_3, 2)
np.hsplit(array4_3, 2)
np.split(array4_3, 2)
print(array4_3)


上一篇:SQL server2008 安装教程


下一篇:使用OpenCV获取我们想要的图片区域