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]]
22[[19],[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产品)?
4 个解决方案
#1
48
Try this,
试试这个,
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]])
#2
24
just do this:
只是这样做:
import numpy as np
a = np.array([[1,2],[3,4]])
b = np.array([[5,6],[7,8]])
a * b
#3
5
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.multiply and * would yield element wise multiplication known as the Hadamard Product
np。乘法和*会产生元素智慧乘法,称为哈达玛乘积
%timeit
is ipython magic
%时间是ipython魔法
#4
0
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 ndarray
and multiplication of two ndarray
would result element wise multiplication. So the result would be:
在这里,np.array(a)返回一个二维数组ndarray,并且两个ndarray的乘法将会得到元素wise乘法。结果是:
result = [[5, 12], [21, 32]]
If you wanna get a matrix, the do it with this:
如果你想要一个矩阵,用这个来做:
result = np.mat(result)
#1
48
Try this,
试试这个,
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]])
#2
24
just do this:
只是这样做:
import numpy as np
a = np.array([[1,2],[3,4]])
b = np.array([[5,6],[7,8]])
a * b
#3
5
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.multiply and * would yield element wise multiplication known as the Hadamard Product
np。乘法和*会产生元素智慧乘法,称为哈达玛乘积
%timeit
is ipython magic
%时间是ipython魔法
#4
0
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 ndarray
and multiplication of two ndarray
would result element wise multiplication. So the result would be:
在这里,np.array(a)返回一个二维数组ndarray,并且两个ndarray的乘法将会得到元素wise乘法。结果是:
result = [[5, 12], [21, 32]]
If you wanna get a matrix, the do it with this:
如果你想要一个矩阵,用这个来做:
result = np.mat(result)