Virtuoz
Virtuoz

Reputation: 914

Outer product of each column of a 2D array to form a 3D array - NumPy

Let X be a M x N matrix. Denote xi the i-th column of X. I want to create a 3 dimensional N x M x M array consisting of M x M matrices xi.dot(xi.T).

How can I do it most elegantly with numpy? Is it possible to do this using only matrix operations, without loops?

Upvotes: 12

Views: 3590

Answers (1)

Divakar
Divakar

Reputation: 221634

One approach with broadcasting -

X.T[:,:,None]*X.T[:,None]

Another with broadcasting and swapping axes afterwards -

(X[:,None,:]*X).swapaxes(0,2)

Another with broadcasting and a multi-dimensional transpose afterwards -

(X[:,None,:]*X).T

Another approach with np.einsum, which might be more intuitive thinking in terms of the iterators involved if you are translating from a loopy code -

np.einsum('ij,kj->jik',X,X)

Basic idea in all of these approaches is that we spread out the last axis for elementwise multiplication against each other keeping the first axis aligned. We achieve this process of putting against each other by extending X to two 3D array versions.

Upvotes: 17

Related Questions