当前位置: 代码迷 >> 综合 >> 骚气的Python之Numpy
  详细解决方案

骚气的Python之Numpy

热度:2   发布时间:2024-01-20 03:50:20.0

假定一组数据

a = np.array([[1, 2, 3],[4, 5, 6]
])
  1. 数据位移(第一列向后移动一位)
    a[:,0] = np.roll(a[:,0], 1)

    结果:

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

     

  2.  条件修改
    a[a%2==0] = 0

    结果:

    [[1 0 3][0 5 0]]

     

  3. 转为一维

    a.flatten()

    结果:

    [1 2 3 4 5 6]

     

  4. 重构

    a.flatten().reshape(3,2)

    结果:

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

     

  5.  转置

    a.T

     结果:

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

     

  6.  

    删除行或列

     

     

    a = np.delete(a, 0, axis=0)   # 删除第一行
    a = np.delete(a, [0, 1], axis=0)   # 删除第一行和第二行
    a = np.delete(a, [0, 1], axis=1)   # 删除第一列和第二列
    

     

  7. 增加行或列
    a = np.concatenate([a,a], axis=0)
     结果:
    [[1 2 3][4 5 6][1 2 3][4 5 6]]
    a = np.concatenate([a,a], axis=1)

    结果:

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

     

  8. 重复
    a = np.repeat(a, 2, axis=1)

    结果:

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

     

  相关解决方案