Header Ads Widget

⚡ Premium Tools Hub • EXE Apps + Full Python Source Code
Lite • Pro • Bundle Packs • Instant Download

NumPy Linear Algebra Tutorial – Matrices, Determinants, Eigenvalues & More

NumPy Linear Algebra

Linear Algebra is one of the most important areas of mathematics in:

  • Data Science
  • Machine Learning
  • Artificial Intelligence
  • Computer Graphics
  • Engineering
  • Scientific Computing

NumPy provides a powerful module called:

numpy.linalg

This module contains functions for performing advanced linear algebra operations efficiently.


What is Linear Algebra?

Linear Algebra deals with:

  • Vectors
  • Matrices
  • Linear equations
  • Transformations
  • Eigenvalues and eigenvectors

Example Matrix:

A = [
[1, 2],
[3, 4]
]

Importing NumPy Linear Algebra Module

import numpy as np

Most operations are accessed through:

np.linalg

Creating Matrices

import numpy as np

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

print(A)

Output:

[[1 2]
[3 4]]

Matrix Addition

import numpy as np

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

B = np.array([
[5, 6],
[7, 8]
])

print(A + B)

Output:

[[ 6  8]
[10 12]]

Matrix Subtraction

import numpy as np

A = np.array([
[5, 6],
[7, 8]
])

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

print(A - B)

Output:

[[4 4]
[4 4]]

Matrix Multiplication

NumPy provides the @ operator.

import numpy as np

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

B = np.array([
[5, 6],
[7, 8]
])

print(A @ B)

Output:

[[19 22]
[43 50]]

Dot Product

import numpy as np

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

print(np.dot(A, B))

Output:

32

Matrix Transpose

Transpose swaps rows and columns.

import numpy as np

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

print(A.T)

Output:

[[1 3]
[2 4]]

Determinant of a Matrix

The determinant indicates whether a matrix is invertible.

A=[abcd],det(A)=adbcA=\begin{bmatrix}a&b\\c&d\end{bmatrix},\quad \det(A)=ad-bc

import numpy as np

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

print(np.linalg.det(A))

Output:

-2.0

Matrix Inverse

The inverse of a matrix is useful for solving equations.

import numpy as np

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

print(np.linalg.inv(A))

Output:

[[-2.   1. ]
[ 1.5 -0.5]]

Solving Linear Equations

Consider:

Ax=bAx=b

import numpy as np

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

b = np.array([8, 13])

x = np.linalg.solve(A, b)

print(x)

Output:

[2.2 3.6]

Eigenvalues and Eigenvectors

Eigenvalues are important in machine learning and PCA.

import numpy as np

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

values, vectors = np.linalg.eig(A)

print(values)
print(vectors)

Matrix Rank

Rank indicates the number of independent rows or columns.

import numpy as np

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

print(np.linalg.matrix_rank(A))

Output:

1

Matrix Trace

Trace is the sum of diagonal elements.

import numpy as np

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

print(np.trace(A))

Output:

5

Norm of a Vector

Vector norms measure magnitude.

import numpy as np

v = np.array([3, 4])

print(np.linalg.norm(v))

Output:

5.0

Singular Value Decomposition (SVD)

SVD is widely used in machine learning and image compression.

import numpy as np

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

U, S, Vt = np.linalg.svd(A)

print(U)
print(S)
print(Vt)

Real-World Example: Student Scores Matrix

import numpy as np

scores = np.array([
[80, 90],
[75, 85]
])

average = np.mean(scores, axis=0)

print(average)

Output:

[77.5 87.5]

Real-World Example: Sales Analysis

import numpy as np

sales = np.array([
[100, 200],
[150, 250]
])

totals = np.sum(sales, axis=0)

print(totals)

Output:

[250 450]

Common NumPy Linear Algebra Functions

FunctionPurpose
np.dot()           Dot product
np.matmul()           Matrix multiplication
np.linalg.det()           Determinant
np.linalg.inv()           Inverse matrix
np.linalg.solve()           Solve equations
np.linalg.eig()           Eigenvalues & eigenvectors
np.linalg.norm()           Vector norm
np.linalg.svd()           Singular Value Decomposition
np.linalg.matrix_rank()           Matrix rank

Advantages of NumPy Linear Algebra

  • Fast matrix operations
  • Optimized numerical computations
  • Essential for machine learning
  • Handles large datasets efficiently
  • Scientific computing ready

Summary

NumPy's Linear Algebra module provides powerful tools for working with matrices, vectors, determinants, inverses, eigenvalues, and solving linear systems. These operations form the foundation of modern data science and machine learning applications.

This functionality is part of NumPy and is extensively used with Python in scientific and analytical computing.


Conclusion

Linear Algebra is at the core of data science, AI, and engineering. By mastering NumPy's linear algebra functions, you can perform complex mathematical computations efficiently and build a strong foundation for advanced machine learning and scientific applications.




Post a Comment

0 Comments