Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 959
Virtuoz Avatar asked Jan 04 '17 17:01

Virtuoz


Video Answer


1 Answers

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.

like image 105
Divakar Avatar answered Oct 01 '22 02:10

Divakar