![](/img/trans.png)
[英]How to append/replace an item from multi dimensional array by an item from another multi-dimensional array based on a specific condition
[英]Replace 2/3 of a multi-dimensional array given a condition
我有以下数据
>>> import numpy as np
>>> original_classes=np.load("classes.npy")
>>> original_features=np.load("features.npy")
这些 NumPy arrays 具有以下形状
>>> original_classes.shape
(12000,)
>>> original_features.shape
(12000, 224, 224, 3)
我想做的是用新数组的内容替换original_features NumPy 数组的 2/3
>> new_features=np.load("new-features.npy")
>> new_features.shape
(600, 224, 224, 3)
但是,这些数据必须替换original_features Numpy 数组中 original_classes==11 的600 个位置。
这意味着original_classes数组中共有 12 个唯一类, original_features中的每个 class 有 1000 个特征。 我想简单地用new_features数组中的 600 个功能替换 class 11 的 600 个功能,用 python 可以做到这一点吗?
PS=数据可以在这里找到
首先,我们应该找出 class 11 的索引:
items_11 = original_classes == 11
idx_11 = np.argwhere(items_11).ravel() # it gives the array of args equal to 11
编辑
然后,我们选择最后 600 个项目:
selected_idx = idx_11[len(idx_11)-600:]
或者你可以随机 select:
size = ( 2 * len(idx_11) ) // 3
selected_idx = np.random.choice(idx_11, size=size, replace=False)
新数据:
mask = np.ones(len(original_features), dtype=bool) # all elements included/True.
mask[selected_idx] = False
new_x = original_features[mask]
new_y = original_classes[mask]
new_x = np.concatenate([new_x,new_features],axis=0)
new_y = np.concatenate([new_y,np.ones(len(new_features)) * 11],axis=0)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.