-1

i am trying to divide each x-value by its row mean

train_rows_mean = train_data.mean(axis=1) #calculate the mean row_wise

#divide each value by row mean
train_data/train_rows_mean #broadcasting issue

print(train_data.shape) #shape of train data
print(train_rows_mean.shape) #shape of mean

but i get this error

ValueError: operands could not be broadcast together with shapes (540,2500) (540,) 
sahuno
  • 321
  • 1
  • 8

2 Answers2

2

Assuming train_data is a 2-dimensional array, try:

train_data/train_rows_mean[:, None]

This adds a new dimension to train_rows_mean before dividing

PlainRavioli
  • 1,127
  • 1
  • 1
  • 10
0

As @Mechanic Pig mentioned in his comment, you need to to preserve the dimension so that numpy can broadcast along it. For more information, look at the docs here.

Also, if you need to take mean row-wise, you need to use axis=0

a = np.array([[1,2],[3,4]])
a_mean = a.mean(axis=0, keepdims=True) #has dimensions 1x2
out = a/a_mean
nevermore
  • 1
  • 5