NumPy Linear Algebra Practice Questions with Solutions

Introduction

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

11. Python Program to Perform Matrix Addition and Subtraction

Problem Statement

Write a Python program to create two 3×3 matrices and perform matrix addition and subtraction.

Python Solution

import numpy as np

matrix1 = np.array([
    [10, 20, 30],
    [40, 50, 60],
    [70, 80, 90]
])

matrix2 = np.array([
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
])

addition = matrix1 + matrix2
subtraction = matrix1 - matrix2

print("Matrix 1:")
print(matrix1)

print("\nMatrix 2:")
print(matrix2)

print("\nMatrix Addition:")
print(addition)

print("\nMatrix Subtraction:")
print(subtraction)

Sample Output

Matrix Addition:
[[11 22 33]
 [44 55 66]
 [77 88 99]]

Matrix Subtraction:
[[ 9 18 27]
 [36 45 54]
 [63 72 81]]

Explanation

NumPy performs element-wise matrix addition and subtraction when matrices have the same dimensions.

Concepts Covered

  • Matrix Addition
  • Matrix Subtraction
  • Element-wise Operations

12. Python Program to Perform Matrix Multiplication

Problem Statement

Write a Python program to multiply two matrices using the np.dot() function.

Python Solution

import numpy as np

matrix1 = np.array([
    [2, 3],
    [4, 5]
])

matrix2 = np.array([
    [6, 7],
    [8, 9]
])

result = np.dot(matrix1, matrix2)

print("Matrix 1:")
print(matrix1)

print("\nMatrix 2:")
print(matrix2)

print("\nMatrix Multiplication:")
print(result)

Sample Output

Matrix Multiplication:
[[36 41]
 [64 73]]

Explanation

np.dot() performs matrix multiplication by multiplying rows of the first matrix with columns of the second matrix.

Concepts Covered

  • np.dot()
  • Matrix Multiplication
  • Dot Product

13. Python Program to Calculate the Transpose of a Matrix

Problem Statement

Write a Python program to calculate the transpose of a matrix.

Python Solution

import numpy as np

matrix = np.array([
    [1, 2, 3],
    [4, 5, 6]
])

transpose = matrix.T

print("Original Matrix:")
print(matrix)

print("\nTranspose:")
print(transpose)

Sample Output

Original Matrix:
[[1 2 3]
 [4 5 6]]

Transpose:
[[1 4]
 [2 5]
 [3 6]]

Explanation

The transpose exchanges rows and columns of a matrix.

Concepts Covered

  • Matrix Transpose
  • .T
  • Linear Algebra Basics

14. Python Program to Calculate 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([
    [4, 7],
    [2, 6]
])

determinant = np.linalg.det(matrix)

print("Matrix:")
print(matrix)

print("\nDeterminant:")
print(determinant)

Sample Output

Matrix:
[[4 7]
 [2 6]]

Determinant:
10.0

Explanation

The determinant indicates whether a square matrix is invertible.

  • Determinant ≠ 0 → Invertible matrix
  • Determinant = 0 → Singular matrix

Concepts Covered

  • np.linalg.det()
  • Determinant
  • Square Matrix

15. Python Program to Calculate the Inverse of a Matrix

Problem Statement

Write a Python program to find the inverse of a square matrix.

Python Solution

import numpy as np

matrix = np.array([
    [4, 7],
    [2, 6]
])

inverse = np.linalg.inv(matrix)

print("Original Matrix:")
print(matrix)

print("\nInverse Matrix:")
print(inverse)

Sample Output

Original Matrix:
[[4 7]
 [2 6]]

Inverse Matrix:
[[ 0.6 -0.7]
 [-0.2  0.4]]

Explanation

The inverse matrix exists only if the determinant is non-zero.

Concepts Covered

  • np.linalg.inv()
  • Matrix Inverse
  • Invertible Matrix

16. Python Program to Solve a System of Linear Equations

Problem Statement

Solve the following system of equations using NumPy:

2x + y = 5
x - y = 1

Python Solution

import numpy as np

A = np.array([
    [2, 1],
    [1, -1]
])

B = np.array([5, 1])

solution = np.linalg.solve(A, B)

print("Coefficient Matrix:")
print(A)

print("\nConstant Matrix:")
print(B)

print("\nSolution:")
print("x =", solution[0])
print("y =", solution[1])

Sample Output

Solution:
x = 2.0
y = 1.0

Explanation

np.linalg.solve() efficiently solves systems of linear equations represented in matrix form.

Concepts Covered

  • np.linalg.solve()
  • Linear Equations
  • Matrix Representation

17. Python Program to Calculate Eigenvalues and Eigenvectors

Problem Statement

Write a Python program to calculate the eigenvalues and eigenvectors of a square matrix.

Python Solution

import numpy as np

matrix = np.array([
    [4, 2],
    [1, 3]
])

eigenvalues, eigenvectors = np.linalg.eig(matrix)

print("Matrix:")
print(matrix)

print("\nEigenvalues:")
print(eigenvalues)

print("\nEigenvectors:")
print(eigenvectors)

Sample Output

Eigenvalues:
[5. 2.]

Eigenvectors:
[[ 0.89442719 -0.70710678]
 [ 0.4472136   0.70710678]]

Explanation

  • Eigenvalues represent scaling factors.
  • Eigenvectors represent directions that remain unchanged after transformation.

Concepts Covered

  • np.linalg.eig()
  • Eigenvalues
  • Eigenvectors

18. Python Program to Calculate Matrix Rank and Trace

Problem Statement

Write a Python program to calculate the rank and trace of a square matrix.

Python Solution

import numpy as np

matrix = np.array([
    [2, 4, 1],
    [0, 5, 3],
    [1, 2, 6]
])

rank = np.linalg.matrix_rank(matrix)
trace = np.trace(matrix)

print("Matrix:")
print(matrix)

print("\nRank:", rank)
print("Trace:", trace)

Sample Output

Matrix:
[[2 4 1]
 [0 5 3]
 [1 2 6]]

Rank: 3
Trace: 13

Explanation

  • Rank is the number of linearly independent rows or columns.
  • Trace is the sum of the diagonal elements of a square matrix.

Concepts Covered

  • np.linalg.matrix_rank()
  • np.trace()
  • Matrix Rank
  • Matrix Trace

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 .T to 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.

Scroll to Top