【问题标题】:Outer product using numpy/scipy使用 numpy/scipy 的外积
【发布时间】:2015-12-04 16:03:11
【问题描述】:

我正在使用 Python 2.7、NumPy 1.6.2 和 SciPy 0.16.0 来计算以下内容。

我创建了一个 Hadamard 矩阵。我想从中获取一个向量并用它自己计算它的外积。这是我的代码。

from scipy import linalg
import numpy
from numpy import linalg as np

def test():
    hadamard_matrix = linalg.hadamard(8)
    outer_product_0 = numpy.multiply(hadamard_matrix[0], hadamard_matrix[0].transpose())
    outer_product_1 = numpy.multiply(hadamard_matrix[0].transpose(), hadamard_matrix[0])

    print str(outer_product_0)
    print str(outer_product_1)

输出:

Python 2.7 (r27:82525, Jul  4 2010, 09:01:59) [MSC v.1500 32 bit (Intel)] on win32
>>> import scipytest
>>> scipytest.test()
[1 1 1 1 1 1 1 1]
[1 1 1 1 1 1 1 1]

您可以看到,我得到的是一个向量,而不是一个 2X2 矩阵。我做错了吗?

【问题讨论】:

    标签: python-2.7 numpy matrix scipy linear-algebra


    【解决方案1】:

    在 numpy 中有一个 outer-product,它的工作原理与 specified 完全相同。

    因此,大小为n 的向量与自身外积将产生nxn 矩阵。

    a = [1, 2, 3]
    np.outer(a, a)
    

    会给你

    array([[1, 2, 3],
           [2, 4, 6],
           [3, 6, 9]])
    

    【讨论】:

      猜你喜欢
      • 2017-05-25
      • 1970-01-01
      • 2015-03-21
      • 2014-03-17
      • 1970-01-01
      • 2012-11-29
      • 2023-03-10
      • 2019-03-06
      相关资源
      最近更新 更多