Skip to content
Related Articles

Related Articles

Improve Article

numpy.outer() function – Python

  • Last Updated : 05 May, 2020

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.]]

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course




My Personal Notes arrow_drop_up
Recommended Articles
Page :