How to do elementwise multiplication of two vectors using NumPy

In Python, a vector is represented as a one-dimensional array, which usually has numerical values. It can be defined as a row or column vector. In Python, the NumPy library simplifies vector manipulation by providing a wide range of mathematical operations/functions that can be applied to vectors.

Element-wise multiplication (Hadamard product) of two vectors involves multiplying each element of the first vector by the corresponding element of the second vector to generate a new vector. For element-wise multiplication, the size of both vectors must be the same.

Elementwise vector multiplication
Elementwise vector multiplication

The multiply() function

The NumPy multiply() function takes two required parameters, which can be two values, arrays, matrices, or vectors. It performs element-wise multiplication and returns the resultant vector, which depends on the input type.

numpy.multiply(x, y, /, out=None, *, where=True, casting='same_kind', dtype=None)
Syntax of the multiply() function for vectors in the NumPy library

The multiply() function parameters are as follows:

  • x, y: These can be the two values or vectors to be multiplied.

  • /: This indicates that the parameters that follow must be specified using positional arguments. In the above syntax x and y must be specified positionally.

  • out: This is the location where the output is stored.

  • *: This indicates that the parameters after it (*) must be specified using keyword arguments.

  • where: At locations where the condition is True, the output vector will be set to the multiplication result. Elsewhere, the output vector will retain its original value.

Note: If an uninitialized output vector is created via the default out=None, locations within it where the condition is false will remain uninitialized.

  • casting: This optional parameter specifies the type casting (converting a value from one data type to another) behavior.

  • dtype: This optional parameter specifies the returned vector data type.

Example

Below is an example that shows how we can create vectors and perform element-wise multiplication using NumPy:

import numpy as np
# Define two vectors
vector_1 = np.array([1, 2, 3])
vector_2 = np.array([7, 8, 9])
# Elementwise multiplication
resultant_vector = np.multiply(vector_1, vector_2)
# Print the result
print(resultant_vector)

Explanation for the code above:

  • Line 1: We import the NumPy library.

  • Lines 4–5: We create/define two vectors using the array() function available in NumPy.

  • Line 8: We perform element wise multiplication using the built-in multiply() function in the NumPy library.

  • Line 11: We print the resultant of the vector multiplication.

Note: The array() function in NumPy is used to create NumPy arrays and vectors as well because they’re fundamental data structures in the NumPy library and are more efficient for numerical operations than Python lists. They’re optimized for numerical operations which makes them faster than regular Python lists for mathematical operations.

There is another way of performing element-wise multiplication that uses the * sign between two vectors. The code example is given below:

import numpy as np
# Define two vectors
vector_1 = np.array([[1], [2], [3]])
vector_2 = np.array([[7], [8], [9]])
# Elementwise multiplication
resultant_vector = vector_1 * vector_2
# Print the result
print(resultant_vector)

Explanation for the code above:

  • Line 1: We import the NumPy library.

  • Lines 4-5: We create/define two vectors using the array() function available in Numpy.

  • Line 8: We perform element wise multiplication using the * operator.

  • Line 11: We print the resultant vector of multiplication.

Conclusion

In this Answer, we went through vectors and their multiplication in Python. Vector multiplication has numerous applications, such as in machine learning, where we update weights and do forward propagation on feature vectors. It’s also used in graphics, where we perform translation and scaling etc.

Copyright ©2024 Educative, Inc. All rights reserved