π 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.