Linear algebra helps us work with numbers in tables and shapes. It makes it easy to solve problems with many numbers at once.
0
0
Why linear algebra matters in NumPy
Introduction
When you want to find patterns in data like images or sounds.
When you need to solve many equations quickly, like in physics or engineering.
When you want to transform or rotate shapes in computer graphics.
When you analyze relationships between many variables in data science.
When you build machine learning models that learn from data.
Syntax
NumPy
import numpy as np # Create a matrix (2D array) A = np.array([[1, 2], [3, 4]]) # Multiply two matrices B = np.array([[5, 6], [7, 8]]) C = np.dot(A, B) # Find the inverse of a matrix A_inv = np.linalg.inv(A)
Use np.array to create vectors or matrices.
Use np.dot or @ for matrix multiplication.
Examples
This creates a simple list of numbers called a vector.
NumPy
import numpy as np # Vector (1D array) v = np.array([1, 2, 3])
This creates a table of numbers with 2 rows and 2 columns.
NumPy
import numpy as np # Matrix (2D array) M = np.array([[1, 2], [3, 4]])
This multiplies two matrices and prints the result.
NumPy
import numpy as np # Multiply matrices A = np.array([[1, 0], [0, 1]]) B = np.array([[4, 1], [2, 2]]) C = A @ B print(C)
Sample Program
This program shows how to multiply matrices, find an inverse, and check the identity matrix. It helps understand how linear algebra works with real numbers.
NumPy
import numpy as np # Create two matrices A = np.array([[2, 3], [1, 4]]) B = np.array([[5, 2], [3, 1]]) # Multiply matrices C = A @ B # Calculate the inverse of A A_inv = np.linalg.inv(A) # Multiply A by its inverse (should be identity matrix) I = A @ A_inv print("Matrix C (A multiplied by B):") print(C) print("\nInverse of matrix A:") print(A_inv) print("\nA multiplied by its inverse (Identity matrix):") print(I)
OutputSuccess
Important Notes
Matrix multiplication is not the same as multiplying numbers one by one.
Not all matrices have an inverse. Only square matrices with non-zero determinant do.
Linear algebra is the foundation for many data science tools and algorithms.
Summary
Linear algebra helps us work with many numbers at once using vectors and matrices.
It is useful for solving equations, transforming data, and building models.
Using numpy makes it easy to do linear algebra in Python.