NumPy provides the numpy.linalg module for performing linear algebra operations. These functions are widely used in machine learning, artificial intelligence, data science, computer graphics, robotics, and scientific computing. In this chapter, you’ll practice beginner-friendly NumPy Linear Algebra questions with complete solutions. NumPy Linear Algebra practice questions with solutions help to understand the concepts
1. Python Program to Add Two Matrices
Problem Statement
Write a Python program to add two matrices using NumPy.
Python Solution
import numpy as np
matrix1 = np.array([[1, 2],
[3, 4]])
matrix2 = np.array([[5, 6],
[7, 8]])
result = matrix1 + matrix2
print(result)
Sample Output
[[ 6 8]
[10 12]]
Explanation
The + operator performs element-wise addition between two matrices of the same shape.
Concepts Covered
- Matrix Addition
- Element-wise Operations
2. Python Program to Subtract Two Matrices
Problem Statement
Write a Python program to subtract one matrix from another.
Python Solution
import numpy as np
matrix1 = np.array([[8, 6],
[4, 2]])
matrix2 = np.array([[1, 2],
[3, 4]])
result = matrix1 - matrix2
print(result)
Sample Output
[[ 7 4]
[ 1 -2]]
Explanation
The - operator subtracts corresponding elements of two matrices.
Concepts Covered
- Matrix Subtraction
3. Python Program to Multiply Two Matrices
Problem Statement
Write a Python program to perform matrix multiplication.
Python Solution
import numpy as np
matrix1 = np.array([[1, 2],
[3, 4]])
matrix2 = np.array([[5, 6],
[7, 8]])
result = np.matmul(matrix1, matrix2)
print(result)
Sample Output
[[19 22]
[43 50]]
Explanation
np.matmul() performs matrix multiplication following linear algebra rules.
Concepts Covered
np.matmul()- Matrix Multiplication
4. Python Program to Find the Transpose of a Matrix
Problem Statement
Write a Python program to find the transpose of a matrix.
Python Solution
import numpy as np
matrix = np.array([[1, 2, 3],
[4, 5, 6]])
print(matrix.T)
Sample Output
[[1 4]
[2 5]
[3 6]]
Explanation
The .T attribute swaps rows and columns.
Concepts Covered
- Matrix Transpose
.T
5. Python Program to Find the Determinant of a Matrix
Problem Statement
Write a Python program to calculate the determinant of a square matrix.
Python Solution
import numpy as np
matrix = np.array([[2, 3],
[1, 4]])
print(np.linalg.det(matrix))
Sample Output
5.0
Explanation
np.linalg.det() returns the determinant of a square matrix.
Concepts Covered
np.linalg.det()- Determinant
6. Python Program to Find the Inverse of a Matrix
Problem Statement
Write a Python program to calculate the inverse of a matrix.
Python Solution
import numpy as np
matrix = np.array([[4, 7],
[2, 6]])
print(np.linalg.inv(matrix))
Sample Output
[[ 0.6 -0.7]
[-0.2 0.4]]
Explanation
np.linalg.inv() calculates the inverse of a non-singular matrix.
Concepts Covered
np.linalg.inv()- Matrix Inverse
7. Python Program to Find Eigenvalues
Problem Statement
Write a Python program to calculate the eigenvalues of a matrix.
Python Solution
import numpy as np
matrix = np.array([[4, 2],
[1, 3]])
eigenvalues, eigenvectors = np.linalg.eig(matrix)
print(eigenvalues)
Sample Output
[5. 2.]
Explanation
np.linalg.eig() returns both eigenvalues and eigenvectors.
Concepts Covered
- Eigenvalues
np.linalg.eig()
8. Python Program to Find Eigenvectors
Problem Statement
Write a Python program to display the eigenvectors of a matrix.
Python Solution
import numpy as np
matrix = np.array([[4, 2],
[1, 3]])
eigenvalues, eigenvectors = np.linalg.eig(matrix)
print(eigenvectors)
Sample Output
[[ 0.8944 -0.7071]
[ 0.4472 0.7071]]
Explanation
Eigenvectors indicate the principal directions associated with the eigenvalues.
Concepts Covered
- Eigenvectors
- Linear Algebra
9. Python Program to Calculate Matrix Rank
Problem Statement
Write a Python program to calculate the rank of a matrix.
Python Solution
import numpy as np
matrix = np.array([[1, 2],
[2, 4]])
print(np.linalg.matrix_rank(matrix))
Sample Output
1
Explanation
np.linalg.matrix_rank() returns the rank of the matrix.
Concepts Covered
- Matrix Rank
10. Python Program to Solve a System of Linear Equations
Problem Statement
Write a Python program to solve linear equations using NumPy.
Python Solution
import numpy as np
A = np.array([[2, 1],
[1, 3]])
B = np.array([8, 13])
solution = np.linalg.solve(A, B)
print(solution)
Sample Output
[2.2 3.6]
Explanation
np.linalg.solve() solves systems of linear equations of the form AX = B.
Concepts Covered
np.linalg.solve()- Linear Equations
Chapter Summary
In this chapter, you learned how to perform common linear algebra operations using NumPy. You practiced matrix addition, subtraction, multiplication, transpose, determinant, inverse, eigenvalues, eigenvectors, matrix rank, and solving systems of linear equations. These concepts are fundamental in machine learning, artificial intelligence, computer vision, robotics, and scientific computing.
Key Takeaways
- Use
+and-for matrix addition and subtraction. - Use
np.matmul()for matrix multiplication. - Use
.Tto transpose a matrix. - Use
np.linalg.det()to calculate the determinant. - Use
np.linalg.inv()to find the inverse of a matrix. - Use
np.linalg.eig()to calculate eigenvalues and eigenvectors. - Use
np.linalg.matrix_rank()to determine matrix rank. - Use
np.linalg.solve()to solve systems of linear equations.
Frequently Asked Questions (FAQs)
1. What is linear algebra in NumPy?
Linear algebra in NumPy involves mathematical operations on matrices and vectors using the numpy.linalg module.
2. Which function performs matrix multiplication?
Use np.matmul() or the @ operator for matrix multiplication.
3. How do I calculate the determinant of a matrix?
Use np.linalg.det().
4. Which function finds the inverse of a matrix?
Use np.linalg.inv().
5. What are eigenvalues and eigenvectors?
Eigenvalues represent scaling factors, while eigenvectors represent directions associated with a matrix transformation.
6. How do I solve linear equations in NumPy?
Use np.linalg.solve(A, B) where A is the coefficient matrix and B is the constant vector.
7. Why is NumPy Linear Algebra important?
It is essential for machine learning, deep learning, computer graphics, robotics, scientific computing, engineering, and data science.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

