1

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

mad
  • 2,677
  • 8
  • 35
  • 78

1 Answers1

1

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) 
A.Najafi
  • 691
  • 1
  • 9
  • 20
  • Thanks for your answer, I had to edit the question and now it is not 2/3 of the data, what I need to do is replace the last 600 elements with all elements from new_features. Could you please give me a hand on this? – mad Jan 11 '22 at 08:23
  • 1
    @mad is there any essence to choose the last 600 elements? – A.Najafi Jan 11 '22 at 08:25
  • I think you already answered my question. All I need to do is replace the size variable in your code with 600 correct? – mad Jan 11 '22 at 08:26
  • I put the wrong data, actually, new_features has 600 elements instead of 2000. – mad Jan 11 '22 at 08:27
  • 1
    @mad I edited the answer based on 600 items. – A.Najafi Jan 11 '22 at 08:29
  • 1
    I also liked your solution with randomization, could you post also that answer (the first you posted)?. – mad Jan 11 '22 at 08:35