数组的综合变换应用

一.数组拓展维度

import numpy as np
line=np.arange(24)
print(line)---------->[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]

将line形状改成2层3行4列
(1)方法一: .reshape(2,3,4)—–>创建一个数组并使其维度为(2,3,4)

result=line.reshape(2,3,4) #不修改数组本身,返回新的数组(相当于复制一份,不改变元数据)
print(result)-------->[[[ 0  1  2  3]      #两层三行四列数组
                        [ 4  5  6  7]
                        [ 8  9 10 11]]

                       [[12 13 14 15]
                        [16 17 18 19]
                        [20 21 22 23]]]

print(line)---------->[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23] 
#原数组line没有改变                

(2)方法二: .shape=(2,3,4)—–>使一维数组维度等于元组(2,3,4)

line.shape=(2,3,4) #修改数组的形状且会修改数组本身
print(line)---------->[[[ 0  1  2  3]
                        [ 4  5  6  7]
                        [ 8  9 10 11]]

                       [[12 13 14 15]
                        [16 17 18 19]
                        [20 21 22 23]]]

(3)方法三:

line.resize(2,3,4) #修改数组的形状且会修改数组本身
print(line)---------->[[[ 0  1  2  3]
                        [ 4  5  6  7]
                        [ 8  9 10 11]]

                       [[12 13 14 15]
                        [16 17 18 19]
                        [20 21 22 23]]]

二.将多维数据展平为一行数据

(1)方法一:————->.ravel(),修改d1的的一个元素,d3也跟着改变

d3=np.arange(24).reshape(2,3,4)
print(d3)--------------------->[[[ 0  1  2  3]
                                 [ 4  5  6  7]
                                 [ 8  9 10 11]]

                                [[12 13 14 15]
                                 [16 17 18 19]
                                 [20 21 22 23]]]
d1=d3.ravel()
print(d1)--------------------->[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]
#修改d1的的一个元素,d3也跟着改变

d1[0]=1
print(d3)--------------------->[[[ 1  1  2  3]
                                 [ 4  5  6  7]
                                 [ 8  9 10 11]]

                                [[12 13 14 15]
                                 [16 17 18 19]
                                 [20 21 22 23]]]

(2)方法二:————->.flatten() ,修改d2的的一个元素,d3不改变

d2=d3.flatten() 
print(d2)--------------------->[ 1  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]
#修改d2的的一个元素,d3不改变

d2[0]=2
print(d2)--------------------->[ 2  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]

print(d3)--------------------->[[[ 1  1  2  3]
                                 [ 4  5  6  7]
                                 [ 8  9 10 11]]

                                [[12 13 14 15]
                                 [16 17 18 19]
                                 [20 21 22 23]]]

三.数组的转置

将4行6列的数组转置成6行4列的数组

arr=np.arange(24).reshape(4,6)
print(arr)---------------->[[ 0  1  2  3  4  5]
                            [ 6  7  8  9 10 11]
                            [12 13 14 15 16 17]
                            [18 19 20 21 22 23]]

