Numpy学习——Task02 数组操作

1.改变形状(shape)

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

(1) shape

通过修改 shape 属性来改变数组的形状。

import numpy as np

x = np.array([1, 2, 9, 4, 5, 6, 7, 8])
print(x.shape)  # (8,) 查看数组形状
x.shape = [2, 4] #改变shape 列表内数字乘积为8即可
print(x)
# [[1 2 9 4]
#  [5 6 7 8]]

(2)flat

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   #此时y 与x 数据指向相同,即改变一者的数据另外一个也会有相同变化
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]]

(3)flatten()

  • numpy.ndarray.flatten([order='C']) 将数组的副本转换为一维数组,并返回。

  • order:‘C’ – 按行,‘F’ – 按列,默认为‘C’

    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]]
    
    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(order='F')
    print(y)
    # [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]
    

(4)reshape()

  • numpy.reshape(a, newshape[, order='C'])在不更改数据的情况下为数组赋予新的形状。

  • reshape()函数当参数newshape = [rows,-1]时,将根据行数自动确定列数。

  • reshape()函数当参数newshape = -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]]
    
    y[0, 1] = 10
    print(x)
    # [ 0 10  2  3  4  5  6  7  8  9 10 11](改变x去reshape后y中的值,x对应元素也改变)
    
    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]
    

2.数组转置

transpose / T

  • numpy.transpose(a, axes=None)Permute the dimensions of an array.

  • numpy.ndarray.T Same as self.transpose(), except that self is returned if self.ndim < 2.

    import numpy as np
    
    x = np.random.rand(5, 5) * 10
    x = np.round(x, 2)#四舍五入,保留两位小数
    print(x)
    # [[6.74 8.46 6.74 5.45 1.25]
    #  [3.54 3.49 8.62 1.94 9.92]
    #  [5.03 7.22 1.6  8.7  0.43]
    #  [7.5  7.31 5.69 9.67 7.65]
    #  [1.8  9.52 2.78 5.87 4.14]]
    y = x.T
    print(y)
    # [[6.74 3.54 5.03 7.5  1.8 ]
    #  [8.46 3.49 7.22 7.31 9.52]
    #  [6.74 8.62 1.6  5.69 2.78]
    #  [5.45 1.94 8.7  9.67 5.87]
    #  [1.25 9.92 0.43 7.65 4.14]]
    y = np.transpose(x)
    print(y)
    # [[6.74 3.54 5.03 7.5  1.8 ]
    #  [8.46 3.49 7.22 7.31 9.52]
    #  [6.74 8.62 1.6  5.69 2.78]
    #  [5.45 1.94 8.7  9.67 5.87]
    #  [1.25 9.92 0.43 7.65 4.14]]
    

3.更改维度

(1)增加维度

很多工具包在进行计算时都会先判断输入数据的维度是否满足要求,如果输入数据达不到指定的维度时,可以使用newaxis参数来增加一个维度。

  • numpy.newaxis = None None的别名,对索引数组很有用。

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

(2)删除维度

  • numpy.squeeze(a, axis=None) 从数组的形状中删除单维度条目,即把shape中为1的维度去掉。

  • a表示输入的数组;

  • axis用于指定需要删除的维度,但是指定的维度必须为单维度,否则将会报错;

    import numpy as np
    
    x = np.array([[[0], [1], [2]]])
    print(x.shape)  # (1, 3, 1)
    print(x)
    # [[[0]
    #   [1]
    #   [2]]]
    
    y = np.squeeze(x)
    print(y.shape)  # (3,)
    print(y)  # [0 1 2]
    
    y = np.squeeze(x, axis=0)
    print(y.shape)  # (3, 1)
    print(y)
    # [[0]
    #  [1]
    #  [2]]
    
    y = np.squeeze(x, axis=2)
    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
    

4.数组组合

(1)concatenate()

(2)stack()

(3)vstack()

(4)hstack()

5.数组拆分

(1)split()

(2)vsplit()

(3)hsplit()

6.数组平铺

(1)tile()

(2)repeat()

7.添加和删除元素

unique()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值