本文介绍了如何在numpy中三向外部积?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于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.

  1. 但是如何实现3数组外部乘积,这意味着:第三个向量c = [c0, c1, ..., cP],如何获得外积在3个numpy数组之间.

  1. 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中三向外部积?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 01:44