为每次迭代向数组追加值



我正试图将测试数组中的索引存储在另外两个数组中。

train_idx = np.zeros(3,dtype='object')
test_dx = np.zeros(3,dtype='object')
array = np.array([1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0, 1])
test = np.array(np.array_split(array, k))
[array([1, 0, 0, 1, 0]) array([1, 1, 1, 0]) array([1, 0, 0, 1])]

在测试中的数组中,我试图将每个数组存储在不同的数组中(train_index和test_index(。

for i in range(len(test)):
test_index = test[i]
for j in range(len(test)):
if i == j:
continue
train_index = test[j]

这个问题是它存储了数组train_index和test_index 的最后一个值

print(train_index)
print(test_index)
[1 1 1 0]
[1 0 0 1]

而完整输出如下所示:

[1 0 0 1 0]
[1 1 1 0]
[1 0 0 1]
[1 1 1 0]
[1 0 0 1 0]
[1 0 0 1]
[1 0 0 1]
[1 0 0 1 0]
[1 1 1 0]

事实上,我想做的是将这些值附加到它们各自的数组中,我试图实现的输出如下:

test_index = [[1 0 0 1 0], [1 1 1 0], [1 0 0 1]]
train_index = [[[1 1 1 0], [1 0 0 1]], [[1 1 1 0], [1 0 0 1]],  [[1 0 0 1], [1 1 1 0]]]

将test_index和train_index定义为空列表:

test_index = []

然后使用附加方法:

test_index.append(test[i])

最新更新