0

I have a numpy array (ndarray, each of shape (1, 10, 4)) of input features X, and a label array y corresponding to classes.

Now I want to create a new array of arrays by stacking together, all input features in X that correspond to class 0 in y, features that correspond to class 1 in y, etc... so that a new array of arrays is formed, with nested the arrays equal to the number classes.

As a minimal example, say I have:

X = np.random.randn(200, 1, 10, 4)
a = np.zeros(100, dtype=int)
b = np.ones(100, dtype=int)
y = np.hstack((a,b))

So,

print(X.shape)
print(y.shape)
(200, 1, 10, 4)
(200,)

New array then should then be like:

Final = [array(#all_features_of class_0), array(#all_features_of_class_1)...]

My intention is to plot these features per class to understand their distribution.

If it may help, every single observation has 4 features, so the 4 in (1, 10, 4).

1 Answer 1

1

did I understood you correct, is this what you want?

class0, class1=[],[]
for i in range(len(y)):
if y[i]==0:
    class0.append(X[i])
else:
    class1.append(X[i])
Final = (np.array(class0) , np.array(class1))
Sign up to request clarification or add additional context in comments.

2 Comments

yes, but then len(class0) gives 100 and len(class1) gives 1 , and in the MWE, all classes have 100 elements..
Both classes are of len 100, as I see in my output, and it just depends upon number of 0 and 1 array y.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.