📌 Dynamic Introduction to NumPy
*NumPy (Numerical Python)* is a fundamental package for numerical computing in Python. It provides *high-performance arrays* and *mathematical functions* that make numerical computations efficient. Let's dive into its core features dynamically, with explanations and code examples.
---
🌟 *Why Use NumPy?*
✅ *Faster Computation* than Python lists (because of optimized C implementation).
✅ *Memory Efficient* (uses fixed-type arrays).
✅ *Supports Vectorized Operations* (no need for loops).
✅ *Rich Mathematical Functions* (linear algebra, statistics, etc.).
✅ *Seamless Integration* with other libraries (Pandas, SciPy, TensorFlow).
---
🔰 *Installation*
If you don’t have NumPy installed, use:
```bash
pip install numpy
```
Then, import it:
```python
import numpy as np
```
---
🔢 *Creating NumPy Arrays*
NumPy uses `ndarray` (N-dimensional array) as its primary data structure.
*1D Array*
```python
arr = np.array([1, 2, 3, 4, 5])
print(arr) # Output: [1 2 3 4 5]
```
*2D Array (Matrix)*
```python
matrix = np.array([[1, 2, 3], [4, 5, 6]])
print(matrix)
```
🔹 `shape` tells the dimensions:
```python
print(matrix.shape) # Output: (2,3)
```
---
🚀 *Essential Array Operations*
*Element-wise Operations*
```python
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
print(a + b) # Output: [5 7 9]
print(a * b) # Output: [ 4 10 18]
```
*Broadcasting (Different Shapes)*
```python
A = np.array([[1, 2, 3], [4, 5, 6]])
B = np.array([1, 2, 3])
print(A + B)
```
✅ NumPy *automatically expands* smaller arrays to match larger ones!
---
🎯 *Commonly Used Functions*
| *Function* | *Description* | *Example* |
|--------------------|-----------------------------------|-------------------------|
| `np.zeros((2,3))` | Creates a 2×3 matrix of zeros | `[[0. 0. 0.], [0. 0. 0.]]` |
| `np.ones((3,3))` | Creates a 3×3 matrix of ones | `[[1. 1. 1.], [1. 1. 1.], [1. 1. 1.]]` |
| `np.eye(3)` | Creates a 3×3 identity matrix | `[[1. 0. 0.], [0. 1. 0.], [0. 0. 1.]]` |
| `np.linspace(1,10,5)` | Generates 5 values from 1 to 10 | `[1. 3.25 5.5 7.75 10.]` |
| `np.random.rand(3,3)` | Generates a 3×3 matrix of random values | Example: `[[0.12 0.88 0.45], [0.67 0.32 0.91], [0.44 0.56 0.78]]` |
---
🏎️ *Advanced Features*
*Matrix Multiplication*
```python
A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])
print(np.dot(A, B)) # Matrix multiplication
```
*Statistical Functions*
```python
data = np.array([10, 20, 30, 40])
print(np.mean(data)) # Mean
print(np.std(data)) # Standard deviation
print(np.max(data)) # Max value
```
*Reshaping & Transposing*
```python
arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr.T) # Transpose
print(arr.reshape(3,2)) # Change shape
```
---
⚡ *Speed Comparison: NumPy vs. Lists*
Let's compare *NumPy arrays* vs *Python lists* in performance:
```python
import time
size = 10**6
list1 = list(range(size))
list2 = list(range(size))
np_arr1 = np.array(list1)
np_arr2 = np.array(list2)
Python list addition
start = time.time()
result = [x + y for x, y in zip(list1, list2)]
print("Python List Time:", time.time() - start)
NumPy array addition
start = time.time()
result = np_arr1 + np_arr2
print("NumPy Time:", time.time() - start)
```
✅ NumPy is *10-100x faster* than Python lists!
---
🔥 *Conclusion*
*NumPy is powerful* for handling numerical data efficiently.
*Supports high-speed operations* on large datasets.
**Integrates well with machine learning & data science libraries**.
🚀 *Next Steps?*
Learn NumPy’s integration with *Pandas, Matplotlib, and SciPy* for real-world applications!
---
Would you like a NumPy *cheat sheet* or specific **hands-on exercises**? 🚀