Python 8 min read

NumPy Tutorial: Complete Guide to Numerical Computing in Python

Master NumPy for scientific computing. Learn arrays, mathematical operations, broadcasting, and data manipulation with practical examples.

MR

Moshiour Rahman

Advertisement

What is NumPy?

NumPy (Numerical Python) is the fundamental package for scientific computing in Python. It provides support for large, multi-dimensional arrays and matrices, along with mathematical functions to operate on them efficiently.

Why NumPy?

Python ListsNumPy Arrays
Slow operationsFast vectorized operations
Any data typeHomogeneous data
Limited mathExtensive math functions
More memoryMemory efficient

Installation

pip install numpy
import numpy as np

Creating Arrays

From Python Lists

# 1D array
arr = np.array([1, 2, 3, 4, 5])
print(arr)  # [1 2 3 4 5]

# 2D array
arr_2d = np.array([[1, 2, 3], [4, 5, 6]])
print(arr_2d)
# [[1 2 3]
#  [4 5 6]]

# 3D array
arr_3d = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])

Built-in Functions

# Zeros
zeros = np.zeros((3, 4))  # 3x4 array of zeros

# Ones
ones = np.ones((2, 3))  # 2x3 array of ones

# Full (custom value)
full = np.full((2, 2), 7)  # 2x2 array filled with 7

# Identity matrix
identity = np.eye(3)  # 3x3 identity matrix

# Range
arr = np.arange(0, 10, 2)  # [0, 2, 4, 6, 8]

# Linspace (evenly spaced)
arr = np.linspace(0, 1, 5)  # [0. 0.25 0.5 0.75 1.]

# Random arrays
random_arr = np.random.rand(3, 3)  # 3x3 uniform [0, 1)
random_int = np.random.randint(0, 10, (3, 3))  # 3x3 random ints
random_normal = np.random.randn(3, 3)  # Standard normal distribution

Array Properties

arr = np.array([[1, 2, 3], [4, 5, 6]])

print(arr.shape)      # (2, 3) - dimensions
print(arr.ndim)       # 2 - number of dimensions
print(arr.size)       # 6 - total elements
print(arr.dtype)      # int64 - data type
print(arr.itemsize)   # 8 - bytes per element
print(arr.nbytes)     # 48 - total bytes

Indexing and Slicing

1D Arrays

arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])

print(arr[0])       # 1 - first element
print(arr[-1])      # 10 - last element
print(arr[2:5])     # [3 4 5] - slice
print(arr[::2])     # [1 3 5 7 9] - every second element
print(arr[::-1])    # [10 9 8 7 6 5 4 3 2 1] - reversed

2D Arrays

arr = np.array([[1, 2, 3, 4],
                [5, 6, 7, 8],
                [9, 10, 11, 12]])

print(arr[0, 0])      # 1 - element at row 0, col 0
print(arr[1, 2])      # 7 - element at row 1, col 2
print(arr[0])         # [1 2 3 4] - first row
print(arr[:, 0])      # [1 5 9] - first column
print(arr[0:2, 1:3])  # [[2 3] [6 7]] - subarray
print(arr[:, -1])     # [4 8 12] - last column

Boolean Indexing

arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])

# Boolean mask
mask = arr > 5
print(mask)         # [False False False False False True True True True True]
print(arr[mask])    # [6 7 8 9 10]

# Direct filtering
print(arr[arr > 5])           # [6 7 8 9 10]
print(arr[(arr > 3) & (arr < 8)])  # [4 5 6 7]
print(arr[arr % 2 == 0])      # [2 4 6 8 10] - even numbers

Fancy Indexing

arr = np.array([10, 20, 30, 40, 50])

# Index with array
indices = np.array([0, 2, 4])
print(arr[indices])  # [10 30 50]

# 2D fancy indexing
arr_2d = np.array([[1, 2], [3, 4], [5, 6]])
rows = np.array([0, 2])
cols = np.array([1, 0])
print(arr_2d[rows, cols])  # [2 5]

Array Operations

Arithmetic Operations

a = np.array([1, 2, 3, 4])
b = np.array([5, 6, 7, 8])

print(a + b)      # [6 8 10 12] - addition
print(a - b)      # [-4 -4 -4 -4] - subtraction
print(a * b)      # [5 12 21 32] - multiplication
print(a / b)      # [0.2 0.33 0.43 0.5] - division
print(a ** 2)     # [1 4 9 16] - power
print(np.sqrt(a)) # [1. 1.41 1.73 2.] - square root

Mathematical Functions

arr = np.array([0, np.pi/2, np.pi])

# Trigonometric
print(np.sin(arr))    # [0. 1. 0.]
print(np.cos(arr))    # [1. 0. -1.]
print(np.tan(arr))

# Exponential and logarithmic
arr = np.array([1, 2, 3])
print(np.exp(arr))    # [2.72 7.39 20.09]
print(np.log(arr))    # [0. 0.69 1.10] - natural log
print(np.log10(arr))  # [0. 0.30 0.48] - log base 10

# Rounding
arr = np.array([1.4, 2.5, 3.7])
print(np.floor(arr))  # [1. 2. 3.]
print(np.ceil(arr))   # [2. 3. 4.]
print(np.round(arr))  # [1. 2. 4.]

Aggregate Functions

arr = np.array([[1, 2, 3], [4, 5, 6]])

print(np.sum(arr))        # 21 - total sum
print(np.sum(arr, axis=0))  # [5 7 9] - sum along columns
print(np.sum(arr, axis=1))  # [6 15] - sum along rows

