如何通过索引将数组插入到现有的多维数组中? [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
我通过为数据集中的每个数字添加一组数字来操纵 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