[pyTorch Study Notes ①] Numpy Basics Part 1

1. Why learn Numpy

Python's own list and array structures have shortcomings: list consumes a lot of memory, and array cannot achieve multi-dimensionality.
The emergence of Numpy makes up for the above defects.
Numpy has two basic objects:
ndarray: multidimensional array that stores a single data type
ufune: able to process arrays

Two, the characteristics of Numpy

1. nbarray: A fast and space-saving multidimensional array, providing data-based arithmetic operations and advanced broadcasting functions.
2. You can use standard mathematical functions to perform fast operations on the data of the entire array without looping.
3. Tools that can read/write array data on disk and manipulate memory image files.
4. Capable of linear algebra, random number generation, and Fourier transform
5. Tools for integrating C, C++, and Fortran codes

3. Generate Numpy array

1. Create an array from existing data

# 从已有数据创建数组
list1 = [3, 2, 1, 0, 1, 2]
list2 = [[3, 2, 1], [0, 1, 2], [3, 2, 5]]
nd1 = np.array(list1)
nd2 = np.array(list2)
print(nd1)
>>>[3 2 1 0 1 2]
print(nd2)
>>>[[3 2 1]
    [0 1 2]
    [3 2 5]]

2. Use the random module to generate

# 利用random模块生成
# 生成0-1之间的随机数
nd3 = np.random.random([3, 3]) 
print(nd3)
>>>[[0.91113427 0.33318659 0.94802844]
    [0.32456725 0.46730493 0.16622273]
    [0.2118558  0.38503486 0.45407848]]
# 生成[1,3)均匀分布的随机数
nd4 = np.random.uniform(1, 3) 
print(nd4)
>>>1.5480276625543103
# 生成标准正态的随机数
nd5 = np.random.randn(3, 3) 
print(nd5)
>>>[[ 0.53406292  1.65104788  1.29605711]
    [-0.17316462  1.18174186  0.52425828]
   [ 1.38202889 -1.40712902 -1.39022516]]
# 生成[1,10)随机的3*3的整数数组
nd6 = np.random.randint(low = 1,high = 10, size = (3, 3)) 
print(nd6)
>>>[[7 4 8]
    [4 9 6]
    [3 1 5]]
# 生成N(0,1)正态分布的3*3数组
nd7 = np.random.normal(loc = 0, scale =1, size = (3,3)) 
print(nd7)
>>>[[-0.24026993  0.00578158  0.19904363]
 	[-0.54125372 -1.21162976  1.08500657]
	[-0.8246835   0.48108164 -0.43231472]]
# 随机打乱顺序
np.random.shuffle(nd7) 
print(nd7)
>>>[[ 0.83493857  1.79464175  1.71298196]
 	[ 0.88533094 -0.0611668  -0.41994593]
 	[ 0.10587105  1.12227856 -0.51794872]]
# 设置随机种子
np.random.seed(0)  # 先定义一个随机数种子
print(np.random.rand(10))  # "随机"生成10个数
>>>[0.5488135  0.71518937 0.60276338 0.54488318 0.4236548  0.64589411
 0.43758721 0.891773   0.96366276 0.38344152]
np.random.seed(0)  
print(np.random.rand(3))  # "随机"生成5个数
>>>[0.5488135  0.71518937 0.60276338]
print(np.random.rand(3))  # "随机"生成5个数
>>>[0.54488318 0.4236548  0.64589411]
np.random.seed(0)  
print(np.random.rand(4))  # "随机"生成5个数
>>>[0.5488135  0.71518937 0.60276338 0.54488318]
print(np.random.rand(4))  # "随机"生成5个数
>>>[0.4236548  0.64589411 0.43758721 0.891773  ]
np.random.seed(1)  
print(np.random.rand(4))  # "随机"生成5个数
>>>[4.17022005e-01 7.20324493e-01 1.14374817e-04 3.02332573e-01]
print(np.random.rand(4))  # "随机"生成5个数
>>>[0.14675589 0.09233859 0.18626021 0.34556073]
# 可以发现,这种随机是假随机,同一个随机种子每次都会从同一个数开始,按顺序输出。

3. Create a multidimensional array of a specific shape

# 生成3*4的零矩阵
nd1 = np.zeros((3,4))
print(nd1)
>>>[[0. 0. 0. 0.] 
 	[0. 0. 0. 0.] 
 	[0. 0. 0. 0.]]
# 生成3*4的一矩阵
nd2 = np.ones((3,4))
print(nd2)
>>>[[1. 1. 1. 1.]
 	[1. 1. 1. 1.]
 	[1. 1. 1. 1.]]
# 生成3*4的空矩阵,(未赋值,垃圾值)
nd3 = np.empty((2,3))
print(nd3)
>>>[[0. 0. 0.]
 	[0. 0. 0.]]
# 以nd3相同的维度生成零矩阵
nd4 = np.zeros_like(nd3)
print(nd4)
>>>[[0. 0. 0.]
 	[0. 0. 0.]]
