Open In App

numpy.outer() function – Python

numpy.outer() function compute the outer product of two vectors.

Syntax : numpy.outer(a, b, out = None)



Parameters :
a : [array_like] First input vector. Input is flattened if not already 1-dimensional.
b : [array_like] Second input vector. Input is flattened if not already 1-dimensional.
out : [ndarray, optional] A location where the result is stored.

Return : [ndarray] Returns the outer product of two vectors. out[i, j] = a[i] * b[j]



Code #1 :




# Python program explaining
# numpy.outer() function
   
# importing numpy as geek 
import numpy as geek 
  
a = geek.ones(4)
b = geek.linspace(-1, 2, 4)
  
gfg = geek.outer(a, b)
  
print (gfg)

Output :

[[-1.  0.  1.  2.]
 [-1.  0.  1.  2.]
 [-1.  0.  1.  2.]
 [-1.  0.  1.  2.]]

 
Code #2 :




# Python program explaining
# numpy.outer() function
   
# importing numpy as geek 
import numpy as geek 
  
a = geek.ones(5)
b = geek.linspace(-2, 2, 5)
  
gfg = geek.outer(a, b)
  
print (gfg)

Output :

[[-2. -1.  0.  1.  2.]
 [-2. -1.  0.  1.  2.]
 [-2. -1.  0.  1.  2.]
 [-2. -1.  0.  1.  2.]
 [-2. -1.  0.  1.  2.]]
Article Tags :