gpt4 book ai didi

python - 在 scipy 中使用 tracedot 执行一系列外积

转载 作者:太空狗 更新时间:2023-10-30 02:35:01 24 4
gpt4 key购买 nike

要在 Python (scipy/numpy) 中执行两个向量之间的外积,您可以使用外函数,或者您可以像这样简单地使用点:

In [76]: dot(rand(2,1), rand(1,2))
Out[76]:
array([[ 0.43427387, 0.5700558 ],
[ 0.19121408, 0.2509999 ]])

现在的问题是,假设我有一个向量列表(或两个列表...)并且我想计算所有外积,创建一个方矩阵列表。我如何轻松做到这一点?我相信 tensordot 能够做到这一点,但如何做到这一点?

最佳答案

计算外积的第三种(也是最容易概括的)方法是通过广播。

一些 3 向量(行上的向量):

import numpy as np
x = np.random.randn(100, 3)
y = np.random.randn(100, 3)

外积:

from numpy import newaxis
xy = x[:,:,newaxis] * y[:,newaxis,:]

# 10th matrix
print xy[10]
print np.outer(x[10,:], y[10,:])

关于python - 在 scipy 中使用 tracedot 执行一系列外积,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5122172/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com