NumPy科学计算库学习_009_NumPy数组的形状操作

NumPy数组的变形

arr1 = np.random.randint(0,10,size=(2,3,4))
arr2 = arr1.reshape(2,12)
arr3 = arr1.reshape(4,-1)
arr4 = arr1.reshape(-1,8)
print("【arr1】\n",arr1)
print("【arr2】\n",arr2)
print("【arr3,固定行数=4,自动判断列数】\n",arr3)
print("【arr4,自动判断行数,固定列数=8】\n",arr4)
【arr1】
 [[[4 7 6 7]
  [5 4 3 0]
  [0 2 3 5]]

 [[0 8 9 7]
  [6 6 3 2]
  [4 9 2 1]]]
【arr2】
 [[4 7 6 7 5 4 3 0 0 2 3 5]
 [0 8 9 7 6 6 3 2 4 9 2 1]]
【arr3,固定行数=4,自动判断列数】
 [[4 7 6 7 5 4]
 [3 0 0 2 3 5]
 [0 8 9 7 6 6]
 [3 2 4 9 2 1]]
【arr4,自动判断行数,固定列数=8】
 [[4 7 6 7 5 4 3 0]
 [0 2 3 5 0 8 9 7]
 [6 6 3 2 4 9 2 1]]

NumPy数组的转置

arr1 = np.random.randint(0,10,size=(2,3))
arr2 = arr1.T

arr3 = np.random.randint(0,10,size=(2,3,4))
arr4 = np.transpose(arr3, axes=(1,0,2)) # axe中表达的是放在size中的索引

print("【arr1】\n",arr1)
print("【arr2】\n",arr2)
print("")
print("【arr3】\n",arr3)
print("【arr4,改变NumPy数组的维度】\n",arr4)
【arr1】
 [[9 3 1]
 [2 6 9]]
【arr2】
 [[9 2]
 [3 6]
 [1 9]]

【arr3】
 [[[2 7 5 5]
  [9 3 4 1]
  [7 2 1 0]]

 [[9 2 6 5]
  [6 7 9 0]
  [8 6 5 5]]]
【arr4,改变NumPy数组的维度】
 [[[2 7 5 5]
  [9 2 6 5]]

 [[9 3 4 1]
  [6 7 9 0]]

 [[7 2 1 0]
  [8 6 5 5]]]

NumPy数组堆叠

arr1 = np.array([[1,2,3]])
arr2 = np.array([[4,5,6]])
print("【arr1】\n",arr1)
print("【arr2】\n",arr2)
print("【第1维度串联】\n",np.concatenate([arr1,arr2],axis=0))
print("【第2维度串联】\n",np.concatenate([arr1,arr2],axis=1))
print("【水平方向堆叠】\n",np.hstack((arr1,arr2)))
print("【垂直方向堆叠】\n",np.vstack((arr1,arr2)))
【arr1】
 [[1 2 3]]
【arr2】
 [[4 5 6]]
【第1维度串联】
 [[1 2 3]
 [4 5 6]]
【第2维度串联】
 [[1 2 3 4 5 6]]
【水平方向堆叠】
 [[1 2 3 4 5 6]]
【垂直方向堆叠】
 [[1 2 3]
 [4 5 6]]

NumPy数组的拆分 split()

  • 创建一个NumPy数组

\[{arr6by5}= \begin{bmatrix} {1}&{4}&{9}&{1}&{4}\\ {5}&{0}&{0}&{2}&{1}\\ {8}&{6}&{9}&{7}&{5}\\ {6}&{0}&{1}&{8}&{2}\\ {7}&{4}&{2}&{5}&{3}\\ {8}&{0}&{3}&{7}&{8}\\ \end{bmatrix} \]

arr6by5 = np.array([[1,4,9,1,4],[5,0,0,2,1],[8,6,9,7,5],[6,0,1,8,2],[7,4,2,5,3],[8,0,3,7,8]]) # size = 6 by 5
print("【arr6by5】\n",arr6by5)
【arr6by5】
 [[1 4 9 1 4]
 [5 0 0 2 1]
 [8 6 9 7 5]
 [6 0 1 8 2]
 [7 4 2 5 3]
 [8 0 3 7 8]]
  • 平分第1维度的数据

\[\color{red} { \begin{bmatrix} {1}&{4}&{9}&{1}&{4}\\ {5}&{0}&{0}&{2}&{1}\\ \end{bmatrix} } \\ \color{blue} { \begin{bmatrix} {8}&{6}&{9}&{7}&{5}\\ {6}&{0}&{1}&{8}&{2}\\ \end{bmatrix} } \\ \color{brown} { \begin{bmatrix} {7}&{4}&{2}&{5}&{3}\\ {8}&{0}&{3}&{7}&{8}\\ \end{bmatrix} } \]

