重复一个 numpy 数组 N 次
repeating a numpy array N times
hii 专家我有一个 1d numpy 数组,我想垂直重复它 3 次,
my_input_array = [-1.02295637 -0.60583836 -0.42240581 -0.78376377 -0.85821456]
我尝试了下面的代码
import numpy as np
x=np.loadtxt(my_input_array)
x.concatenate()
但是我得到错误...这样...希望我能得到一些solution.Thanks。
我的预期输出应该如下
-1.02295637
-0.60583836
-0.42240581
-0.78376377
-0.85821456
-1.02295637
-0.60583836
-0.42240581
-0.78376377
-0.85821456
-1.02295637
-0.60583836
-0.42240581
-0.78376377
-0.85821456
这就是你想要的:
x=np.concatenate([my_input_array, my_input_array, my_input_array])
for i in x:
print(i)
输出:
-1.02295637
-0.60583836
-0.42240581
-0.78376377
-0.85821456
-1.02295637
-0.60583836
-0.42240581
-0.78376377
-0.85821456
-1.02295637
-0.60583836
-0.42240581
-0.78376377
-0.85821456
只需使用 tile
方法将数组与给定形状相乘,然后使用 reshape
方法构造它。使用 x.shape[0]*x.shape[1]
将其更改为列向量而不显式给出形状尺寸!
x=np.tile(x,(3,1))
y=x.reshape(x.shape[0]*x.shape[1])
np.tile(my_input_array, 3)
输出
array([-1.02295637, -0.60583836, -0.42240581, -0.78376377, -0.85821456,
-1.02295637, -0.60583836, -0.42240581, -0.78376377, -0.85821456,
-1.02295637, -0.60583836, -0.42240581, -0.78376377, -0.85821456])
编辑:刚注意到@hpaulj 的回答。我仍然会留下我的答案,但他首先提到 np.tile。
hii 专家我有一个 1d numpy 数组,我想垂直重复它 3 次,
my_input_array = [-1.02295637 -0.60583836 -0.42240581 -0.78376377 -0.85821456]
我尝试了下面的代码
import numpy as np
x=np.loadtxt(my_input_array)
x.concatenate()
但是我得到错误...这样...希望我能得到一些solution.Thanks。
我的预期输出应该如下
-1.02295637
-0.60583836
-0.42240581
-0.78376377
-0.85821456
-1.02295637
-0.60583836
-0.42240581
-0.78376377
-0.85821456
-1.02295637
-0.60583836
-0.42240581
-0.78376377
-0.85821456
这就是你想要的:
x=np.concatenate([my_input_array, my_input_array, my_input_array])
for i in x:
print(i)
输出:
-1.02295637
-0.60583836
-0.42240581
-0.78376377
-0.85821456
-1.02295637
-0.60583836
-0.42240581
-0.78376377
-0.85821456
-1.02295637
-0.60583836
-0.42240581
-0.78376377
-0.85821456
只需使用 tile
方法将数组与给定形状相乘,然后使用 reshape
方法构造它。使用 x.shape[0]*x.shape[1]
将其更改为列向量而不显式给出形状尺寸!
x=np.tile(x,(3,1))
y=x.reshape(x.shape[0]*x.shape[1])
np.tile(my_input_array, 3)
输出
array([-1.02295637, -0.60583836, -0.42240581, -0.78376377, -0.85821456,
-1.02295637, -0.60583836, -0.42240581, -0.78376377, -0.85821456,
-1.02295637, -0.60583836, -0.42240581, -0.78376377, -0.85821456])
编辑:刚注意到@hpaulj 的回答。我仍然会留下我的答案,但他首先提到 np.tile。