![](/img/trans.png)
[英]How to ffill nan values in a numpy array using the last non-nan values repeating N times
[英]repeating a numpy array N times
嗨,专家,我有一个 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
只需使用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。
这就是你想要的:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.