print(np.mean(arr))       # 3.5 - average
print(np.median(arr))     # 3.5
print(np.std(arr))        # 1.71 - standard deviation
print(np.var(arr))        # 2.92 - variance

print(np.min(arr))        # 1
print(np.max(arr))        # 6
print(np.argmin(arr))     # 0 - index of min
print(np.argmax(arr))     # 5 - index of max

Broadcasting

NumPy automatically expands arrays to perform operations:

# Scalar broadcasting
arr = np.array([1, 2, 3, 4])
print(arr + 10)  # [11 12 13 14]
print(arr * 2)   # [2 4 6 8]

# Array broadcasting
a = np.array([[1, 2, 3],
              [4, 5, 6]])  # Shape: (2, 3)
b = np.array([10, 20, 30])  # Shape: (3,)

print(a + b)
# [[11 22 33]
#  [14 25 36]]

# Column broadcasting
c = np.array([[10], [20]])  # Shape: (2, 1)
print(a + c)
# [[11 12 13]
#  [24 25 26]]

Reshaping Arrays

arr = np.arange(12)  # [0 1 2 3 4 5 6 7 8 9 10 11]

# Reshape
reshaped = arr.reshape(3, 4)
print(reshaped)
# [[ 0  1  2  3]
#  [ 4  5  6  7]
#  [ 8  9 10 11]]

# Automatic dimension
reshaped = arr.reshape(3, -1)  # -1 means "figure it out"

# Flatten
print(reshaped.flatten())  # [0 1 2 3 4 5 6 7 8 9 10 11]
print(reshaped.ravel())    # Same, but may return view

# Transpose
print(reshaped.T)
# [[ 0  4  8]
#  [ 1  5  9]
#  [ 2  6 10]
#  [ 3  7 11]]

# Add dimension
arr = np.array([1, 2, 3])
print(arr[:, np.newaxis])  # Column vector (3, 1)
print(arr[np.newaxis, :])  # Row vector (1, 3)

Stacking and Splitting

a = np.array([1, 2, 3])
b = np.array([4, 5, 6])

# Stacking
print(np.vstack([a, b]))  # Vertical stack
# [[1 2 3]
#  [4 5 6]]

print(np.hstack([a, b]))  # Horizontal stack
# [1 2 3 4 5 6]

print(np.concatenate([a, b]))  # Same as hstack for 1D

# 2D stacking
a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6], [7, 8]])

print(np.vstack([a, b]))
# [[1 2]
#  [3 4]
#  [5 6]
#  [7 8]]

# Splitting
arr = np.array([1, 2, 3, 4, 5, 6])
print(np.split(arr, 3))  # [array([1, 2]), array([3, 4]), array([5, 6])]

arr_2d = np.array([[1, 2, 3, 4],
                   [5, 6, 7, 8]])
print(np.hsplit(arr_2d, 2))  # Split into 2 horizontally

Linear Algebra

# Matrix multiplication
a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6], [7, 8]])

print(np.dot(a, b))    # Matrix multiplication
print(a @ b)           # Same thing (Python 3.5+)

# Element-wise multiplication
print(a * b)

# Determinant
print(np.linalg.det(a))  # -2.0

# Inverse
print(np.linalg.inv(a))

# Eigenvalues and eigenvectors
eigenvalues, eigenvectors = np.linalg.eig(a)

# Solve linear equations (Ax = b)
A = np.array([[2, 1], [1, 3]])
b = np.array([4, 5])
x = np.linalg.solve(A, b)
print(x)  # [1. 1.33...]

Practical Examples

Normalize Data

data = np.array([10, 20, 30, 40, 50])

# Min-max normalization (0 to 1)
normalized = (data - data.min()) / (data.max() - data.min())
print(normalized)  # [0. 0.25 0.5 0.75 1.]

# Z-score normalization
z_scores = (data - data.mean()) / data.std()
print(z_scores)

Moving Average

data = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
window_size = 3

# Using convolution
weights = np.ones(window_size) / window_size
moving_avg = np.convolve(data, weights, mode='valid')
print(moving_avg)  # [2. 3. 4. 5. 6. 7. 8. 9.]

Image Operations

# Simulate grayscale image (2D array)
image = np.random.randint(0, 256, (100, 100))

# Flip
flipped_h = np.fliplr(image)  # Horizontal flip
flipped_v = np.flipud(image)  # Vertical flip

# Rotate 90 degrees
rotated = np.rot90(image)

# Crop
cropped = image[10:50, 20:80]

Quick Reference

FunctionDescription
np.array()Create array
np.zeros(), np.ones()Arrays of 0s or 1s
np.arange()Range array
np.linspace()Evenly spaced array
np.reshape()Change shape
np.sum(), np.mean()Aggregations
np.dot(), @Matrix multiplication
np.linalg.inv()Matrix inverse

Summary

NumPy is essential for:

  1. Fast array operations - vectorized computations
  2. Data manipulation - reshaping, slicing, indexing
  3. Mathematical operations - linear algebra, statistics
  4. Foundation for ML - used by Pandas, scikit-learn, TensorFlow

Master NumPy to unlock efficient numerical computing in Python.

Advertisement

MR

Moshiour Rahman

Software Architect & AI Engineer

Share:
MR

Moshiour Rahman

Software Architect & AI Engineer

Enterprise software architect with deep expertise in financial systems, distributed architecture, and AI-powered applications. Building large-scale systems at Fortune 500 companies. Specializing in LLM orchestration, multi-agent systems, and cloud-native solutions. I share battle-tested patterns from real enterprise projects.

Related Articles

Comments

Comments are powered by GitHub Discussions.

Configure Giscus at giscus.app to enable comments.