numpy.outer()函数Python
numpy.outer()
函数计算两个向量的外积。
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]
代码#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)
输出 :
[[-1. 0. 1. 2.]
[-1. 0. 1. 2.]
[-1. 0. 1. 2.]
[-1. 0. 1. 2.]]
代码#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)
输出 :
[[-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.]]