numpy outer product

Learning Python and SAS | 01 October 2018

One of the advantage of Python over base SAS (of course SAS has other advantages over Python) is matrix operations. This short post is about a few ways to do product and outer product using Numpy.

products

np.multiply and * accomplish the same thing. Both multiply element-wise.

np.dot as the name implies, gives a dot product. The result is a single number.

reshape

An useful reshape trick is -1, which is a placeholder that refers to the size of the remaining axis that is to be inferred.

For example, we begin with a 1-d array of length 6, and want to reshape it to (2,3), 2 rows and 3 columns. By (2,-1), we mean: 2 rows and 6/2 columns. -1 is a placeholder for the inferred 3.

>>> a = np.array([1,2,3,4,5,6])
>>> a1 = a.reshape(2,-1)
>>> print(a1)
Out:
[[1,2,3]
[4,5,6]]

Outer product

Outer product is also called “cartesian product”. It is every order combination of the two arrays, and multiplied. The number of elements in the end result is nXm, if the left has n and the right has m elements, respectively.

In all methods below, my favorite is reshape because there is nothing extra to remember other than the use of -1.

codeouter product.py
In [1]: x = np.array([1,2,3])

In [2]: y = np.array([2,4])

In [3]: np.outer(x,y)
Out[3]:
array([[ 2,  4],
       [ 4,  8],
       [ 6, 12]])

In [4]: x[:,np.newaxis]*y[np.newaxis,:]
Out[4]:
array([[ 2,  4],
       [ 4,  8],
       [ 6, 12]])

In [5]: x.reshape((-1,1))*y.reshape((1,-1))
Out[5]:
array([[ 2,  4],
       [ 4,  8],
       [ 6, 12]])

In [6]: x.reshape((-1,1))*y.reshape((-1,1)).T
Out[6]:
array([[ 2,  4],
       [ 4,  8],
       [ 6, 12]])

In [7]: np.multiply(x.ravel()[:,np.newaxis],y.ravel()[np.newaxis,:])
Out[7]:
array([[ 2,  4],
       [ 4,  8],
       [ 6, 12]])