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

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

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

    1. import numpy as np
    2. x = np.array([1, 2, 9, 4, 5, 6, 7, 8])
    3. print(x.shape) # (8,)
    4. x.shape = [2, 4]
    5. print(x)
    6. # [[1 2 9 4]
    7. # [5 6 7 8]]
    • numpy.ndarray.flat 将数组转换为一维的迭代器,可以用for访问数组每一个元素。

    【例】

    1. import numpy as np
    2. x = np.array([[11, 12, 13, 14, 15],
    3. [16, 17, 18, 19, 20],
    4. [21, 22, 23, 24, 25],
    5. [26, 27, 28, 29, 30],
    6. [31, 32, 33, 34, 35]])
    7. y = x.flat
    8. print(y)
    9. # <numpy.flatiter object at 0x0000020F9BA10C60>
    10. for i in y:
    11. print(i, end=' ')
    12. # 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
    13. y[3] = 0
    14. print(end='\n')
    15. print(x)
    16. # [[11 12 13 0 15]
    17. # [16 17 18 19 20]
    18. # [21 22 23 24 25]
    19. # [26 27 28 29 30]
    20. # [31 32 33 34 35]]
    • numpy.ndarray.flatten([order='C'])将数组的副本转换为一维数组,并返回。
      • order:’C’ — 按行,’F’ — 按列,’A’ — 原顺序,’k’ — 元素在内存中的出现顺序。(简记)
      • order:{‘C / F,’A,K},可选使用此索引顺序读取a的元素。’C’意味着以行大的C风格顺序对元素进行索引,最后一个轴索引会更改F表示以列大的Fortran样式顺序索引元素,其中第一个索引变化最快,最后一个索引变化最快。请注意,’C’和’F’选项不考虑基础数组的内存布局,仅引用轴索引的顺序.A’表示如果a为Fortran,则以类似Fortran的索引顺序读取元素在内存中连续,否则类似C的顺序。“ K”表示按照步序在内存中的顺序读取元素,但步幅为负时反转数据除外。默认情况下,使用Cindex顺序。

    【例】flatten()函数返回的是拷贝。

    1. import numpy as np
    2. x = np.array([[11, 12, 13, 14, 15],
    3. [16, 17, 18, 19, 20],
    4. [21, 22, 23, 24, 25],
    5. [26, 27, 28, 29, 30],
    6. [31, 32, 33, 34, 35]])
    7. y = x.flatten()
    8. print(y)
    9. # [11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
    10. # 35]
    11. y[3] = 0
    12. print(x)
    13. # [[11 12 13 14 15]
    14. # [16 17 18 19 20]
    15. # [21 22 23 24 25]
    16. # [26 27 28 29 30]
    17. # [31 32 33 34 35]]
    18. x = np.array([[11, 12, 13, 14, 15],
    19. [16, 17, 18, 19, 20],
    20. [21, 22, 23, 24, 25],
    21. [26, 27, 28, 29, 30],
    22. [31, 32, 33, 34, 35]])
    23. y = x.flatten(order='F')
    24. print(y)
    25. # [11 16 21 26 31 12 17 22 27 32 13 18 23 28 33 14 19 24 29 34 15 20 25 30
    26. # 35]
    27. y[3] = 0
    28. print(x)
    29. # [[11 12 13 14 15]
    30. # [16 17 18 19 20]
    31. # [21 22 23 24 25]
    32. # [26 27 28 29 30]
    33. # [31 32 33 34 35]]
    • numpy.ravel(a, order='C')Return a contiguous flattened array.

    【例】ravel()返回的是视图。

    1. import numpy as np
    2. x = np.array([[11, 12, 13, 14, 15],
    3. [16, 17, 18, 19, 20],
    4. [21, 22, 23, 24, 25],
    5. [26, 27, 28, 29, 30],
    6. [31, 32, 33, 34, 35]])
    7. y = np.ravel(x)
    8. print(y)
    9. # [11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
    10. # 35]
    11. y[3] = 0
    12. print(x)
    13. # [[11 12 13 0 15]
    14. # [16 17 18 19 20]
    15. # [21 22 23 24 25]
    16. # [26 27 28 29 30]
    17. # [31 32 33 34 35]]

    【例】order=F 就是拷贝

    1. x = np.array([[11, 12, 13, 14, 15],
    2. [16, 17, 18, 19, 20],
    3. [21, 22, 23, 24, 25],
    4. [26, 27, 28, 29, 30],
    5. [31, 32, 33, 34, 35]])
    6. y = np.ravel(x, order='F')
    7. print(y)
    8. # [11 16 21 26 31 12 17 22 27 32 13 18 23 28 33 14 19 24 29 34 15 20 25 30
    9. # 35]
    10. y[3] = 0
    11. print(x)
    12. # [[11 12 13 14 15]
    13. # [16 17 18 19 20]
    14. # [21 22 23 24 25]
    15. # [26 27 28 29 30]
    16. # [31 32 33 34 35]]
    • numpy.reshape(a, newshape[, order='C'])在不更改数据的情况下为数组赋予新的形状。

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

    1. import numpy as np
    2. x = np.arange(12)
    3. y = np.reshape(x, [3, 4])
    4. print(y.dtype) # int32
    5. print(y)
    6. # [[ 0 1 2 3]
    7. # [ 4 5 6 7]
    8. # [ 8 9 10 11]]
    9. y = np.reshape(x, [3, -1])
    10. print(y)
    11. # [[ 0 1 2 3]
    12. # [ 4 5 6 7]
    13. # [ 8 9 10 11]]
    14. y = np.reshape(x,[-1,3])
    15. print(y)
    16. # [[ 0 1 2]
    17. # [ 3 4 5]
    18. # [ 6 7 8]
    19. # [ 9 10 11]]
    20. y[0, 1] = 10
    21. print(x)
    22. # [ 0 10 2 3 4 5 6 7 8 9 10 11](改变x去reshape后y中的值,x对应元素也改变)

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

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