原文:https://docs.scipy.org/doc/numpy/reference/generated/numpy.ufunc.outer.html
校对:(虚位以待)
ufunc.
outer
(A, B)将ufunc op应用于所有对(a,b),其中a在A和b在B。
让M = A.ndim
,N B.ndim
。然后,op.outer(A, B)
的结果C是维度M + N,使得:
对于A和B一维,这等价于:
r = empty(len(A),len(B))
for i in range(len(A)):
for j in range(len(B)):
r[i,j] = op(A[i], B[j]) # op = ufunc in question
参数: | A:array_like
B:array_like
|
---|---|
返回: | r:ndarray
|
也可以看看
例子
>>> np.multiply.outer([1, 2, 3], [4, 5, 6])
array([[ 4, 5, 6],
[ 8, 10, 12],
[12, 15, 18]])
多维例子:
>>> A = np.array([[1, 2, 3], [4, 5, 6]])
>>> A.shape
(2, 3)
>>> B = np.array([[1, 2, 3, 4]])
>>> B.shape
(1, 4)
>>> C = np.multiply.outer(A, B)
>>> C.shape; C
(2, 3, 1, 4)
array([[[[ 1, 2, 3, 4]],
[[ 2, 4, 6, 8]],
[[ 3, 6, 9, 12]]],
[[[ 4, 8, 12, 16]],
[[ 5, 10, 15, 20]],
[[ 6, 12, 18, 24]]]])