arr6by5_1 = np.split(arr6by5,indices_or_sections=3,axis=0)
print("【arr6by5_1,在第1维度的6中,分成3份】\n",arr6by5_1)
【arr6by5_1,在第1维度的6中,分成3份】
 [array([[1, 4, 9, 1, 4],
       [5, 0, 0, 2, 1]]), array([[8, 6, 9, 7, 5],
       [6, 0, 1, 8, 2]]), array([[7, 4, 2, 5, 3],
       [8, 0, 3, 7, 8]])]
  • 编辑第2维度的数据

\[\color{red}{\begin{bmatrix} {1}&{4}\\ {5}&{0}\\ {8}&{6}\\ {6}&{0}\\ {7}&{4}\\ {8}&{0}\\ \end{bmatrix}} \color{blue}{\begin{bmatrix} {9}\\ {0}\\ {9}\\ {1}\\ {2}\\ {3}\\ \end{bmatrix}} \color{brown}{\begin{bmatrix} {1}&{4}\\ {2}&{1}\\ {7}&{5}\\ {8}&{2}\\ {5}&{3}\\ {7}&{8}\\ \end{bmatrix}} \]

arr6by5_2 = np.split(arr1,indices_or_sections=[2,3],axis=1)
print("【arr6by5_2,在第2维度的5中,索引2和3开始为断点,进行分割】\n",arr6by5_2)
【arr6by5_2,在第2维度的5中,索引2和3为断点,进行分割】
 [array([[1, 4],
       [5, 0],
       [8, 6],
       [6, 0],
       [7, 4],
       [8, 0]]), array([[9],
       [0],
       [9],
       [1],
       [2],
       [3]]), array([[1, 4],
       [2, 1],
       [7, 5],
       [8, 2],
       [5, 3],
       [7, 8]])]
  • 竖直方向将NumPy数组进行分配【本质是横向切】

\[\color{red} { \begin{bmatrix} {1}&{4}&{9}&{1}&{4}\\ {5}&{0}&{0}&{2}&{1}\\ {8}&{6}&{9}&{7}&{5}\\ \end{bmatrix} } \\ \color{blue} { \begin{bmatrix} {6}&{0}&{1}&{8}&{2}\\ {7}&{4}&{2}&{5}&{3}\\ {8}&{0}&{3}&{7}&{8}\\ \end{bmatrix} } \]

arr6by5_3 = np.vsplit(arr6by5, indices_or_sections=2)
print("【arr6by5_3】\n",arr6by5_3)
【arr6by5_3】
 [array([[1, 4, 9, 1, 4],
       [5, 0, 0, 2, 1],
       [8, 6, 9, 7, 5]]), array([[6, 0, 1, 8, 2],
       [7, 4, 2, 5, 3],
       [8, 0, 3, 7, 8]])]
  • 水平方向将NumPy数组进行分配【本质是纵向切】

\[\color{red}{\begin{bmatrix} {1}\\ {5}\\ {8}\\ {6}\\ {7}\\ {8}\\ \end{bmatrix}} \color{blue}{\begin{bmatrix} {4}&{9}&{1}\\ {0}&{0}&{2}\\ {6}&{9}&{7}\\ {0}&{1}&{8}\\ {4}&{2}&{5}\\ {0}&{3}&{7}\\ \end{bmatrix}} \color{brown}{\begin{bmatrix} {4}\\ {1}\\ {5}\\ {2}\\ {3}\\ {8}\\ \end{bmatrix}} \]

arr6by5_4 = np.hsplit(arr6by5, indices_or_sections=[1,4]) # 从第1列开始、从第4列开始
print("【arr6by5_4,水平方向,以索引1和4为断点分割NumPy数组】\n",arr6by5_4)
【arr6by5_4,水平方向,以索引1和4为断点分割NumPy数组】
 [array([[1],
       [5],
       [8],
       [6],
       [7],
       [8]]), array([[4, 9, 1],
       [0, 0, 2],
       [6, 9, 7],
       [0, 1, 8],
       [4, 2, 5],
       [0, 3, 7]]), array([[4],
       [1],
       [5],
       [2],
       [3],
       [8]])]
posted @ 2023-01-03 07:35  顺心无忧  阅读(7)  评论(0编辑  收藏  举报