How to Perform Vector Hadamard Multiplication in Python

How to Perform Vector Hadamard Multiplication in Python

Hadamard multiplication, also known as element-wise multiplication, involves multiplying two vectors of the same dimension, element by element.

Let’s begin by examining two column vectors:

We perform Hadamard multiplication as follows: 

To demonstrate this operation using Python, we initiate by creating two vectors of equal dimensions:

import numpy as np

# Vector 1
vector1 = np.array([[1], [2], [3]])

# Vector 2
vector2 = np.array([[4], [5], [6]])

# Print the shapes of Vector 1 and Vector 2
print("Shape of Vector 1:", vector1.shape)
print("Shape of Vector 2:", vector2.shape)

Expected output confirming their dimensions:

Shape of Vector 1: (3, 1)
Shape of Vector 2: (3, 1)

Now, let’s perform Hadamard multiplication:

# Hadamard multiplication of Vectors 1 and 2
result = vector1 * vector2

Viewing the results:

# Output result and shape
print("Result of Hadamard Multiplication:")
print(result)
print("Shape of Result:", result.shape)

Output of the operation:

Result of Hadamard Multiplication:
[[ 4]
 [10]
 [18]]

Shape of Result: (3, 1)

The resulting operation of Hadamard Multiplication will always have the same shape or dimension as the original vectors. 

What if we attempt this operation with vectors of mismatched sizes? Here’s an example to illustrate:

import numpy as np

# Vector 1
vector1 = np.array([[1], [2], [3]])

# Vector 3 with fewer elements
vector3 = np.array([[8], [9]])

try:
    # Attempt Hadamard multiplication
    result = vector1 * vector3
    print("Result of Hadamard Multiplication:")
    print(result)
except ValueError as e:
    print("Error during Hadamard multiplication:", str(e))

This attempt will generate an error message:

Error during Hadamard multiplication: 
operands could not be broadcast together with shapes (3,1) (2,1) 

This error, “operands could not be broadcast together,” underscores the need for precise dimension alignment in mathematical operations, particularly in fields like machine learning and data science.

Understanding these constraints is essential for successful applications, from signal processing to neural networks, where element-wise operations are integral to algorithm functionality.

Leave a Reply

Your email address will not be published. Required fields are marked *