How to Get Element-Wise Matrix Multiplication (Hadamard Product) in Numpy

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

For elementwise multiplication of matrix objects, you can use 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 array instead of matrix. matrix objects have all sorts of horrible incompatibilities with regular ndarrays. With ndarrays, you can just use * for elementwise multiplication:

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:

a @ b  # matrix multiplication

element-wise matrix multiplication (Hadamard product) using numpy

If I understand correctly, this might work:

import numpy as np

a = np.array([[1,1],[1,0]])
b = np.array([[3,4],[5,4]])

x = np.array([[a,b],[b,a]])
y = np.array([[a,a],[b,b]])

result = np.array([_x @ _y for _x, _y in zip(x,y)])

element wise multiplication of a vector and a matrix with numpy

Probably the simplest is to do the following.

import numpy as np
a = np.arange(6).reshape(3, 2) # a = [[0, 1], [2, 3], [4, 5]]; a.shape = (3, 2)
b = np.arange(3) + 1

ans = np.diag(b)@a

Here's a method that exploits numpy multiplication broadcasting:

ans = (b*a.T).T

These two solutions basically take the same approach

ans = np.tile(b,(2,1)).T*a
ans = np.vstack([b for _ in range(a.shape[1])]).T*a

Elementwise multiplication of NumPy arrays of matrices

Operators are "element-wise" by default for numpy arrays. Just use the @ operator (matrix multiplication) instead of *:

In [24]: A = np.arange(9).reshape(3,3)

In [25]: X = np.array([A[:], A[:]*2, A[:]*3])

In [26]: Y = X[:]

In [27]: X @ Y
Out[27]:
array([[[ 15, 18, 21],
[ 42, 54, 66],
[ 69, 90, 111]],

[[ 60, 72, 84],
[168, 216, 264],
[276, 360, 444]],

[[135, 162, 189],
[378, 486, 594],
[621, 810, 999]]])

In [28]: X[0] @ Y[0]
Out[28]:
array([[ 15, 18, 21],
[ 42, 54, 66],
[ 69, 90, 111]])

In [29]: X[1] @ Y[1]
Out[29]:
array([[ 60, 72, 84],
[168, 216, 264],
[276, 360, 444]])

In [30]: X[2] @ Y[2]
Out[30]:
array([[135, 162, 189],
[378, 486, 594],
[621, 810, 999]])

HTH.

Element-wise numpy matrix multiplication

You can do the following:

new_array = np.einsum('ijk,jlk->ilk', A, B)

Element-wise matrix multiplication in NumPy

Numpy arrays use element-wise multiplication by default. Check out numpy.einsum and numpy.tensordot. I think what you're looking for is something like this:

results = np.einsum('ij,jkl->ikl',factor,input)

How to do element wise matrix multiply using numpy

You can try np.matmul(b.T, np.dot(a,b)):

import numpy as np
import pandas as pd

a = np.random.sample((4, 3, 3))
b = np.random.sample((3, 3))
c = np.zeros_like(a)

# using for loop
for i0, ai in enumerate(a):
c[i0] = np.dot(b.T, np.dot(ai, b))

# alternative method
e = np.zeros_like(a)
e = np.matmul(b.T, np.dot(a,b))

# checking for equal
print(np.array_equal(c, e))

Numpy - Multiply each element of a matrix with the element of another matrix at the same position

Just multiply them. numpy supports matrix operations.

x = np.arange(1, 10).reshape(3, 3)
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
print(x*x)

All elements will be multiplied by the respective number.

array([[ 1,  4,  9],
[16, 25, 36],
[49, 64, 81]])

Generalized Matrix Multiplication without Numpy

# 1 x 3 Matrix
A = [ [5, -5, 10]]

# 3 x 2 Matrix
B = [
[-10, 13],
[57, -37],
[-96, 15]
]

def mult_matx(A, B):

rowsA = len(A)
colsB = len(B[0])
result = [[0] * colsB for i in range(rowsA)]

for i in range(rowsA):

# iterating by column by B
for j in range(colsB):


# iterating by rows of B
for k in range(len(B)):

result[i][j] += A[i][k] * B[k][j]

for r in result:
print(r)


Related Topics



Leave a reply



Submit