# 以nd3相同的维度生成一矩阵
nd5 = np.ones_like(nd3)
print(nd5)
>>>[[1. 1. 1.]
 	[1. 1. 1.]]
# 以nd1相同的维度生成空矩阵
nd6 = np.empty(nd1)
print(nd6)
>>>[[1. 1. 1. 1.]
 	[1. 1. 1. 1.]
 	[1. 1. 1. 1.]]
# 生成5维对角线为1的矩阵
nd7 = np.eye(5)
print(nd7)
>>>[[1. 0. 0. 0. 0.]
 	[0. 1. 0. 0. 0.]
 	[0. 0. 1. 0. 0.]
 	[0. 0. 0. 1. 0.]
 	[0. 0. 0. 0. 1.]]
 # 生成3*5,元素全为666的矩阵
nd8 = np.full((3,5),666)
print(nd8)
>>>[[666 666 666 666 666]
 	[666 666 666 666 666]
 	[666 666 666 666 666]]

4. Use arange and linspace functions to generate arrays

# 生成【0,10),步长为1的数组
print(np.arange(10))
>>>[0 1 2 3 4 5 6 7 8 9]
# 生成【0,10),步长为1的数组
print(np.arange(0,10))
>>>[0 1 2 3 4 5 6 7 8 9]
# 生成【1,10),步长为2的数组
print(np.arange(1,10,2))
>>>[1 3 5 7 9]
# 生成【9,-5),步长为-4的数组
print(np.arange(9,-5,-4))
>>>[ 9  5  1 -3]
# 将【0,10】等分成9份
print(np.linspace(0,10,9))
>>>[ 0.    1.25  2.5   3.75  5.    6.25  7.5   8.75 10.  ]
# 将【0,10)等分成9份
print(np.linspace(0,10,9,endpoint=False))
>>>[0.         1.11111111 2.22222222 3.33333333 4.44444444 5.55555556
 	6.66666667 7.77777778 8.88888889]
# 将【0,10】等分成9份,返回步长
print(np.linspace(0,10,9,retstep = True))
>>>(array([ 0.  ,  1.25,  2.5 ,  3.75,  5.  ,  6.25,  7.5 ,  8.75, 10.  ]), 1.25)

4. Get elements

1. One-dimensional data

# 随机生成一组1*10数组
nd1 = np.random.random([10])
print(nd1)
>>>[0.22793867 0.56777956 0.4588223  0.56765467 0.04356994 0.20583102
 	0.10753376 0.54937099 0.07197144 0.48013895]
# 输出第一个元素
print(nd1[0])
>>>0.22793866818053765
# 截取0-3元素
print(nd1[0:3])
>>>[0.22793867 0.56777956 0.4588223 ]
# 获取第2-10个以3为步长的数组
print(nd1[1:9:3])
>>>[0.56777956 0.04356994 0.54937099]
# 倒序
print(nd1[::-1])
>>>[0.48013895 0.07197144 0.54937099 0.10753376 0.20583102 0.04356994
 	0.56765467 0.4588223  0.56777956 0.22793867]

2. Multidimensional data

# 生成【0,25)步长为1的一维数组,变形为5*5
nd2 = np.arange(25).reshape([5,5])
print(nd2)
>>>[[ 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]]
# 输出第2,3,4行、2,3,4列的子数组
print(nd2[1:3,1:3])
>>>[[ 6  7]
 	[11 12]]
# 输出大于3,小于10的数据
print(nd2[(nd2>3)&(nd2<10)])
>>>[4 5 6 7 8 9]
# 输出第2,3行的数据
print(nd2[[1,2]])
>>>[[ 5  6  7  8  9]
 	[10 11 12 13 14]]
#输出第2,3列的数据
print(nd2[:,1:3])
>>>[[ 1  2]
 	[ 6  7]
 	[11 12]
 	[16 17]
 	[21 22]]

3. Randomly extract data

nd = np.arange(1,25,dtype = float)
print(nd)
>>>[ 1.  2.  3.  4.  5.  6.  7.  8.  9. 10. 11. 12. 13. 14. 15. 16. 17. 18.
 	19. 20. 21. 22. 23. 24.]
# 随机抽取一个3*4的数组
print(np.random.choice(nd,size=(3,4)))
>>>[[15. 20. 21. 12.]
 	[23.  6.  3.  1.]
 	[24.  8.  4.  1.]]
# 随机抽取一个3*4的数组,但不重复
print(np.random.choice(nd,size=(3,4),replace=False))
>>>[[20.  1.  2. 21.]
 	[ 7.  3.  8. 22.]
 	[15. 17. 12.  9.]]
# 随机抽取一个3*4的数组,但依照指定概率
print(np.random.choice(nd,size = (3,4),p = nd/np.sum(nd)))
>>>[[19. 11. 22. 21.]
 	[22. 18. 19. 23.]
 	[24. 22. 16. 16.]]

related suggestion

[pyTorch Study Notes ①] Numpy Basics Part 1
[pyTorch Study Notes ②] Numpy Basics Part 2

Guess you like

Origin blog.csdn.net/qq_46319397/article/details/129534472