[Tutor] Python 3.6 Multiply the elements of a 2D Array by the elements of a 1D Aeeay

giacomo.boffi at gmail.com giacomo.boffi at gmail.com
Sun Apr 16 17:08:43 EDT 2017


giacomo.boffi at gmail.com writes:

I throw two errors in my answer, see the inline corrections

> Possibility 1
>
> c = np.outer(D, s) # c has shape (i*j, k) as outer flattens its arguments 
> c = sum(c, 1)      # c has shape (i*j) because se summed over 2nd index

  c = np.sum(c, 1)   # c has shape (i*j) because se summed over 2nd index
  
> c.reshape(D.shape) # c has the shape of D, i.e., (i, j)
>
> Possibility 2
>
> # https://docs.scipy.org/doc/numpy/reference/generated/numpy.einsum.html
> stackoverflow.com/questions/26089893/understanding-numpys-einsum

  # http://stackoverflow.com/questions/26089893/understanding-numpys-einsum

> c = np.einsum('ij,k -> ij', D, s)

in particular `c = sum(...)` instead of `c = np.sum(...)` is a nasty
error because `sum` is a builtin and the error message one gets could be
misleading



More information about the Tutor mailing list