numpy 数组操作、变形

1.更改形状(对数组进行操作时,为了满足格式和计算的要求通常会改变其形状)

numpy.ndarray.shape表示数组的维度,返回一个元组,这个元组的长度就是维度的数目,即 ndim 属性(秩)。

import numpy as np
x = np.array([1, 2, 9, 4, 5, 6, 7, 8])
print(x.shape)  # (8,)
x.shape = [2, 4]
print(x)
# [[1 2 9 4]
#  [5 6 7 8]]

numpy.ndarray.flat 将数组转换为一维的迭代器,可以用for访问数组每一个元素。

import numpy as np

x = np.array([[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]])
y = x.flat
print(y)
# <numpy.flatiter object at 0x0000020F9BA10C60>
for i in y:
    print(i, end=' ')
# 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

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

numpy.ndarray.flatten([order='C']) 将数组的副本转换为一维数组,并返回。order:'C' -- 按行,'F' -- 按列,'A' -- 原顺序,'k' -- 元素在内存中的出现顺序。

import numpy as np
x = np.array([[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]])
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]
y[3] = 0
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]]

ravel()返回的是视图

import numpy as np

x = np.array([[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]])
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]

y[3] = 0
print(x)
# [[11 12 13  0 15]
#  [16 17 18 19 20]
#  [21 22 23 24 25]
#  [26 27 28 29 30]
#  [31 32 33 34 35]]
  • numpy.reshape(a, newshape[, order='C'])在不更改数据的情况下为数组赋予新的形状。reshape()函数当参数newshape = [rows,-1]时,将根据行数自动确定列数。
import numpy as np

x = np.arange(12)
y = np.reshape(x, [3, 4])
print(y.dtype)  # int32
print(y)
# [[ 0  1  2  3]
#  [ 4  5  6  7]
#  [ 8  9 10 11]]

y = np.reshape(x, [3, -1])
print(y)
# [[ 0  1  2  3]
#  [ 4  5  6  7]
#  [ 8  9 10 11]]

y = np.reshape(x,[-1,3])
print(y)
# [[ 0  1  2]
#  [ 3  4  5]
#  [ 6  7  8]
#  [ 9 10 11]]

reshape()函数当参数newshape = -1时,表示将数组降为一维。

import numpy as np

x = np.random.randint(12, size=[2, 2, 3])
print(x)
# [[[11  9  1]
#   [ 1 10  3]]
# 
#  [[ 0  6  1]
#   [ 4 11  3]]]
y = np.reshape(x, -1)
print(y)
# [11  9  1  1 10  3  0  6  1  4 11  3]

数组转置:

>>> x = np.random.rand(5, 5) * 10  #取随机数
>>> print(x)
[[5.05157895 3.42077127 9.60091724 7.67972514 5.33624506]
 [7.72144193 8.15188233 0.09831899 8.02862418 1.65304559]
 [0.79167253 2.16361239 0.25714688 1.78965687 7.51666443]
 [2.72353226 3.81206368 3.52424089 5.00717337 8.83226508]
 [5.32147403 9.1167779  8.79356762 4.59581239 2.89089182]]
>>> x = np.around(x, 2)  #保留两位小数
>>> print(x)
[[5.05 3.42 9.6  7.68 5.34]
 [7.72 8.15 0.1  8.03 1.65]
 [0.79 2.16 0.26 1.79 7.52]
 [2.72 3.81 3.52 5.01 8.83]
 [5.32 9.12 8.79 4.6  2.89]]

>>> y=x.T  #转置
>>> print(y)
[[5.05 7.72 0.79 2.72 5.32]
 [3.42 8.15 2.16 3.81 9.12]
 [9.6  0.1  0.26 3.52 8.79]
 [7.68 8.03 1.79 5.01 4.6 ]
 [5.34 1.65 7.52 8.83 2.89]]
>>> y = np.transpose(x)  
>>> print(y)
[[5.05 7.72 0.79 2.72 5.32]
 [3.42 8.15 2.16 3.81 9.12]
 [9.6  0.1  0.26 3.52 8.79]
 [7.68 8.03 1.79 5.01 4.6 ]
 [5.34 1.65 7.52 8.83 2.89]]
>>>

更改维度:当创建一个数组之后,还可以给它增加一个维度,这在矩阵计算中经常会用到。

import numpy as np

x = np.array([1, 2, 9, 4, 5, 6, 7, 8])
print(x.shape)  # (8,)  秩为1
print(x)  # [1 2 9 4 5 6 7 8]

