问题描述
关于numpy.outer [link] .
About the numpy.outer [link] .
给定两个向量a = [a0, a1, ..., aM]
和b = [b0, b1, ..., bN]
,外部乘积将为M * N矩阵.
Given two vectors, a = [a0, a1, ..., aM]
and b = [b0, b1, ..., bN]
, the outer product will be M*N matrix.
-
但是如何实现3数组外部乘积,这意味着:第三个向量
c = [c0, c1, ..., cP]
,如何获得外积在3个numpy数组之间.
But how to implement a 3-array outer product, which means : giventhird vector
c = [c0, c1, ..., cP]
, how to get the outer productbetween the 3 numpy arrays.
以及如何为numpy中的n数组获取n路外部乘积,对于einsum
的方法,如何将'i,j,k->ijk'
更改为处理"n"
.
and how to get n-way outer product for n-array in numpy, for the method of einsum
, how to change 'i,j,k->ijk'
to process "n"
.
推荐答案
充分利用广播的直接方法是:
The direct way of doing this, taking full advantage of broadcasting is:
a[:,None,None] * b[None,:,None] * c[None,None,:]
np.ix_
为您进行了重塑,但速度不高
np.ix_
does this reshaping for you, at a modest cost in speed
In [919]: np.ix_(a,b,c)
Out[919]:
(array([[[0]],
[[1]],
[[2]],
[[3]],
[[4]]]), array([[[10],
[11],
[12],
[13]]]), array([[[20, 21, 22]]]))
,结果数组可以乘以
np.prod(np.ix_(a,b,c))
einsum
版本简单,快速
np.einsum('i,j,k',a,b,c)
学习所有三种方法是一个好主意.
It's a good idea to learn all 3 methods.
嵌套outer
的问题是期望输入为1d或将其展平.可以使用,但需要一些重塑
The problem with nesting outer
is that expects the inputs to be 1d, or it flattens them. It can be used, but needs some reshaping
np.outer(a,np.outer(b,c)).reshape(a.shape[0],b.shape[0],c.shape[0])
这篇关于如何在numpy中三向外部积?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!