外部产品使用numpy/scipy [英] Outer product using numpy/scipy

查看:102
本文介绍了外部产品使用numpy/scipy的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using Python 2.7, NumPy 1.6.2 and SciPy 0.16.0 to calculate the following.

我创建了一个Hadamard矩阵.我想从中获取一个向量,然后计算出它的外部乘积.这是我的代码.

I have created a Hadamard matrix. I would like to take a vector from it and compute its outer product with itself. Here is my code.

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矩阵.我在做错什么吗?

You can see that instead of getting a 2X2 matrix I am getting a vector. Am I doing something wrong?

推荐答案

有一个外部产品,它完全按照指定.

因此,大小为n的向量本身是外部乘积,将生成nxn矩阵.

So vector of size n which is outer-producted with itself would result in nxn matrix.

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

会给你

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

这篇关于外部产品使用numpy/scipy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