y = x[np.newaxis, :]
print(y.shape)  # (1, 8)  秩为2
print(y)  # [[1 2 9 4 5 6 7 8]]

y = x[:, np.newaxis]
print(y.shape)  # (8, 1)  秩为2
print(y)
# [[1]
#  [2]
#  [9]
#  [4]
#  [5]
#  [6]
#  [7]
#  [8]]
  • numpy.squeeze(a, axis=None) 从数组的形状中删除单维度条目,即把shape中为1的维度去掉。
    • a表示输入的数组;
    • axis用于指定需要删除的维度,但是指定的维度必须为单维度,否则将会报错;
import numpy as np

x = np.array([[[0], [1], [2]]])   #第一个中括号一个元素,第二个有3个,第三个有1个元素,形状为1x3x1.
print(x.shape)  # (1, 3, 1)      #此形式为三维数组,有1个3X1的数组
print(x)
# [[[0]
#   [1]
#   [2]]]

y = np.squeeze(x)      #1x3x1 将维度为1的全部压缩。只剩三个数
print(y.shape)  # (3,)
print(y)  # [0 1 2]

y = np.squeeze(x, axis=0)  #将第一维的1压缩,只剩3x1
print(y.shape)  # (3, 1)
print(y)
# [[0]
#  [1]
#  [2]]

y = np.squeeze(x, axis=2) #将第三维压缩,只剩1x3
print(y.shape)  # (1, 3)
print(y)  # [[0 1 2]]

y = np.squeeze(x, axis=1)
# ValueError: cannot select an axis to squeeze out which has size not equal to one

数组组合:

如果要将两份数据组合到一起,就需要拼接操作

numpy.concatenate((a1, a2, ...), axis=0, out=None) Join a sequence of arrays along an existing axis

x = np.array([1, 2, 3])    #形成的是一维数组

import numpy as np

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]]
z = np.concatenate([x, y], axis=0)  #axis=0  按行添加
print(z)
# [[ 1  2  3]
#  [ 7  8  9]]
z = np.concatenate([x, y], axis=1)   #axis=1  按列添加
print(z)
# [[ 1  2  3  7  8  9]]
  • numpy.stack(arrays, axis=0, out=None)Join a sequence of arrays along a new axis.

沿着新的轴加入一系列数组

import numpy as np

x = np.array([1, 2, 3])
y = np.array([7, 8, 9])
z = np.stack([x, y])
print(z.shape)  # (2, 3)
print(z)
# [[1 2 3]
#  [7 8 9]]

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

数组拆分:

  • numpy.split(ary, indices_or_sections, axis=0) Split an array into multiple sub-arrays as views into ary.

垂直切分是把数组按照高度切分。

水平切分是把数组按照宽度切分。

参考:numpy.split()函数_CodingALife的博客-CSDN博客_numpy split

import numpy as np

x = np.array([[11, 12, 13, 14],
              [16, 17, 18, 19],
              [21, 22, 23, 24]])
y = np.hsplit(x, 2)
print(y)
# [array([[11, 12],
#        [16, 17],
#        [21, 22]]), array([[13, 14],
#        [18, 19],
#        [23, 24]])]

y = np.split(x, 2, axis=1)
print(y)
# [array([[11, 12],
#        [16, 17],
#        [21, 22]]), array([[13, 14],
#        [18, 19],
#        [23, 24]])]

y = np.hsplit(x, [3])
print(y)
# [array([[11, 12, 13],
#        [16, 17, 18],
#        [21, 22, 23]]), array([[14],
#        [19],
#        [24]])]

y = np.split(x, [3], axis=1)
print(y)
# [array([[11, 12, 13],
#        [16, 17, 18],
#        [21, 22, 23]]), array([[14],
#        [19],
#        [24]])]

y = np.hsplit(x, [1, 3])
print(y)
# [array([[11],
#        [16],
#        [21]]), array([[12, 13],
#        [17, 18],
#        [22, 23]]), array([[14],
#        [19],
#        [24]])]

y = np.split(x, [1, 3], axis=1)
print(y)
# [array([[11],
#        [16],
#        [21]]), array([[12, 13],
#        [17, 18],
#        [22, 23]]), array([[14],
#        [19],
#        [24]])]

参考:阿里云登录 - 欢迎登录阿里云,安全稳定的云计算服务平台

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值