【问题标题】:What is the opposite of numpy.repeat?numpy.repeat 的反义词是什么?
【发布时间】:2016-11-15 19:03:02
【问题描述】:

here 给出的答案解释了如何调整数组的大小

[1,5,9]
[2,7,3]
[8,4,6]

[1,1,5,5,9,9]
[1,1,5,5,9,9]
[2,2,7,7,3,3]
[2,2,7,7,3,3]
[8,8,4,4,6,6]
[8,8,4,4,6,6]

使用np.repeat。给定较低的数组,将其缩小到上层的最佳方法是什么?

【问题讨论】:

    标签: python arrays numpy


    【解决方案1】:

    Slice 以适当的步长跨两个轴 -

    a[::2,::2] # 2 being stepsize here
    

    示例运行 -

    In [23]: a
    Out[23]: 
    array([[1, 1, 5, 5, 9, 9],
           [1, 1, 5, 5, 9, 9],
           [2, 2, 7, 7, 3, 3],
           [2, 2, 7, 7, 3, 3],
           [8, 8, 4, 4, 6, 6],
           [8, 8, 4, 4, 6, 6]])
    
    In [24]: a[::2,::2]
    Out[24]: 
    array([[1, 5, 9],
           [2, 7, 3],
           [8, 4, 6]])
    

    【讨论】:

      【解决方案2】:

      np.repeat(a, 重复, 轴=无)


      重复数组的元素。

      输入:

      a = 
      
      np.array([[1,5,9],
      [2,7,3],
      [8,4,6]])
      print (a)
      

      输出:

      Out[76]: 
      array([[1, 5, 9],
             [2, 7, 3],
             [8, 4, 6]])
      

      使用np.repeat

      In [78]: b = np.repeat(a,[2],axis = 1)
      
      In [79]: b
      Out[79]: 
      array([[1, 1, 5, 5, 9, 9],
             [2, 2, 7, 7, 3, 3],
             [8, 8, 4, 4, 6, 6]])
      
      In [80]: c = np.repeat(b,[2],axis = 0)
      
      In [81]: c
      Out[81]: 
      array([[1, 1, 5, 5, 9, 9],
             [1, 1, 5, 5, 9, 9],
             [2, 2, 7, 7, 3, 3],
             [2, 2, 7, 7, 3, 3],
             [8, 8, 4, 4, 6, 6],
             [8, 8, 4, 4, 6, 6]])
      

      【讨论】:

        猜你喜欢
        • 2011-01-04
        • 2014-04-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-07
        • 1970-01-01
        相关资源
        最近更新 更多