Python 如何在numpy中获得逐元素矩阵乘法(Hadamard积)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40034993/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 23:06:12  来源:igfitidea点击:

How to get element-wise matrix multiplication (Hadamard product) in numpy?

pythonnumpymatrixmatrix-multiplicationelementwise-operations

提问by Malintha

I have two matrices

我有两个矩阵

a = np.matrix([[1,2], [3,4]])
b = np.matrix([[5,6], [7,8]])

and I want to get the element-wise product, [[1*5,2*6], [3*7,4*8]], equaling

我想得到元素明智的产品[[1*5,2*6], [3*7,4*8]],等于

[[5,12], [21,32]]

[[5,12], [21,32]]

I have tried

我试过了

print(np.dot(a,b)) 

and

print(a*b)

but both give the result

但两者都给出了结果

[[19 22], [43 50]]

[[19 22], [43 50]]

which is the matrix product, not the element-wise product. How can I get the the element-wise product (aka Hadamard product) using built-in functions?

这是矩阵乘积,而不是逐元素乘积。如何使用内置函数获得元素级产品(又名 Hadamard 产品)?

回答by Rahul K P

For elementwise multiplication of matrixobjects, you can use numpy.multiply:

对于matrix对象的元素乘法,您可以使用numpy.multiply

import numpy as np
a = np.array([[1,2],[3,4]])
b = np.array([[5,6],[7,8]])
np.multiply(a,b)

Result

结果

array([[ 5, 12],
       [21, 32]])

However, you should really use arrayinstead of matrix. matrixobjects have all sorts of horrible incompatibilities with regular ndarrays. With ndarrays, you can just use *for elementwise multiplication:

但是,您确实应该使用array代替matrix. matrix对象与常规 ndarray 有各种可怕的不兼容。使用 ndarrays,您可以只*用于元素乘法:

a * b

If you're on Python 3.5+, you don't even lose the ability to perform matrix multiplication with an operator, because @does matrix multiplication now:

如果您使用的是 Python 3.5+,您甚至不会失去使用运算符执行矩阵乘法的能力,因为@现在矩阵乘法是

a @ b  # matrix multiplication

回答by jtitusj

just do this:

只需这样做:

import numpy as np

a = np.array([[1,2],[3,4]])
b = np.array([[5,6],[7,8]])

a * b

回答by 4rshdeep

import numpy as np
x = np.array([[1,2,3], [4,5,6]])
y = np.array([[-1, 2, 0], [-2, 5, 1]])

x*y
Out: 
array([[-1,  4,  0],
       [-8, 25,  6]])

%timeit x*y
1000000 loops, best of 3: 421 ns per loop

np.multiply(x,y)
Out: 
array([[-1,  4,  0],
       [-8, 25,  6]])

%timeit np.multiply(x, y)
1000000 loops, best of 3: 457 ns per loop

Both np.multiplyand *would yield element wise multiplication known as the Hadamard Product

双方np.multiply*会产生被称为阿达玛产品元素方式乘法

%timeitis ipython magic

%timeit是 ipython 的魔法吗

回答by Amir Rezazadeh

Try this:

尝试这个:

a = np.matrix([[1,2], [3,4]])
b = np.matrix([[5,6], [7,8]])

#This would result a 'numpy.ndarray'
result = np.array(a) * np.array(b)

Here, np.array(a)returns a 2D array of type ndarrayand multiplication of two ndarraywould result element wise multiplication. So the result would be:

在这里,np.array(a)返回一个 2D 类型的数组ndarray,两个的乘法ndarray将导致元素乘法。所以结果将是:

result = [[5, 12], [21, 32]]

If you wanna get a matrix, the do it with this:

如果你想得到一个矩阵,用这个做:

result = np.mat(result)