繁体   English   中英

如何通过索引将数组插入到现有的多维数组中? [Python]

[英]How to insert an array into existing multi-dimensional array by index? [Python]

我通过为数据集中的每个数字添加一组数字来操作 MNIST 数据集进行研究。

操作前:

In:
x_train.shape

Out: 
(60000, 28, 28)

操作后的预期结果:

In:
x_train_new.shape

Out: 
(60000, 11, 28, 28)

但是我搞砸了,忘记在每次迭代中添加x_train[i] 因此我的形状如下:

In:
x_train_new.shape

Out: 
(60000, 10, 28, 28)

我试图应用np.insert ,但是我正在努力正确应用它,因为它是一个多维数组:

tryout = x_train_new[0]

In:
np.insert(tryout, 0, x_train[0])

Out:
ValueError: could not broadcast input array from shape (28,28) into shape (28)

如何将x_train[i]插入每个``ì```? 是否可以将数组作为每个数组中的第一个值插入?

任何帮助表示赞赏。 非常感谢。

只需创建一个空数组

x_train_expected = np.empty((60000, 11, 28, 28))

并做

x_train_expected[:,1:] = x_train_new
x_train_expected[:,0] = x_train

暂无
暂无

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

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