将新元素添加到三维数组NUMPY中



我有一个数组,其形状等于(1,59,1(它看起来如下:

[[[0.93169003]
[0.96923472]
[0.97881434]
[0.99266784]
[0.97358235]
............
[0.83777312]
[0.82086134]]]

我希望我可以在末尾添加新的元素,它等于[[0.86442673]],这样我的数组的形状将等于(1,60,1(,并且看起来如下:

[[[0.93169003]
[0.96923472]
[0.97881434]
[0.99266784]
[0.97358235]
............
[0.83777312]
[0.82086134]
[0.86442673]]]

我试过np.append,但它对我不起作用。请帮我

尝试:

arr=np.append(arr,[[[0.86442673]]], axis=1)

其中arr是您的输入阵列

从文档中:"注意,追加不会发生在适当的位置:分配并填充了一个新数组"。你必须将结果分配给一个变量才能得到结果。

X_test = np.append(X_test ,pred_price)

这取决于您如何使用numpy.insert

import numpy as np
d=np.random.randint(1,3,(2,5,3))
print(type(d),'n',d.shape,'n',d)

给出

<class 'numpy.ndarray'> 
(2, 5, 3) 
[[[1 1 2]
[2 2 2]
[1 1 2]
[2 2 2]
[2 1 1]]
[[2 2 2]
[2 2 2]
[2 1 2]
[1 1 1]
[1 2 2]]]

然后

#numpy.insert(arr, obj, values, axis=None)[source]   obj is the index, axis is the dimension number
e=np.insert(d,1,5,0)
print(f'en{e}')
f=np.insert(d,1,5,1)
print(f'fn{f}')
g=np.insert(d,1,5,2)
print(f'gn{g}')

给出

e
[[[1 1 2]
[2 2 2]
[1 1 2]
[2 2 2]
[2 1 1]]
[[5 5 5]
[5 5 5]
[5 5 5]
[5 5 5]
[5 5 5]]
[[2 2 2]
[2 2 2]
[2 1 2]
[1 1 1]
[1 2 2]]]
f
[[[1 1 2]
[5 5 5]
[2 2 2]
[1 1 2]
[2 2 2]
[2 1 1]]
[[2 2 2]
[5 5 5]
[2 2 2]
[2 1 2]
[1 1 1]
[1 2 2]]]
g
[[[1 5 1 2]
[2 5 2 2]
[1 5 1 2]
[2 5 2 2]
[2 5 1 1]]
[[2 5 2 2]
[2 5 2 2]
[2 5 1 2]
[1 5 1 1]
[1 5 2 2]]]