In this article, we will learn matrix-vector multiplication using NumPy.
Table Of Contents
What is a matrix in numpy and how to create it?
The numpy stands for numeric python, and it is used to work on the arrays. It is a module that can be imported directly. A matrix is a two-dimensional array that includes a row as one dimension and a column as another dimension.
We can create a matrix by using numpy.array() method.
Syntax:
numpy.array([[elements...], [elements...], .....])
Where elements refer to the values stored in the numpy array. Let’s create a matrix with two rows and three columns and display it.
import numpy # creating the first matrix with 3 rows and 3 columns first_matrix = numpy.array([[1,2,3], [2,5,1], [4,2,1]]) # Display the Matrix print(first_matrix)
Output:
Frequently Asked:
[[1 2 3] [2 5 1] [4 2 1]]
There are multiple ways to Perform matrix-vector multiplication. Lets discuss all the methods one by one with proper approach and a working code example
Perform matrix-vector multiplication using numpy with dot()
Numpy supports a dot() method, that returns a dot product. Which is equal to matrix-vector multiplication.
Syntax:
numpy.dot(first_matrix,second_matrix)
Parameters
- first_matrix is the first input numpy matrix
- second_matrix is the second input numpy matrix
Example 1:
In this example, we will create two matrices and apply dot() to perform matrix-vector multiplication.
import numpy # creating the first matrix with 3 rows and 3 columns first_matrix = numpy.array([[1,2,3], [2,5,1], [4,2,1]]) # creating the second matrix with 3 rows and 4 columns second_matrix = numpy.array([[1,2,2,1], [3,1,2,1], [0,0,1,2]]) # display both the matrices print(first_matrix) print('*******') print(second_matrix) print('*******') # Apply dot to perform matrix vector multiplication print("matrix vector multiplication:") print( numpy.dot(first_matrix,second_matrix) )
Output:
[[1 2 3] [2 5 1] [4 2 1]] ******* [[1 2 2 1] [3 1 2 1] [0 0 1 2]] ******* matrix vector multiplication: [[ 7 4 9 9] [17 9 15 9] [10 10 13 8]]
In the above source code, we created the first matrix with three rows and three columns. Then we created the second matrix with three rows and four columns. Finally, we applied the dot() method on these two matrices to perform matrix-vector multiplication.
Example 2:
In this example, we will create two matrices and apply dot() to perform matrix-vector multiplication.
import numpy # creating the first matrix with 5 rows and 3 columns first_matrix = numpy.array([[1, 2, 3], [2, 5, 1], [4, 2, 1], [2, 5, 1], [4, 2, 1]]) # creating the second matrix with 3 rows and 2 columns second_matrix = numpy.array([[1, 2], [3, 1], [0, 0]]) # display both the matrices print(first_matrix) print('*******') print(second_matrix) print('*******') # Apply dot to perform matrix vector multiplication print("matrix vector multiplication:") print( numpy.dot(first_matrix,second_matrix) )
Output:
[[1 2 3] [2 5 1] [4 2 1] [2 5 1] [4 2 1]] ******* [[1 2] [3 1] [0 0]] ******* matrix vector multiplication: [[ 7 4] [17 9] [10 10] [17 9] [10 10]]
In the above source code, we created the first matrix with five rows and three columns. Then we created the second matrix with three rows and two columns. Finally, we applied the dot() method on these two matrices to perform matrix-vector multiplication.
Perform matrix-vector multiplication using numpy with matmul() method.
The numpy supports matmul() function that will return the resultant multiplied matrix. This is similar to the functionality of dot() method.
Syntax:
numpy.matmul(first_matrix,second_matrix)
Parameters
- first_matrix is the first input numpy matrix
- second_matrix is the second input numpy matrix
Example 1:
In this example, we will create two matrices and apply matmul() to perform matrix-vector multiplication.
import numpy # Creating the first matrix with 3 rows and 3 columns first_matrix = numpy.array([[1, 2, 3], [2, 5, 1], [4, 2, 1]]) # Creating the second matrix with 3 rows and 4 columns second_matrix = numpy.array([[1, 2, 2, 1], [3, 1, 2, 1], [0, 0, 1, 2]]) # Display both the matrices print(first_matrix) print('********') print(second_matrix) print('********') # Apply matmul to perform matrix vector multiplication print("matrix vector multiplication:") print(numpy.matmul(first_matrix,second_matrix))
Output:
[[1 2 3] [2 5 1] [4 2 1]] ******** [[1 2 2 1] [3 1 2 1] [0 0 1 2]] ******** matrix vector multiplication: [[ 7 4 9 9] [17 9 15 9] [10 10 13 8]]
In the above source code, we created the first matrix with three rows and three columns. Then we created the second matrix with three rows and four columns. Finally, we applied the matmul() method on these two matrices to perform matrix-vector multiplication.
Example 2:
In this example, we will create two matrices and apply matmul() to perform matrix-vector multiplication.
import numpy # Creating the first matrix with 5 rows and 3 columns first_matrix = numpy.array([[1, 2, 3], [2, 5, 1], [4, 2, 1], [2, 5, 1], [4, 2, 1]]) # Creating the second matrix with 3 rows and 2 columns second_matrix = numpy.array([[1, 2], [3, 1], [0, 0]]) # Display both the matrices print(first_matrix) print('*********') print(second_matrix) print('*********') # Apply matmul to perform matrix vector multiplication matrix = numpy.matmul(first_matrix,second_matrix) print("matrix vector multiplication:") print(matrix)
Output:
[[1 2 3] [2 5 1] [4 2 1] [2 5 1] [4 2 1]] ********* [[1 2] [3 1] [0 0]] ********* matrix vector multiplication: [[ 7 4] [17 9] [10 10] [17 9] [10 10]]
In the above source code, we created the first matrix with five rows and three columns. Then created the second matrix with three rows and two columns. Finally, we applied the matmul() method to these two matrices to perform matrix-vector multiplication.
Perform matrix-vector multiplication using @ operator.
Here, we are not using numpy module to perform matrix-vector multiplication, we simply use the @ operator, which will perform the same functionality as dot() and matmul() methods.
Syntax:
first_matrix@second_matrix
where,
- first_matrix is the first input numpy matrix
- second_matrix is the second input numpy matrix
Example:
In this example, we will create two matrices and apply @ operator to perform matrix-vector multiplication.
import numpy # Creating the first matrix with 5 rows and 3 columns first_matrix = numpy.array([[1, 2, 3], [2, 5, 1], [4, 2, 1], [2, 5, 1], [4, 2, 1]]) # Creating the second matrix with 3 rows and 2 columns second_matrix = numpy.array([[1, 2], [3, 1], [0, 0]]) # Display both the matrices print(first_matrix) print('********') print(second_matrix) print('********') # Apply @ to perform matrix vector multiplication matrix = first_matrix @ second_matrix print("matrix vector multiplication:") print(matrix)
Output:
[[1 2 3] [2 5 1] [4 2 1] [2 5 1] [4 2 1]] ******** [[1 2] [3 1] [0 0]] ******** matrix vector multiplication: [[ 7 4] [17 9] [10 10] [17 9] [10 10]]
In the above source code, we created the first matrix with five rows and three columns. Then we created the second matrix with three rows and two columns. Finally, we applied the “@” operator method on these two matrices to perform matrix-vector multiplication.
Summary
Great! you did it. We discussed matrix vector multiplication using the dot() and matmul() methods. We can perform matrix-vector multiplication on two numpy matrices. These two methods are available in numpy module. Happy Learning.