数组的操作(三)

数组形状的转换

x.shape=[a,b]

将 x 的形状转换为 a行b列

import numpy as np

x = np.arange(1,9)
print(x)

# [1 2 3 4 5 6 7 8] 

这里导入 numpy 后,下面演示代码不再展示导入 numpy 的代码

下面所有的 np 都代表 numpy

打印下 x 的形状和纬度

x.shape

# (8,)

x.ndim
# 1

改变 x 的形状

x.shape = [2,4]
print(x)

# [[1 2 3 4]
# [5 6 7 8]]

np.reshpae(x,[row,column]) 将 x 转换为 row行,column列的数组,并返回转换后的数组,不改变原数组的数据和形状,返回的是一个视图

x = np.arange(11,36).reshape(5,-1)

y = np.reshape(x,[5,5])
print(y)
# [[11 12 13 14 15]
# [16 17 18 19 20]
# [21 22 23 24 25]
# [26 27 28 29 30]
# [31 32 33 34 35]]

当 row 或 column 指定为 -1 时,将自动计算数值
如 z = np.reshape(x,[5,-1] 将 x 转化为 5行的数组,自动计算列数

x = np.arange(11,36).reshape(5,-1)
print(x)

# [[11 12 13 14 15]
# [16 17 18 19 20]
# [21 22 23 24 25]
# [26 27 28 29 30]
# [31 32 33 34 35]]

w = np.reshape(x,[-1,5])
print(w)
# [[11 12 13 14 15]
# [16 17 18 19 20]
# [21 22 23 24 25]
# [26 27 28 29 30]
# [31 32 33 34 35]]

x.reshape() 返回的是一个视图

w[0] = 999
print(x)

# [[999 999 999 999 999]
# [ 16  17  18  19  20]
# [ 21  22  23  24  25]
# [ 26  27  28  29  30]
# [ 31  32  33  34  35]]

reshape() 函数如果参数 newshape = -1 , 表示将数组降为一维

x = np.random.randint(12,size=[2,2,3])
print(x)
print()
y = np.reshape(x,-1)
print(y)

# [[[ 0  4 10]
#  [ 2  5  7]]
#
# [[ 2  8 11]
#  [ 5  2  7]]]
#
# [ 0  4 10  2  5  7  2  8 11  5  2  7]

数组的平铺

np.ndarray.flat 返回的是一个一维数组的迭代器,是原数据的副本

x = np.arange(11,36).reshape(5,-1)

y = x.flat

for i in y:
    print(i,end='\t')
    
# 11	12	13	14	15	16	17	18	19	20	21	22	23	24	25	26	27	28	29	30	31	32	33	34	35

注意,x.flat 返回的是一个视图,也是一个迭代器

y[3] = 999

print(y)
# <numpy.flatiter object at 0x000001D8D3949740>

print(x)
# [[ 11  12  13 999  15]
# [ 16  17  18  19  20]
# [ 21  22  23  24  25]
# [ 26  27  28  29  30]
# [ 31  32  33  34  35]]

np.ndarray.flatten(order=‘C’) 返回的是一个一维数组,是一个副本

  • order = ‘C’ 表示按行平铺

  • order = ‘F’ 表示按列平铺

x = np.arange(11,36).reshape(5,-1)

y = x.flatten()
print(y)
# [11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
# 35]

x.flatten() 返回的是一个数组,不是迭代器

返回的是一个副本

y[3] = 999
print(x)
# [[11 12 13 14 15]
# [16 17 18 19 20]
# [21 22 23 24 25]
# [26 27 28 29 30]
# [31 32 33 34 35]]

z = x.flatten(order='F')
print(z)

# [11 16 21 26 31 12 17 22 27 32 13 18 23 28 33 14 19 24 29 34 15 20 25 30
# 35]
 
z[0] = 999
print(z)
print()
print(x)

# [999  16  21  26  31  12  17  22  27  32  13  18  23  28  33  14  19  24
#  29  34  15  20  25  30  35]

# [[11 12 13 14 15]
# [16 17 18 19 20]
# [21 22 23 24 25]
# [26 27 28 29 30]
# [31 32 33 34 35]]

x = np.arange(11,36).reshape(5,-1)

y = np.ravel(x)
print(y)

# [11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
# 35]

数组转置

  • np.transpose(a,axis = None)
  • np.ndarray.T
  • self.transpose()

三者均为数组的转置

x = np.random.rand(5,5)*10
x = np.around(x,2)

print(x)

# [[9.42 0.23 5.68 8.79 3.69]
# [7.98 2.68 6.85 6.84 0.41]
# [6.17 3.58 3.61 1.21 4.03]
# [8.99 9.24 6.72 5.8  4.51]
# [4.96 6.08 5.92 6.07 3.14]]

np.ndarray.T

y = x.T
print(y)

# [[9.42 7.98 6.17 8.99 4.96]
# [0.23 2.68 3.58 9.24 6.08]
# [5.68 6.85 3.61 6.72 5.92]
# [8.79 6.84 1.21 5.8  6.07]
# [3.69 0.41 4.03 4.51 3.14]]

np.transpose(a,axis = None)

z = np.transpose(x)
print(z)

# [[9.42 7.98 6.17 8.99 4.96]
# [0.23 2.68 3.58 9.24 6.08]
# [5.68 6.85 3.61 6.72 5.92]
# [8.79 6.84 1.21 5.8  6.07]
# [3.69 0.41 4.03 4.51 3.14]]

self.transpose()

w = x.transpose()
print(w)

# [[9.42 7.98 6.17 8.99 4.96]
# [0.23 2.68 3.58 9.24 6.08]
# [5.68 6.85 3.61 6.72 5.92]
# [8.79 6.84 1.21 5.8  6.07]
# [3.69 0.41 4.03 4.51 3.14]]

更改纬度

np.newaxis = None

有时候,我们需要将一个一维向量转换为二位的数组,这时就可以使用 np.newaxis

x = np.arange(1,9)
print(x.ndim)
print(x.shape)
print(x)

# 1
# (8,)
# [1 2 3 4 5 6 7 8]

y = x[np.newaxis,:]
print(y.ndim)
print(y.shape)
print(y)

# 2
# (1, 8)
# [[1 2 3 4 5 6 7 8]]

z = x[:,np.newaxis]
print(z.ndim)
print(z.shape)
print(z)

# 2
# (8, 1)
# [[1]
# [2]
# [3]
# [4]
# [5]
# [6]
# [7]
# [8]]

数组组合

  • np.concatenate()
  • np.stack()
  • np.vstack()
  • np.hstack()

np.concatenate([x,y] , axis = 0) 表示宽度合并

np.concatenate([x,y] , axis = 1) 表示高度合并

axis 默认为 0

x = np.array([1,2,3])
y = np.array([4,5,6])
z = np.concatenate([x,y])

print(z)

# [1 2 3 4 5 6]

w = np.concatenate([x,y] , axis = 0)
print(w)

# [1 2 3 4 5 6]

np.concatenate() 对于一维数组只能进行宽度合并


s = np.concatenate([x,y] , axis = 1)
print(s)

# AxisError                                 Traceback (most recent call last)
# <ipython-input-5-65eb7b0e4291> in <module>
# ----> 1 s = np.concatenate([x,y] , axis = 1)
#      2 print(s)

# <__array_function__ internals> in concatenate(*args, **kwargs)

# AxisError: axis 1 is out of bounds for array of dimension 1

原来x,y都是二维的,拼接后的结果仍然是二维的

np.concatenate([x,y]) 默认表示进行高度拼接,即行拼接

x = np.array([1,2,3]).reshape(1,3)
y = np.array([7,8,9]).reshape(1,3)

# 默认按行进行拼接
z = np.concatenate([x,y])

print(z)

# [[1 2 3]
# [7 8 9]]

axis=0 表示行拼接

w = np.concatenate([x,y],axis=0)
print(w)

# [[1 2 3]
# [7 8 9]]

axis = 1 表示列拼接

s = np.concatenate([x,y] , axis = 1)
print(s)

# [[1 2 3 7 8 9]]

np.concatenate() 默认是将元素在原来的维度上进行拼接,即行拼接,增加行数

x = np.array([[1,2,3],[4,5,6]])
y = np.array([[7,8,9],[10,11,12]])

z = np.concatenate([x,y])

print(z)

# [[ 1  2  3]
# [ 4  5  6]
# [ 7  8  9]
# [10 11 12]]

行拼接

w = np.concatenate([x,y],axis = 0)
print(w)

# [[ 1  2  3]
# [ 4  5  6]
# [ 7  8  9]
# [10 11 12]]

列拼接

s =  np.concatenate([x,y],axis=1)
print(s)

# [[ 1  2  3  7  8  9]
# [ 4  5  6 10 11 12]]

np.stack( arrays ,axis = 0) 数组拼接方法

np.stack() 与 np.concatenate() 类似,默认表示在元素原来的维度上进行拼接,即高度拼接,增加元素的行数

x = np.array([1,2,3])
y = np.array([4,5,6])

z = np.stack([x,y])

print(z)

# [[1 2 3]
# [4 5 6]]

指定 axis = 0,表示高度拼接

w = np.stack([x,y],axis=0)
print(w)

# [[1 2 3]
# [4 5 6]]

指定 axis = 1,表示宽度拼接

s = np.stack([x,y],axis=1)
print(s)

# [[1 4]
# [2 5]
# [3 6]]

关于数组的拼接,numpy 提供了专门的函数

  • np.vstack(x,y) 行合并,增加行数
  • np.hstack(x,y) 列合并,增加列数
x = np.array([1, 2, 3])
y = np.array([7, 8, 9])

z = np.vstack([x, y])
print(z.shape)
print(z)

# (2, 3)
# [[1 2 3]
# [7 8 9]]
 
w = np.hstack([x,y])
print(w.shape)
print(w)

# (6,)
# [1 2 3 7 8 9]

小结

np.concatenate([x,y],axis=0) = np.vstack([x,y])

np.concatenate([x,y],axis=1) = np.hstack([x,y)

x = np.array([1,2,3]).reshape(1,3)
y = np.array([7,8,9]).reshape(1,3)

z = np.vstack([x,y])

print(z)

# [[1 2 3]
# [7 8 9]]
 
w = np.hstack([x,y])

print(w)
# [[1 2 3 7 8 9]]

z = np.concatenate([x,y])

print(z)
# [[1 2 3]
# [7 8 9]]
 
z = np.concatenate([x,y] , axis = 0)
print(z)

# [[1 2 3]
# [7 8 9]]

w = np.concatenate([x,y] , axis = 1)

print(w)
# [[1 2 3 7 8 9]]

数组拆分

  • np.split()
  • np.vsplit()
  • np.hsplit()

np.split(x , [a,b,c] , axis = 0) = np.vsplit( x , [ a,b,c ] )

将 x 数组按照索引为 a,b,c 拆分,axis表示高度拆分

np.split(x , [a,b,c] , axis = 1) = np.hsplit(x , [ a,b,c ] )

将 x 数组按照索引为 a,b,c 拆分,axis表示宽度拆分

x = np.arange(11,23).reshape(3,-1)

print(x)

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

y = np.split(x,[1])

print(y)
# [array([[11, 12, 13, 14]]), array([[15, 16, 17, 18],
#       [19, 20, 21, 22]])]

在第一行处划分数据,会将第二行的数据划分到第二组中

y , w = np.split(x,[1])

print(y.shape)
print(y)

print()

print(w.shape)
print(w)

# (1, 4)
# [[11 12 13 14]]

# (2, 4)
# [[15 16 17 18]
# [19 20 21 22]]

y = np.split(x,[1],axis = 1)
print(y)

# [array([[11],
#       [15],
#       [19]]), array([[12, 13, 14],
#       [16, 17, 18],
#       [20, 21, 22]])]

在第一列处划分数据,会将第二列的数据划分到第二组中

y , w = np.split(x,[1],axis = 1)
print(y.shape)
print(y)
print()
print(w.shape)
print(w)

# (3, 1)
# [[11]
# [15]
# [19]]
# 
# (3, 3)
# [[12 13 14]
# [16 17 18]
# [20 21 22]]

使用 np.split() 返回的数据都是二维数组,即使划分后某个数组只有一列元素

numpy 提供的有专门用于划分数组的方法

np.vsplit(x,[1,3]) 按照高度进行划分,相当于 np.split(x,[1,3],axis=0)

np.hsplit(x,[1,3]) 按照宽度进行划分,相当于 np.split(x,[1,3),axis=1)

x = np.arange(20).reshape(4,-1)
print(x)

# [[ 0  1  2  3  4]
# [ 5  6  7  8  9]
# [10 11 12 13 14]
# [15 16 17 18 19]]

split(axis = 0) 相当于 vsplit()

使用 split(axis = 0)

y , w , s = np.split(x,[1,3],axis=0)

print(y)
print()
print(w)
print()
print(s)

# [[0 1 2 3 4]]
#
# [[ 5  6  7  8  9]
# [10 11 12 13 14]]

# [[15 16 17 18 19]]

使用 vsplit()

y , w , s = np.vsplit(x,[1,3])

print(y)
print()
print(w)
print()
print(s)

# [[0 1 2 3 4]]
#
# [[ 5  6  7  8  9]
# [10 11 12 13 14]]
#
# [[15 16 17 18 19]]

split(axis = 1) 相当于 hsplit()

使用 split(axis=1)

y , w , s = np.split(x,[1,3],axis=1)

print(y)
print()
print(w)
print()
print(s)

# [[ 0]
# [ 5]
# [10]
# [15]]
#
# [[ 1  2]
# [ 6  7]
# [11 12]
# [16 17]]
#
# [[ 3  4]
# [ 8  9]
# [13 14]
# [18 19]]

使用 hsplit()

y , w , s = np.hsplit(x,[1,3])

print(y)
print()
print(w)
print()
print(s)

# [[ 0]
# [ 5]
# [10]
# [15]]
#
# [[ 1  2]
# [ 6  7]
# [11 12]
# [16 17]]

# [[ 3  4]
# [ 8  9]
# [13 14]
# [18 19]]
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值