Linear Algebra with Python – Tutorial for Beginners

Linear Algebra with Python - Tutorial for Beginners
Linear Algebra with Python – Tutorial for Beginners

Linear Algebra with Python is an essential tool for scientific computing, data analysis, and machine learning. It helps in solving complex mathematical problems with ease and efficiency. Python, being an open-source language, has many libraries that provide support for Linear Algebra operations. One such library is NumPy, which is extensively used for mathematical operations.

Linear Algebra Operation with NumPy library

NumPy is a Python library that provides support for numerical operations. It offers an array object that is fast and efficient, making it easy to perform mathematical operations. NumPy offers several linear algebra functions that include solving linear equations, matrix factorization, eigenvalue problems, and much more. Let’s take a look at some basic operations.

  • Creating Arrays

Arrays are the most important data structures in NumPy. You can create arrays using the numpy.array() function. For example:

import numpy as np

# Creating an array of zeros
zeros_array = np.zeros((2,2))
print(zeros_array)

# Creating an array of ones
ones_array = np.ones((2,2))
print(ones_array)

# Creating a random array
random_array = np.random.rand(2,2)
print(random_array)
[[0. 0.]
 [0. 0.]]
[[1. 1.]
 [1. 1.]]
[[0.69357044 0.92424373]
 [0.66226801 0.9836632 ]]
  • Basic Operations

You can perform basic operations like addition, subtraction, multiplication, and division on NumPy arrays. For example:

# Addition
a = np.array([[1,2], [3,4]])
b = np.array([[5,6], [7,8]])
c = a + b
print(c)

# Subtraction
d = b - a
print(d)

# Multiplication
e = a * b
print(e)

# Division
f = b / a
print(f)
[[ 6  8]
 [10 12]]
[[4 4]
 [4 4]]
[[ 5 12]
 [21 32]]
[[5.         3.        ]
 [2.33333333 2.        ]]
  • Matrix Multiplication

Matrix multiplication is a crucial operation in linear algebra. In NumPy, you can perform matrix multiplication using the np.dot() function. For example:

# Matrix Multiplication
a = np.array([[1,2], [3,4]])
b = np.array([[5,6], [7,8]])
c = np.dot(a,b)
print(c)
[[19 22]
 [43 50]]

Special Matrices

NumPy provides support for special matrices such as identity matrix, diagonal matrix, triangular matrix, and much more. Let’s take a look at some of them.

  • Identity Matrix

The identity matrix is a square matrix with diagonal elements as 1 and all other elements as 0. You can create an identity matrix using the np.eye() function. For example:

# Identity Matrix
identity_matrix = np.eye(3)
print(identity_matrix)
[[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]
  • Diagonal Matrix

The diagonal matrix is a matrix with non-zero elements only on the diagonal. You can create a diagonal matrix using the np.diag() function. For example:

# Diagonal Matrix
diagonal_matrix = np.diag([1, 2, 3])
print(diagonal_matrix)
[[1 0 0]
 [0 2 0]
 [0 0 3]]
  • Triangular Matrix

A triangular matrix is a matrix where all the elements above or below the diagonal are zero. You can create a triangular matrix using the np.tri() function. For example:

# Lower Triangular Matrix
lower_triangular_matrix = np.tri(3, 3, -1)
print(lower_triangular_matrix)

# Upper Triangular Matrix
upper_triangular_matrix = np.tri(3, 3, 0)
print(upper_triangular_matrix)
[[0. 0. 0.]
 [1. 0. 0.]
 [1. 1. 0.]]
[[1. 0. 0.]
 [1. 1. 0.]
 [1. 1. 1.]]

Now let’s take a look at some examples of linear algebra operations using NumPy.

  • solve() function

You can solve linear equations using NumPy’s linalg.solve() function. For example:

a = np.array([[3, 1], [1, 2]])
b = np.array([9, 8])
x = np.linalg.solve(a, b)
print(x)
[2. 3.]
  • Let’s say we have a system of linear equations:

3x + 2y = 5 4x – y = 2
We can represent this system of equations in matrix form as Ax = b, where:
A = [[3, 2], [4, -1]]
x = [[x], [y]]
b = [[5], [2]]
We can solve this system of equations using NumPy as follows:

import numpy as np

# Define the matrices
A = np.array([[3, 2], [4, -1]])
b = np.array([[5], [2]])

# Solve the system of equations
x = np.linalg.solve(A, b)

# Print the solution
print(x)
[[1.]
 [1.]]
  • Matrix Factorization

Matrix factorization is a method used to decompose a matrix into simpler matrices. One such factorization method is the singular value decomposition (SVD). You can perform SVD using NumPy’s linalg.svd() function. For example:

import numpy as np

# Define a matrix
A = np.array([[1, 2], [3, 4]])

# Perform SVD
U, S, V = np.linalg.svd(A)

# Print the matrices
print("U: ", U)
print("S: ", S)
print("V: ", V)
U:  [[-0.40455358 -0.9145143 ]
 [-0.9145143   0.40455358]]
S:  [5.4649857  0.36596619]
V:  [[-0.57604844 -0.81741556]
 [-0.81741556  0.57604844]]

Leave a Reply

Prev
Analyze Data from the National Parks about Endangered Species
Analyze Data from the National Parks about Endangered Species

Analyze Data from the National Parks about Endangered Species

This goal of this project is to analyze biodiversity data from the National

Next
Create a Python Trivia Quiz Game with Python
Create a Python Trivia Quiz Game with Python

Create a Python Trivia Quiz Game with Python

Let’s create a Python trivia quiz game with Python

You May Also Like