重复 numpy 数组 N 次

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()

但是我收到错误...以这种方式...希望我能得到一些解决方案。谢谢。


我的预期输出应该如下


 -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


慕桂英3389331
浏览 109回答 3
3回答

慕的地10843

numpy.tilenp.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。

慕姐4208626

只需使用将tile数组与给定形状相乘的方法和reshape方法来构造它。用于x.shape[0]*x.shape[1]将其更改为列向量,而无需明确给出形状尺寸!x=np.tile(x,(3,1)) y=x.reshape(x.shape[0]*x.shape[1])

RISEBY

这就是你想要的: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
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python