Use numpy to generate random multidimensional matrices and perform dimension conversion

Use numpy to generate random multidimensional matrices and perform dimension conversion

Introduction to numpy

Previous review: The previous blog of the column explained the installation of numpy and used numpy to generate random vectors--> The delivery address of the previous blog

Generate random multidimensional matrices using numpy

  • Use numpy to generate a random integer matrix of length 553:
import numpy as np
"""
low:随机数最小值
high:随机数最大值
size:数据维度,可以自定义任意维度数据
"""
data = np.random.randint(low=0, high=10, size=(5, 5, 3))
print(data.shape)
结果:
(5, 5, 3)
  • Use numpy to generate a random floating point matrix of length 553:
data = np.random.uniform(low=0, high=10, size=size=(5, 5, 3))
print(data.shape)
结果:
size=(5, 5, 3)

Convert matrix dimensions using numpy

Use np.transpose interface to process matrices

data = np.random.randint(low=0, high=10, size=(5, 5, 3))
print(f"转换前矩阵维度:{data.shape}")
# 维度转换
"""
transpose第一个参数是需要做处理的数据
transpose第二个参数是维度重新排布的顺序
"""
new_data = np.transpose(data, (2, 0, 1))
print(f"转换后矩阵维度:{new_data.shape}")
结果:
转换前矩阵维度:(5, 5, 3)
转换后矩阵维度:(3, 5, 5)

in conclusion

Numpy is a commonly used library in Python and a must-have library in deep learning. In the next blog, we will use numpy for numerical search and filling.

Guess you like

Origin blog.csdn.net/Silver__Wolf/article/details/132350025