强大的多维度数组与矩阵计算库
戳我~Python学习资料全套ling
# 列表l = [1,2,3,4,5,6]l[3]# 输出# 4# numpy数组(ndarray类型)n = np.array(l)n[3]# 输出# 4# 二维数组n = np.random.randint(0,10, size=(4,5))n# array([[1, 2, 5, 1, 5],# [5, 5, 6, 9, 8],# [3, 4, 2, 2, 0],# [4, 4, 8, 4, 3]])# 找到3n[3][4]n[-1][-1]# 简写n[3,4]n[-1,-1]# 三维数组n = np.random.randint(0, 100, size=(4,5,6))# 3个维度n[2,2,3]n[-2,-3,3]
# 定位到指定元素,直接修改n[2,2,3] = 6666# 修改一个数组n[0,0] = [1, 2, 3]n = np.zeros((6,6), dtype=int)n# 输出# array([[0, 0, 0, 0, 0, 0],# [0, 0, 0, 0, 0, 0],# [0, 0, 0, 0, 0, 0],# [0, 0, 0, 0, 0, 0],# [0, 0, 0, 0, 0, 0],# [0, 0, 0, 0, 0, 0]])# 修改1行n[0] = 1n# 输出# array([[1, 1, 1, 1, 1, 1],# [0, 0, 0, 0, 0, 0],# [0, 0, 0, 0, 0, 0],# [0, 0, 0, 0, 0, 0],# [0, 0, 0, 0, 0, 0],# [0, 0, 0, 0, 0, 0]])# 修改多行n[[0,3,-1]] = 2n# 输出# array([[2, 2, 2, 2, 2, 2],# [0, 0, 0, 0, 0, 0],# [0, 0, 0, 0, 0, 0],# [2, 2, 2, 2, 2, 2],# [0, 0, 0, 0, 0, 0],# [2, 2, 2, 2, 2, 2]])
# 列表切片l = [1,2,3,4,5,6]l[::-1]# 输出# [6, 5, 4, 3, 2, 1]# 一维数组n = np.arange(10)n# 输出# array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])n[::2]# 输出# array([0, 2, 4, 6, 8])n[::-1]# 输出# array([9, 8, 7, 6, 5, 4, 3, 2, 1, 0])# 多维数组n = np.random.randint(0,100, size=(5,6))n# 输出# array([[94, 45, 60, 71, 70, 88],# [43, 16, 39, 70, 14, 4],# [59, 12, 84, 38, 96, 88],# [80, 9, 72, 95, 69, 91],# [44, 84, 5, 47, 92, 31]])# 行 翻转n[::-1]# 列 翻转 (第二个维度)n[:, ::-1]
# cat.jpgcat = plt.imread('cat.jpg')#[[[231 186 131]# [232 187 132]# [233 188 133]# ...# [100 54 54]# [ 92 48 47]# [ 85 43 44]]# ...# ]cat.shape# 图片: 3维# (456, 730, 3)# 显示图片plt.imshow(cat)
# 上下翻转plt.imshow(cat[::-1])
# 左右翻转plt.imshow(cat[:,::-1])
# 第三个维度翻转,颜色翻转,模糊处理plt.imshow(cat[::10,::10,::-1])
n = np.arange(1, 21)n# array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20])n.shape# (20,)# 变成2维n2 = np.reshape(n, (4,5))n2# array([[ 1, 2, 3, 4, 5],# [ 6, 7, 8, 9, 10],# [11, 12, 13, 14, 15],# [16, 17, 18, 19, 20]])n2.shape# (4, 5)n2.reshape((20,)) # 变成一维n2.reshape((-1,)) # 变成一维# 改变cat的形状cat.shape# (456, 730, 3)# -1 表示行会自动分配,列数是6cat2 = cat.reshape((-1, 6))cat2.shape# (166440, 6)# -1 表示列会自动分配,行数是6cat2 = cat.reshape((6,-1))cat2.shape# (6, 166440)
n1 = np.random.randint(0,100, size=(4,5))n2 = np.random.randint(0,100, size=(4,5))display(n1, n2)# array([[48, 89, 82, 88, 55],# [63, 80, 77, 27, 51],# [11, 77, 90, 23, 71],# [ 4, 11, 19, 84, 57]])# #array([[86, 26, 71, 62, 46],# [75, 43, 84, 87, 99],# [34, 33, 58, 56, 29],# [56, 32, 53, 43, 5]])# 级联,合并# 上下合并:垂直级联np.concatenate((n1, n2))np.concatenate((n1, n2), axis=0) # axis=0表示行,第一个维度# array([[48, 89, 82, 88, 55],# [63, 80, 77, 27, 51],# [11, 77, 90, 23, 71],# [ 4, 11, 19, 84, 57],# [86, 26, 71, 62, 46],# [75, 43, 84, 87, 99],# [34, 33, 58, 56, 29],# [56, 32, 53, 43, 5]])# 左右合并:水平级联np.concatenate((n1, n2), axis=1) # axis=1表示列,第二个维度# array([[48, 89, 82, 88, 55, 86, 26, 71, 62, 46],# [63, 80, 77, 27, 51, 75, 43, 84, 87, 99],# [11, 77, 90, 23, 71, 34, 33, 58, 56, 29],# [ 4, 11, 19, 84, 57, 56, 32, 53, 43, 5]])
# 左右合并:水平级联np.hstack((n1, n2)) # array([[48, 89, 82, 88, 55, 86, 26, 71, 62, 46],# [63, 80, 77, 27, 51, 75, 43, 84, 87, 99],# [11, 77, 90, 23, 71, 34, 33, 58, 56, 29],# [ 4, 11, 19, 84, 57, 56, 32, 53, 43, 5]])# 上下合并:垂直级联np.vstack((n1, n2)) # array([[48, 89, 82, 88, 55],# [63, 80, 77, 27, 51],# [11, 77, 90, 23, 71],# [ 4, 11, 19, 84, 57],# [86, 26, 71, 62, 46],# [75, 43, 84, 87, 99],# [34, 33, 58, 56, 29],# [56, 32, 53, 43, 5]])
n = np.random.randint(0, 100, size=(6,4))n# array([[ 3, 90, 62, 89],# [75, 7, 10, 76],# [77, 94, 88, 59],# [78, 66, 81, 83],# [18, 88, 40, 81],# [ 2, 38, 26, 21]])# 垂直方向,平均切成3份np.vsplit(n, 3)# [array([[ 3, 90, 62, 89],# [75, 7, 10, 76]]),# array([[77, 94, 88, 59],# [78, 66, 81, 83]]),# array([[18, 88, 40, 81],# [ 2, 38, 26, 21]])]# 如果是数组np.vsplit(n, (1,2,4))# [array([[ 3, 90, 62, 89]]),# array([[75, 7, 10, 76]]),# array([[77, 94, 88, 59],# [78, 66, 81, 83]]),# array([[18, 88, 40, 81],# [ 2, 38, 26, 21]])]# 水平方向np.hsplit(n, 2)# [array([[97, 86],# [16, 70],# [26, 95],# [ 6, 83],# [97, 43],# [96, 57]]),# array([[88, 69],# [60, 7],# [32, 82],# [24, 86],# [62, 23],# [43, 19]])]# 通过axis来按照指定维度拆分np.split(n, 2, axis=1)# [array([[97, 86],# [16, 70],# [26, 95],# [ 6, 83],# [97, 43],# [96, 57]]),# array([[88, 69],# [60, 7],# [32, 82],# [24, 86],# [62, 23],# [43, 19]])]
cat.shape# (456, 730, 3)# 拆分cat2 = np.split(cat, 2, axis=0)cat2[0]plt.imshow(cat2[0])
# 拆分成5分cat2 = np.split(cat, 5, axis=1)plt.imshow(cat2[2])
# 赋值: 不使用copyn1 = np.arange(10)n2 = n1n1[0] = 100display(n1, n2)# array([100, 1, 2, 3, 4, 5, 6, 7, 8, 9])# array([100, 1, 2, 3, 4, 5, 6, 7, 8, 9])# 拷贝: copyn1 = np.arange(10)n2 = n1.copy()n1[0] = 100display(n1, n2)# array([100, 1, 2, 3, 4, 5, 6, 7, 8, 9])# array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
# 转置n = np.random.randint(0, 10, size=(3, 4)) n.T # transpose改变数组维度n = np.random.randint(0, 10, size=(3, 4, 5)) # shape(3, 4, 5)np.transpose(n, axes=(2,0,1))