简体   繁体   English

重复一个 numpy 数组 N 次

[英]repeating a numpy array N times

hii experts i have a 1d numpy array and i want to repeat it 3 times vertically,嗨,专家,我有一个 1d numpy 数组,我想垂直重复 3 次,

my_input_array = [-1.02295637 -0.60583836 -0.42240581 -0.78376377 -0.85821456]

i tried the below code我试过下面的代码

import numpy as np
x=np.loadtxt(my_input_array)
x.concatenate()

however i get error...in this way...hope i will get some solution.Thanks.但是我得到错误...以这种方式...希望我能得到一些解决方案。谢谢。

my expected output should be as below我的预期输出应该如下

 -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
 

Just use tile method which multiplies the array with given shape and reshape method to structure it.只需使用tile方法将数组与给定的形状相乘,并使用reshape方法来构造它。 Use x.shape[0]*x.shape[1] to change it into a column vector without explicitly giving the shape dimensions!使用x.shape[0]*x.shape[1]将其更改为列向量,而无需明确给出形状尺寸!

x=np.tile(x,(3,1))
y=x.reshape(x.shape[0]*x.shape[1])

numpy.tile

np.tile(my_input_array, 3)

Output输出

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

Edit: just noticed @hpaulj's answer.编辑:刚刚注意到@hpaulj 的回答。 I'll still leave my answer but he mentioned np.tile first.我仍然会留下我的答案,但他首先提到了 np.tile。

This is what you want:这就是你想要的:

x=np.concatenate([my_input_array, my_input_array, my_input_array])
for i in x:
    print(i)

Output:输出:

-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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM