In this article, we will learn matrixvector 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 twodimensional 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:
[[1 2 3] [2 5 1] [4 2 1]]
There are multiple ways to Perform matrixvector multiplication. Lets discuss all the methods one by one with proper approach and a working code example
Perform matrixvector multiplication using numpy with dot()
Numpy supports a dot() method, that returns a dot product. Which is equal to matrixvector 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 matrixvector 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 matrixvector multiplication.
Example 2:
In this example, we will create two matrices and apply dot() to perform matrixvector 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 matrixvector multiplication.
Perform matrixvector 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 matrixvector 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 matrixvector multiplication.
Example 2:
In this example, we will create two matrices and apply matmul() to perform matrixvector 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 matrixvector multiplication.
Perform matrixvector multiplication using @ operator.
Here, we are not using numpy module to perform matrixvector multiplication, we simply use the @ operator, which will perform the same functionality as dot() and matmul() methods.
Syntax:
[email protected]_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 matrixvector 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 matrixvector multiplication.
Summary
Great! you did it. We discussed matrix vector multiplication using the dot() and matmul() methods. We can perform matrixvector multiplication on two numpy matrices. These two methods are available in numpy module. Happy Learning.
Pandas Tutorials Learn Data Analysis with Python

Pandas Tutorial Part #1  Introduction to Data Analysis with Python

Pandas Tutorial Part #2  Basics of Pandas Series

Pandas Tutorial Part #3  Get & Set Series values

Pandas Tutorial Part #4  Attributes & methods of Pandas Series

Pandas Tutorial Part #5  Add or Remove Pandas Series elements

Pandas Tutorial Part #6  Introduction to DataFrame

Pandas Tutorial Part #7  DataFrame.loc[]  Select Rows / Columns by Indexing

Pandas Tutorial Part #8  DataFrame.iloc[]  Select Rows / Columns by Label Names

Pandas Tutorial Part #9  Filter DataFrame Rows

Pandas Tutorial Part #10  Add/Remove DataFrame Rows & Columns

Pandas Tutorial Part #11  DataFrame attributes & methods

Pandas Tutorial Part #12  Handling Missing Data or NaN values

Pandas Tutorial Part #13  Iterate over Rows & Columns of DataFrame

Pandas Tutorial Part #14  Sorting DataFrame by Rows or Columns

Pandas Tutorial Part #15  Merging or Concatenating DataFrames

Pandas Tutorial Part #16  DataFrame GroupBy explained with examples
Are you looking to make a career in Data Science with Python?
Data Science is the future, and the future is here now. Data Scientists are now the most soughtafter professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.
Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.
Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.