0

I was trying to perform mean clustering on my dataset. The dataset X is of the dimension (182,108,130). The mean is calculated using np.mean(X, axis = 1). The mean is of dimension (182,130). Now I'd like to subtract the mean from the dataset. How do I achieve it without a for loop?

I have tried to directly subtract X - mean but returned error

`operands could not be broadcast together with shapes (182,108,130) (130,182) `

I'd expect the mean being subtracted in each block.

1 Answer 1

0

You need to use keepdims=True while calculating mean. Example code below:

x = np.random.random((180,108,130)) # random init
x_mean = x.mean(axis=1, keepdims=True) # shape (180, 1, 130)

x - x_mean

Another option (if you don't want to use keepdims=True is to reshape the array by adding a third axis something like this 9which essentially does the same thing as keepdims=True:

x_mean.reshape(x.shape[0], 1, x.shape[2])
Sign up to request clarification or add additional context in comments.

Comments

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.