What is an outer product of vectors?
Let’s say we are given two vectors a and b. The vector a is represented using an mx1 matrix and the vector b is represented using an nx1 matrix.
At this point, if we take an outer product of a and b, we will get an mxn matrix M.
How to perform an outer product of vectors using Python NumPy?
We can use the following Python code to perform an outer product of two vectors using NumPy.
import numpy m = 3 n = 4 a = numpy.random.randint(low=1, high=10, size=(m, 1)) b = numpy.random.randint(low=1, high=10, size=(n, 1)) print("a: \n", a) print("b: \n", b) M = numpy.outer(a, b) print("M: \n", M)
Here, we are first creating two column vectors a and b. The vectors a and b contain random integers. We are using the numpy.random.randint() function to generate random integers. The generated random integers will be within the range [low, high).
The size aregument specifies the size of the vector. For example, size=(m, 1) specifies that the vector will have m elements in a single column. In other words, the vector will be a column vector that can be specified using an mx1 matrix.
Now, we are using the numpy.outer() function to compute the outer product of a and b. The output of the mentioned program will be:
a: [[7] [6] [5]] b: [[9] [9] [9] [3]] M: [[63 63 63 21] [54 54 54 18] [45 45 45 15]]






0 Comments