Numpy

What is Numpy?

NumPy is a powerful Python library for numerical computing. It allows you to create and manipulate arrays efficiently and perform complex mathematical operations with ease. Set it up with:

import numpy as np

Arrays and Dimensions

Create arrays with:

a = np.array([1, 2, 4])                    # 1D array
a = np.array([[1, 2, 4], [5, 6, 7]])       # 2D array
a = np.array([1, 2, 4], dtype='int16')     # 1D array with specified data type

Dimension explained:

  • (4,) → 1D array with 4 elements
  • (2, 3) → 2D array: 2 rows, 3 columns
  • (4, 3, 2) → 3D array

Loading Data

data = np.genfromtxt('filename.txt', delimiter=',')  # Loads as float by default
data2 = data.astype('int32')                         # Converts to int

Initialisng Arrays

np.zeros((2, 3))                # All 0s
np.ones((2, 3))                 # All 1s
np.full((2, 3), 7)              # All 7s
np.full_like(a, 4)             # Same shape as a, filled with 4s
np.full(a.shape, 4)            # Same as above

np.random.rand(3, 4)           # Random floats (0-1), shape NOT a tuple
np.random.random_sample(a.shape)  # Random floats with same shape as a
np.random.randint(0, 10, size=(2, 3))  # Random ints from 0 to 9
np.identity(4)                 # 4x4 identity matrix

Math with Arrays

array1 + array2        # Element-wise addition
array1 * array2        # Element-wise multiplication
array1 ** 2            # Element-wise exponentiation
np.sin(array1)         # Apply sin to every element
np.min(array1)         # Smallest value
np.max(array1)         # Largest value
np.sum(array1)         # Total sum

np.min(array1, axis=0)  # Min per column
np.min(array1, axis=1)  # Min per row
np.sum(array1, axis=1)  # Row sums

Array Attributes and Info

array.ndim             # Number of dimensions
array.shape            # Shape of array (rows, columns, etc.)
array.dtype            # Data type (e.g., int32)
array.itemsize         # Bytes per element
array.size             # Total number of elements
array.nbytes           # Total memory used (size * itemsize)

Reshaping and Stacking

new_array = array.reshape((8, 1))     # Change shape (e.g., from (2,4) to (8,1))

v1 = np.array([1, 2, 3, 4])
v2 = np.array([5, 6, 7, 8])
np.vstack([v1, v2])    # Stack vertically
np.hstack([v1, v2])    # Stack horizontally

Indexing and Slicing

array[1, 5]              # Value at row 2, column 6
array[0, :]              # All columns in row 1
array[:, 2]              # All rows in column 3
array[0, 1:6:2]          # Row 1, columns 2 to 6 with step 2

array[1, 5] = 15         # Set a single value
array[:, 2] = 5          # Set entire column to 5
array[:, 2] = [1, 2]     # Set column with multiple values

# 3D array indexing: outer → middle → inner
array[[1, 2, 8]]         # Select specific elements from a 1D array
array[[0,1,2,3], [1,2,3,4]]  # Index specific pairs

Boolean Masking

data2 > 50                    # Boolean array (True/False)
data2[data2 > 50]             # Extract elements > 50
np.any(data2 > 50, axis=0)    # Are ANY values > 50 in each column?
np.all(data2 > 50, axis=0)    # Are ALL values > 50 in each column?

Binomial Distribution

For experiments with two outcomes (e.g., coin flips). Example: Flip a coin 10 times, repeat 1000 experiments.

np.random.binomial(n=10, p=0.5, size=1000)
  • n: Number of trials per experiment
  • p: Probability of success (e.g., 0.5 for heads)
  • size: Number of experiments