
Python
NumPy enables efficient mathematical operations on arrays. Essential for data science and scientific computing.
NumPy arrays are faster and more efficient than Python lists for numerical operations.
import numpy as np
import time
# Compare list vs NumPy array
py_list = list(range(1000000))
np_array = np.arange(1000000)
# List operation
start = time.time()
result = [x * 2 for x in py_list]
print(f"List: {time.time() - start:.6f}s")
# NumPy operation
start = time.time()
result = np_array * 2
print(f"NumPy: {time.time() - start:.6f}s") # Much faster!import numpy as np
# From list
arr1 = np.array([1, 2, 3, 4, 5])
# Ranges
arr2 = np.arange(0, 10, 2) # Start, stop, step
arr3 = np.linspace(0, 1, 5) # 5 evenly spaced values
# Zeros and ones
zeros = np.zeros(5) # [0, 0, 0, 0, 0]
ones = np.ones(5) # [1, 1, 1, 1, 1]
# Random arrays
random = np.random.rand(5) # Random 0-1
random_int = np.random.randint(1, 100, 5)
# 2D arrays
matrix = np.array([[1, 2, 3], [4, 5, 6]])
print(matrix.shape) # (2, 3)import numpy as np
arr = np.array([1, 2, 3, 4, 5])
# Element-wise operations
print(arr + 1) # [2, 3, 4, 5, 6]
print(arr * 2) # [2, 4, 6, 8, 10]
print(arr ** 2) # [1, 4, 9, 16, 25]
print(np.sqrt(arr)) # [1, 1.41, 1.73, 2, 2.24]
# Statistical operations
print(arr.mean()) # Average: 3.0
print(arr.std()) # Standard deviation
print(arr.sum()) # Total: 15
print(arr.min()) # Minimum: 1
print(arr.max()) # Maximum: 5
# Logical operations
print(arr > 3) # [False, False, False, True, True]
print(arr[arr > 3]) # [4, 5] - filterimport numpy as np
arr = np.array([10, 20, 30, 40, 50])
# Indexing
print(arr[0]) # 10
print(arr[-1]) # 50
# Slicing
print(arr[1:3]) # [20, 30]
print(arr[::2]) # [10, 30, 50] - every 2nd element
print(arr[::-1]) # [50, 40, 30, 20, 10] - reverse
# 2D array indexing
matrix = np.array([[1, 2, 3], [4, 5, 6]])
print(matrix[0, 1]) # 2 (row 0, col 1)
print(matrix[1, :]) # [4, 5, 6] (row 1, all columns)NumPy automatically handles operations on arrays of different shapes.
import numpy as np
# 1D array + scalar
arr = np.array([1, 2, 3])
result = arr + 10 # [11, 12, 13]
# Different shapes (broadcasting)
arr1 = np.array([[1, 2, 3]]) # Shape (1, 3)
arr2 = np.array([[10], [20]]) # Shape (2, 1)
result = arr1 + arr2 # Shape (2, 3)
# [[11, 12, 13],
# [21, 22, 23]]import numpy as np
# Temperature readings (3 days, 4 locations)
temps = np.array([
[20, 22, 21, 23], # Day 1
[21, 23, 22, 24], # Day 2
[19, 21, 20, 22] # Day 3
])
# Average temperature per day
daily_avg = temps.mean(axis=1)
print(f"Daily average: {daily_avg}")
# Average temperature per location
location_avg = temps.mean(axis=0)
print(f"Location average: {location_avg}")
# Overall average
overall_avg = temps.mean()
print(f"Overall average: {overall_avg:.2f}")
# Hottest reading
print(f"Hottest: {temps.max()}")
# Coldest reading
print(f"Coldest: {temps.min()}")
# Days above 22C
warm_days = (temps > 22).sum(axis=1)
print(f"Readings >22C per day: {warm_days}")import numpy as np
arr = np.arange(12)
print(arr.shape) # (12,)
# Reshape to 2D
matrix = arr.reshape(3, 4)
print(matrix.shape) # (3, 4)
# Reshape to 3D
cube = arr.reshape(2, 2, 3)
print(cube.shape) # (2, 2, 3)
# Flatten back to 1D
flat = matrix.flatten()
print(flat.shape) # (12,)
# Transpose
transposed = matrix.T
print(transposed.shape) # (4, 3)import numpy as np
# Matrix multiplication
A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])
# Matrix product
C = np.dot(A, B)
# Vector operations
v1 = np.array([1, 2, 3])
v2 = np.array([4, 5, 6])
# Dot product
dot = np.dot(v1, v2) # 1*4 + 2*5 + 3*6 = 32
# Cross product
cross = np.cross(v1, v2)| Function | Purpose | Example |
|---|---|---|
| `arange()` | Create range | `np.arange(0, 10, 2)` |
| `linspace()` | Evenly spaced | `np.linspace(0, 1, 5)` |
| `reshape()` | Change shape | `arr.reshape(3, 4)` |
| `sum()`, `mean()` | Aggregate | `arr.sum()` |
| `dot()` | Matrix product | `np.dot(A, B)` |
| `transpose()` | Flip axes | `arr.T` |
Continue: Data Visualization Basics
Resources
Ojasa Mirai
Master AI-powered development skills through structured learning, real projects, and verified credentials. Whether you're upskilling your team or launching your career, we deliver the skills companies actually need.
Learn Deep • Build Real • Verify Skills • Launch Forward