๐ Introduction
Linear algebra is the backbone of:
- Data Science
- Machine Learning
- Artificial Intelligence
With NumPy, you can perform complex matrix operations easily.
๐ Why Linear Algebra Matters?
It is used in:
- Neural networks
- Image processing
- Scientific computing
๐งช Example Matrices
import numpy as npA = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])
โ๏ธ 1. Matrix Multiplication (dot())
print(np.dot(A, B))
๐ Output:
[[19 22]
[43 50]]
โ Multiplies rows ร columns
๐ 2. Using @ Operator
print(A @ B)
โ Shortcut for matrix multiplication
๐ 3. Transpose of Matrix
print(A.T)
๐ Output:
[[1 3]
[2 4]]
๐ 4. Matrix Inverse
print(np.linalg.inv(A))
๐ Output:
[[-2. 1. ]
[ 1.5 -0.5]]
โ Only works for square matrices
๐ 5. Determinant
print(np.linalg.det(A))
๐ Output:
-2.0
๐ข 6. Solving Linear Equations
b = np.array([5, 11])x = np.linalg.solve(A, b)
print(x)
โก Why Use NumPy for Linear Algebra?
Using NumPy:
- Fast computations
- Accurate results
- Optimized backend
๐ฆ Real-World Use Case
weights = np.array([0.2, 0.8])
features = np.array([50, 100])result = np.dot(weights, features)
print(result)
โ Used in:
- Machine learning models
- Data transformations
๐ Works with Other Libraries
Linear algebra is essential in:
- TensorFlow
- Scikit-learn
- Pandas
๐ Summary Table
| Operation | Function |
|---|---|
| Multiplication | dot(), @ |
| Transpose | .T |
| Inverse | np.linalg.inv() |
| Determinant | np.linalg.det() |
๐ง Pro Tips
- Use
@for cleaner code - Always check matrix shape
- Inverse only works for square matrices
๐ Conclusion
Linear algebra in NumPy is essential for advanced data science and machine learning applications.