arr_T=arr.transpose()
print(arr_T)-------------->[[ 0  6 12 18]
                            [ 1  7 13 19]
                            [ 2  8 14 20]
                            [ 3  9 15 21]
                            [ 4 10 16 22]
                            [ 5 11 17 23]
#行转为列

四.数组的组合

将arr1和arr2两个数组进行组合

arr1=np.arange(9).reshape(3,3)
print(arr1)---------------->[[0 1 2]
                             [3 4 5]
                             [6 7 8]]
arr2=arr1**2
print(arr2)---------------->[[ 0  1  4]
                             [ 9 16 25]
                             [36 49 64]]

(1)方法一:vastack按垂直方向拼接数组v—verhical

result1=np.vstack((arr1,arr2))
print(result1)-------------->[[ 0  1  2]
                              [ 3  4  5]
                              [ 6  7  8]
                              [ 0  1  4]
                              [ 9 16 25]
                              [36 49 64]]

(2)方法二:hstack按水平方向拼接数组h—horizonal

hstack=np.hstack((arr1,arr2))
print(hstack)---------------->[[ 0  1  2  0  1  4]
                               [ 3  4  5  9 16 25]
                               [ 6  7  8 36 49 64]]

(3)方法三:column_stack按列方向进行合并 ————————->和hstack一样
#按列组合,行数量不变

column_stack=np.column_stack((arr1,arr2))
print(column_stack)---------->[[ 0  1  2  0  1  4]
                               [ 3  4  5  9 16 25]
                               [ 6  7  8 36 49 64]]

(4)方法四:row_stack按行方向进行合并 ——————————>和vastack一样
#按行组合,列数量不变

扫描二维码关注公众号,回复: 1123712 查看本文章
row_stack=np.row_stack((arr1,arr2))
print(row_stack)-------------->[[ 0  1  2]
                                [ 3  4  5]
                                [ 6  7  8]
                                [ 0  1  4]
                                [ 9 16 25]
                                [36 49 64]]

五.数组的分割

(1)水平分割 hsplit(arg,num) 和 split(arg,num,axis=1)

full=np.arange(9).reshape(3,3)
print(full)----------------------------------->[[0 1 2]
                                                [3 4 5]
                                                [6 7 8]]
hsplit=np.hsplit(full,3)
axis0=np.split(full,3,axis=1)
print(axis0) #结果一样
print(hsplit)--------------------------------->[array([[0],
                                                       [3],
                                                       [6]]), 
                                                array([[1],
                                                       [4],
                                                       [7]]), 
                                                array([[2],
                                                       [5],
                                                       [8]])]
print(axis0[0])------------------------------->[[0]
                                                [3]
                                                [6]]
print(axis0[1])------------------------------->[[1]
                                                [4]
                                                [7]]
print(axis0[2])------------------------------->[[2]
                                                [5]
                                                [8]]                                            

(2)垂直分割 vsplit(arg,num) 和 split(arg,num,axis=0)

vsplit=np.vsplit(full,3)
axis1=np.split(full,3,axis=0)
print(vsplit)
print(axis1)---------------------->[array([[0, 1, 2]]), array([[3, 4, 5]]), array([[6, 7, 8]])]
print(axis1[0])------------------->[[0 1 2]]
print(axis1[1])------------------->[[3 4 5]]
print(axis1[2])------------------->[[6 7 8]]

(3)深度分割 dsplit(arr,num)
(将一系列数组沿着纵轴(深度)方向进行层叠组合)

d3=np.arange(8).reshape(2,2,2)
print(d3)------------------------------------->[[[0 1]
                                                 [2 3]]

                                                [[4 5]
                                                 [6 7]]]

dsplit=np.dsplit(d3,2)

print("dsplit:\n",dsplit)--------------------->dsplit:
                                                     [array([[[0],
                                                              [2]],

                                                             [[4],
                                                              [6]]]), 
                                                      array([[[1],
                                                              [3]],

                                                             [[5],
                                                              [7]]])]

print("dsplit_0:\n",dsplit[0])----------------->dsplit_0:
                                                         [[[0]
                                                           [2]]

                                                          [[4]
                                                           [6]]]

print("dsplit_1:\n",dsplit[1])----------------->dsplit_1:
                                                         [[[1]
                                                           [3]]

                                                          [[5]
                                                           [7]]]

六.数组合并

(1)行合并:vstack , concatenate((arg1,arg2),axis=0) 和 row_stack

m1=np.arange(9).reshape(3,3)
print(m1)----------------------------------->[[0 1 2]
                                              [3 4 5]
                                              [6 7 8]]
m2=m1*2
print(m2)----------------------------------->[[ 0  2  4]
                                              [ 6  8 10]
                                              [12 14 16]]

vstack()
concatenate((arg1,arg2),axis=0)
row_stack()

concate1=np.concatenate((m1,m2),axis=0)
vstack=np.vstack((m1,m2))
row_stack=np.row_stack((m1,m2))
print(concate1)
print(vstack)   #结果一样
print(row_stack)---------------------------->[[ 0  1  2]
                                              [ 3  4  5]
                                              [ 6  7  8]
                                              [ 0  2  4]
                                              [ 6  8 10]
                                              [12 14 16]]

(2)列合并:hstack , concatenate((arg1,arg2),axis=1) 和 column_stack
hstack()
concatenate((arg1,arg2),axis=1)
column_stack()
“”“

concate2=np.concatenate((m1,m2),axis=1)
hstack=np.hstack((m1,m2))
column_stack=np.column_stack((m1,m2))
print(concate2)
print(hstack)      #结果一样
print(column_stack)------------------------->[[ 0  1  2  0  2  4]
                                              [ 3  4  5  6  8 10]
                                              [ 6  7  8 12 14 16]]

(3)深度合并dstack()

dstack=np.dstack((m1,m2))
print(dstack)------------------------------->[[[ 0  0]
                                               [ 1  2]
                                               [ 2  4]]

                                              [[ 3  6]
                                               [ 4  8]
                                               [ 5 10]]

                                              [[ 6 12]
                                               [ 7 14]
                                               [ 8 16]]]
m3=np.arange(9,18).reshape(3,3)
print(m3)----------------------------------->[[ 9 10 11]
                                              [12 13 14]
                                              [15 16 17]]
dstack1=np.dstack((m1,m2,m3))
print(dstack1.shape)------------------------>(3,3,3)
print(dstack1)------------------------------>[[[ 0  0  9]
                                               [ 1  2 10]
                                               [ 2  4 11]]
    #行变列进行深度组合                                      
                                              [[ 3  6 12]
                                               [ 4  8 13]
                                               [ 5 10 14]]

                                              [[ 6 12 15]
                                               [ 7 14 16]
                                               [ 8 16 17]]]

猜你喜欢

转载自blog.csdn.net/messi_james/article/details/80460835
今日推荐