【问题标题】:How to make multidimensional array by numpy.repeat [duplicate]如何通过 numpy.repeat 创建多维数组 [重复]
【发布时间】:2017-10-24 09:32:48
【问题描述】:

我想把这个多维数组写成软代码

adjust = np.array([[0.,1.,0.],
               [0.,1.,0.],
               [0.,1.,0.],
               [0.,1.,0.],
               [0.,1.,0.],
               [0.,1.,0.],
               [0.,1.,0.],
               [0.,1.,0.],
               [0.,1.,0.],   
               [0.,1.,0.]])

我为x = [0.,1.,0.] 尝试了np.repeat(x, 10, axis=0),它在同一个括号[] 中重复了它们。可以在这里使用 np.repeat 吗?还是其他 numpy?

还有,可以写软代码吗

adjust = np.array([[0.,0.,1.,0.,0.],
               [0.,0.,1.,0.,0.],
               [0.,0.,1.,0.,0.]])

将来我可能需要将左右 0 扩展为各种数字?

【问题讨论】:

    标签: python numpy multidimensional-array


    【解决方案1】:

    您可以在重复之前向数组添加另一个轴(注意我们重复了n = 1000 次)

    n = 1000
    %timeit adjust = np.repeat(np.array([0., 1., 0.])[None, :], n, axis=0)
    # 7.67 µs ± 940 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
    

    或者对repeat()的结果进行整形和转置

    %timeit adjust = np.repeat([0., 1., 0.], n, axis=0).reshape(3, -1).T
    # 22.5 µs ± 1.13 µs per loop (mean ± std. dev. of 7 runs, 100000 loops each)
    

    或使用广播

    %timeit adjust = np.array([0., 1., 0.]) * np.ones(n)[:, None]
    # 26.8 µs ± 880 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
    

    为了性能比较,艾伦的建议:

    %timeit adjust = np.asarray([ 0.,  1.,  0.] * n).reshape(n,-1)
    # 93.5 µs ± 7.87 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
    

    和 Divakars 的建议

    %timeit adjust = np.tile(np.array([0., 1., 0.]), (n, 1))
    # 11.1 µs ± 686 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
    

    结论:np.repeat()加另一个轴后最快。

    【讨论】:

      【解决方案2】:

      重复你的列表 n 次,把它放在一个 numpy 数组中,然后将它重新整形为 n 行。

      np.asarray([ 0.,  1.,  0.]*10).reshape(10,-1)
      Out[139]: 
      array([[ 0.,  1.,  0.],
             [ 0.,  1.,  0.],
             [ 0.,  1.,  0.],
             ..., 
             [ 0.,  1.,  0.],
             [ 0.,  1.,  0.],
             [ 0.,  1.,  0.]])
      

      第二个数组也是如此:

      np.asarray([0.,0.,1.,0.,0.]*3).reshape(3,-1)
      Out[140]: 
      array([[ 0.,  0.,  1.,  0.,  0.],
             [ 0.,  0.,  1.,  0.,  0.],
             [ 0.,  0.,  1.,  0.,  0.]])
      

      时间安排:

      %timeit np.asarray([ 0.,  1.,  0.]*10).reshape(10,-1)
      The slowest run took 14.97 times longer than the fastest. This could mean that an intermediate result is being cached.
      100000 loops, best of 3: 4.51 µs per loop
      
      %timeit np.repeat([0., 1., 0.], 10, axis=0).reshape(3, -1).T
      The slowest run took 4.44 times longer than the fastest. This could mean that an intermediate result is being cached.
      100000 loops, best of 3: 11.3 µs per loop
      
      %timeit np.array([0., 1., 0.]) * np.ones(10)[:, None]
      The slowest run took 10.28 times longer than the fastest. This could mean that an intermediate result is being cached.
      100000 loops, best of 3: 11.3 µs per loop
      

      【讨论】:

      • 虽然asarray 对于能够处理异构输入(列表、元组、ndarray)很有用,但在这种情况下,我们知道 [0.,1.,0.] 是一个列表并且应该使用array,它的执行速度会稍微快一些。也就是说,这个答案显然是最易读、最清晰的答案。
      猜你喜欢
      • 1970-01-01
      • 2019-07-18
      • 2017-07-03
      • 1970-01-01
      • 2011-06-25
      • 2019-08-18
      • 2019-04-28
      • 2011-11-24
      相关资源
      最近更新 更多