简体   繁体   English

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

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

I manipulated the MNIST dataset for research, by adding a set of digits to each digit in the dataset.我通过为数据集中的每个数字添加一组数字来操作 MNIST 数据集进行研究。

Before manipulation:操作前:

In:
x_train.shape

Out: 
(60000, 28, 28)

Expectated result after manipulation:操作后的预期结果:

In:
x_train_new.shape

Out: 
(60000, 11, 28, 28)

However I messed up and forgot to add x_train[i] in each iteration.但是我搞砸了,忘记在每次迭代中添加x_train[i] Therefore my shape is the following:因此我的形状如下:

In:
x_train_new.shape

Out: 
(60000, 10, 28, 28)

I have tried to apply np.insert , however I am struggling to apply it correctly since it is a multidimensional array:我试图应用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)

How can I insert x_train[i] to each ``ì```?如何将x_train[i]插入每个``ì```? Is it possible to insert the array as the first value in each array?是否可以将数组作为每个数组中的第一个值插入?

Any help is appreciated.任何帮助表示赞赏。 Thank you very much.非常感谢。

Just create an empty array只需创建一个空数组

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

and do并做

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