Repeat given array to more complex shape.

huangapple go评论66阅读模式
英文:

Repeat given array to more complex shape

问题

arr = np.tile(np.tile(np.linspace(start=10, stop=40, num=4), (3, 1)), (3, 1, 1))
英文:

I want to create an array of shape (3, 3, 4). The data to populate the array with is given.

My solution right now works perfectly fine but feels like I am missing a numpy lesson here. I do not want to do multiple .repeat()s over and over.

start = np.linspace(start=10, stop=40, num=4)
arr = np.repeat([start], 3, axis=0)
arr = np.repeat([arr], 3, axis=0)
arr

# output
array([[[10., 20., 30., 40.],
        [10., 20., 30., 40.],
        [10., 20., 30., 40.]],

       [[10., 20., 30., 40.],
        [10., 20., 30., 40.],
        [10., 20., 30., 40.]],

       [[10., 20., 30., 40.],
        [10., 20., 30., 40.],
        [10., 20., 30., 40.]]])

答案1

得分: 1

以下是翻译好的代码部分:

通过首先使用np.linspace()创建一个一维数组,然后使用np.tile()将该数组重复到所需的形状。

以下是更新后的代码:

import numpy as np

start = np.linspace(start=10, stop=40, num=4)
arr = np.tile(start, (3, 3, 1))

print(arr)

我认为这是创建所需数组的更简洁方法。

以下是您获得的输出:

[[[10. 20. 30. 40.]
  [10. 20. 30. 40.]
  [10. 20. 30. 40.]]

 [[10. 20. 30. 40.]
  [10. 20. 30. 40.]
  [10. 20. 30. 40.]]

 [[10. 20. 30. 40.]
  [10. 20. 30. 40.]
  [10. 20. 30. 40.]]]

在上述代码中,np.tile() 用于将 start 数组重复到形状 (3, 3, 1)。最后一个维度 (1) 被添加以匹配所需的形状 (3, 3, 4) 并正确分配值。

英文:

The array mentioned by you can be created by first creating a 1D array with np.linspace() and then using np.tile() to repeat that array in the required shape.

Following is the updated code:

import numpy as np

start = np.linspace(start=10, stop=40, num=4)
arr = np.tile(start, (3, 3, 1))

print(arr)

I think this is a more concise way to create the array you require.

Following is the output that you get:

[[[10. 20. 30. 40.]
  [10. 20. 30. 40.]
  [10. 20. 30. 40.]]

 [[10. 20. 30. 40.]
  [10. 20. 30. 40.]
  [10. 20. 30. 40.]]

 [[10. 20. 30. 40.]
  [10. 20. 30. 40.]
  [10. 20. 30. 40.]]]

In the above code, np.tile() is used to repeat the start array in the shape of (3, 3, 1). The last dimension (1) is added to match the required shape of (3, 3, 4) and distribute the values correctly.

答案2

得分: 1

有多种方法可以创建你期望的数组,但请记住,由于广播,你的数组在许多上下文中可能会自动像一个形状为(3, 3, 4)的数组一样运作。

在这种情况下,我认为创建你期望的数组最优雅的方法是通过np.broadcast_to来"强制"广播:

import numpy as np

start = np.linspace(start=10, stop=40, num=4)

out = np.broadcast_to(start, (3, 3, 4))

输出:

array([[[10., 20., 30., 40.],
        [10., 20., 30., 40.],
        [10., 20., 30., 40.]],

       [[10., 20., 30., 40.],
        [10., 20., 30., 40.],
        [10., 20., 30., 40.]],

       [[10., 20., 30., 40.],
        [10., 20., 30., 40.],
        [10., 20., 30., 40.]]])
英文:

There are various ways to create your desired array, but keep in mind that your array could automagically function like a (3, 3, 4) shaped array in many contexts due to broadcasting.

In this case, I think the most elegant way to create your desired array is to "force" the broadcasting via np.broadcast_to:

import numpy as np

start = np.linspace(start=10, stop=40, num=4)

out = np.broadcast_to(start, (3, 3, 4))

out:

array([[[10., 20., 30., 40.],
        [10., 20., 30., 40.],
        [10., 20., 30., 40.]],

       [[10., 20., 30., 40.],
        [10., 20., 30., 40.],
        [10., 20., 30., 40.]],

       [[10., 20., 30., 40.],
        [10., 20., 30., 40.],
        [10., 20., 30., 40.]]])

答案3

得分: 0

另一个选项除了"tile"之外是使用broadcast_to。在这种情况下,

start = np.linspace(start=10, stop=40, num=4)
arr = np.broadcast_to(start,(3,3,4))

可以得到所期望的结果。

英文:

Another option besides tile is to use broadcast_to. In this case,

start = np.linspace(start=10, stop=40, num=4)
arr = np.broadcast_to(start,(3,3,4))

leads to the desired result.

huangapple
  • 本文由 发表于 2023年4月13日 23:45:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76007394.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定