0

I am getting the error:

ValueError: operands could not be broadcast together with shapes (3,4) (3,3)

z= np.arange(12).reshape(3,4)
m= np.arange(9).reshape(3,3)
z * m

error:

ValueError: operands could not be broadcast together with shapes (3,4) (3,3)

abc
  • 33
  • 1
  • 1
  • 9
  • which is the line that raise the error? it seems like you are trying to add one to the other, but they have different shape – Matteo Peluso Jul 03 '19 at 12:26
  • when i try to multiply these two then i got error @MatteoPeluso – abc Jul 03 '19 at 12:26
  • 2
    Which result do you expect to get when multiplying arrays of different dimesions? – Yossi Jul 03 '19 at 12:33
  • The * operator provides elementwise multiplication, which requires that the arrays are the same shape, or are 'broadcastable'. Use `np.dot(z, m)` in your case – Shadowfax Jul 03 '19 at 12:37
  • Why do you think they should multioly? Do you know the basics of numpy broadcasting? – hpaulj Jul 03 '19 at 12:42

2 Answers2

4
import numpy as np
z= np.arange(12).reshape(3,4)
m= np.arange(9).reshape(3,3)

print(np.dot(m,z))
## [[ 20  23  26  29]
## [ 56  68  80  92]
## [ 92 113 134 155]]

print(np.dot(z.T,m))
##[[ 60  72  84]
##[ 69  84  99]
##[ 78  96 114]
##[ 87 108 129]]
Prudhvi
  • 1,095
  • 1
  • 7
  • 18
0

to be able to multiply those matrices they should share the same dimension along the multiplication:

Z[3x4] * M[3x3] is not possible to evaluate because you have four columns on the Z matrix

to be able to multiply it you should or build differently your z matrix or translate it

in this example I translate the dimension of the matrix before doing the multiplication: Z[4x3] * M[3x3] = ZM[4x3]:

np.dot(z.T, m)

Matteo Peluso
  • 452
  • 1
  • 6
  • 23