I have the following data
>>> import numpy as np
>>> original_classes=np.load("classes.npy")
>>> original_features=np.load("features.npy")
These NumPy arrays have the following shapes
>>> original_classes.shape
(12000,)
>>> original_features.shape
(12000, 224, 224, 3)
What I would like to do is to replace 2/3 of the original_features NumPy array with the content of a new array
>> new_features=np.load("new-features.npy")
>> new_features.shape
(600, 224, 224, 3)
However, these data must replace 600 of the positions in original_features Numpy array where the original_classes==11.
That means, there are a total of 12 unique classes in original_classes array, and there are 1000 features per class in original_features. I want to simply replace 600 features of class 11 with 600 features from new_features array, is that any way of doing that with python?
P.S= data can be found here
CodePudding user response:
First, we should find out which indices are for class 11:
items_11 = original_classes == 11
idx_11 = np.argwhere(items_11).ravel() # it gives the array of args equal to 11
EDIT
Then, we choose the last 600 items:
selected_idx = idx_11[len(idx_11)-600:]
Or you can select randomly:
size = ( 2 * len(idx_11) ) // 3
selected_idx = np.random.choice(idx_11, size=size, replace=False)
New Data:
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)
