Embark on a journey of knowledge! Take the quiz and earn valuable credits.
Take A QuizChallenge yourself and boost your learning! Start the quiz now to earn credits.
Take A QuizUnlock your potential! Begin the quiz, answer questions, and accumulate credits along the way.
Take A Quiz
🔹 1. Introduction
Linear algebra forms the core of many modern
applications — from machine learning and computer graphics to engineering
simulations and data transformations. NumPy provides an efficient
and intuitive set of tools to perform linear algebra operations using the
numpy.linalg module.
In this chapter, we’ll dive into:
These techniques are essential for data scientists,
analysts, engineers, and developers working on predictive models or
simulations.
🔹 2. Matrix
Representation in NumPy
import
numpy as np
A
= np.array([[1, 2], [3, 4]])
B
= np.array([[5, 6], [7, 8]])
Here, both A and B are 2×2 matrices.
🔹 3. Matrix
Multiplication & Dot Product
✅ Matrix Multiplication
Use @ or np.dot() for matrix multiplication:
result
= A @ B
#
or
result
= np.dot(A, B)
Output:
[[19
22]
[43 50]]
✅ Element-wise Multiplication
(not matrix multiplication)
A
* B
#
[[ 5 12]
# [21 32]]
🔹 4. Transpose of a
Matrix
Flip rows to columns:
A.T
#
[[1 3]
# [2 4]]
🔹 5. Inverse of a Matrix
To find the inverse of a square matrix A, use:
inv_A
= np.linalg.inv(A)
Only works if the matrix is non-singular (det ≠ 0).
🔹 6. Identity Matrix
I
= np.eye(3)
Output:
lua
Copy
[[1.
0. 0.]
[0. 1. 0.]
[0. 0. 1.]]
Useful
for checking if:
A @ np.linalg.inv(A) ≈ I
🔹 7. Determinant of a
Matrix
np.linalg.det(A)
# Output: -2.0 (means A is invertible)
🔹 8. Rank of a Matrix
Use:
np.linalg.matrix_rank(A)
🔹 9. Solving Systems of
Equations
2x
+ 3y = 8
4x
+ 9y = 20
✅ Represent as AX = B
A
= np.array([[2, 3], [4, 9]])
B
= np.array([8, 20])
X
= np.linalg.solve(A, B)
print(X) # Solution: [2. 1.]
🔹 10. Eigenvalues and
Eigenvectors
Eigen decomposition helps in PCA, dynamics,
and dimensionality reduction.
vals,
vecs = np.linalg.eig(A)
print("Eigenvalues:",
vals)
print("Eigenvectors:\n",
vecs)
🔹 11. Norms and Condition
Numbers
✅ Norm (magnitude of vector)
v
= np.array([3, 4])
np.linalg.norm(v) # Output: 5.0
✅ Condition Number (matrix
stability)
np.linalg.cond(A)
A high condition number means the matrix is close to
singular → numerical instability.
🔹 12. Pseudoinverse
Used when the matrix isn’t square or isn’t invertible.
np.linalg.pinv(A)
🔹 13. Summary Table
Operation |
Function/Method |
Description |
Matrix
multiplication |
np.dot() or @ |
Matrix
product |
Transpose |
.T |
Swap rows and
columns |
Inverse |
np.linalg.inv() |
Matrix
inverse |
Determinant |
np.linalg.det() |
Scalar value
from square matrix |
Rank |
np.linalg.matrix_rank() |
Dimension of
the column space |
Solve
equations |
np.linalg.solve() |
Solve AX = B |
Eigenvalues/vectors |
np.linalg.eig() |
For PCA,
dynamics |
Norm |
np.linalg.norm() |
Vector
magnitude |
Pseudoinverse |
np.linalg.pinv() |
Generalized
inverse for non-square A |
🔹 14. Real-World Example:
PCA (Simplified)
X
= np.array([[2.5, 2.4],
[0.5, 0.7],
[2.2, 2.9]])
#
Step 1: Mean Center
X_centered
= X - np.mean(X, axis=0)
#
Step 2: Covariance matrix
cov
= np.cov(X_centered.T)
#
Step 3: Eigen decomposition
vals,
vecs = np.linalg.eig(cov)
Principal
components = Eigenvectors
Importance = Eigenvalues
NumPy is used for numerical computations, array operations, linear algebra, and data processing in Python.
NumPy arrays are faster, use less memory, and support vectorized operations, unlike Python lists which are slower and less flexible for numerical tasks
It’s the core data structure in NumPy — an N-dimensional array that allows element-wise operations and advanced indexing.
No, it needs to be installed separately using pip install numpy.
Broadcasting allows NumPy to perform operations on arrays of different shapes by automatically expanding them to be compatible.
Yes, it provides comprehensive support for matrix multiplication, eigenvalues, singular value decomposition, and more.
While NumPy is essential for preprocessing and fast array computations, deep learning libraries like TensorFlow or PyTorch build on top of it for more advanced tasks.
✅ Absolutely — Pandas is built on NumPy arrays, and Matplotlib supports NumPy for plotting.
✅ Yes — the numpy.random module offers distributions like normal, binomial, uniform, etc.
✅ Significantly. NumPy’s vectorized operations are typically 10x to 100x faster than traditional for-loops in Python.
Please log in to access this content. You will be redirected to the login page shortly.
LoginReady to take your education and career to the next level? Register today and join our growing community of learners and professionals.
Comments(0)