Run in Google Colab
|
View on GitHub
|
NumPy Tutorial¶
This should give you a grasp on NumPy’s essential operations, including fancy indexing and boolean masking.
1. Installation and Import¶
- Install NumPy (if you haven’t already):
# Install numpy using pip
!pip install numpy
This downloads and installs the NumPy package from the Python Package Index (PyPI).
The as np alias is a common convention that makes it easier to reference NumPy throughout your code.
# Importing NumPy
import numpy as np
2. Creating Arrays¶
NumPy arrays are the core data structure for fast numerical computation in Python. They’re more efficient than standard Python lists for large-scale numerical operations.
# From Python Lists
arr_list = [1, 2, 3, 4]
arr_np = np.array(arr_list)
print(arr_np) # [1 2 3 4]
print(arr_np.dtype) # e.g., int64
# Using Built-in Functions
zeros_arr = np.zeros((2, 3)) # 2x3 array of zeros
ones_arr = np.ones(5) # 1D array of ones
range_arr = np.arange(0, 10, 2) # [0, 2, 4, 6, 8]
lin_arr = np.linspace(0, 1, 5) # [0., 0.25, 0.5, 0.75, 1.]
print(zeros_arr)
print(ones_arr)
print(range_arr)
print(lin_arr)
3. Basic Operations¶
NumPy arrays allow you to perform element-wise arithmetic with concise syntax, which is much faster than using regular Python lists in a loop.
# Arithmetic
x = np.array([1, 2, 3])
y = np.array([10, 20, 30])
print(x + y) # [11 22 33]
print(x * 2) # [2 4 6]
print(x * y) # [10 40 90]
# Array Statistics
arr = np.array([1, 2, 3, 4, 5])
print(arr.mean()) # 3.0
print(arr.sum()) # 15
print(arr.max()) # 5
4. Reshaping and Indexing¶
# Reshape
mat = np.arange(1, 7) # [1 2 3 4 5 6]
mat_2d = mat.reshape(2, 3)
print(mat_2d)
# Slicing
# 1D slicing
arr = np.array([10, 20, 30, 40, 50])
print(arr[1:3]) # [20 30]
# 2D slicing
mat_2d = np.array([[1, 2, 3],
[4, 5, 6]])
print(mat_2d[0, 1]) # 2 (row 0, col 1)
print(mat_2d[:, 0]) # [1 4] (all rows, col 0)
5. Fancy Indexing and Boolean Masking¶
# Fancy Indexing
arr = np.array([10, 20, 30, 40, 50])
idx = np.array([2, 0, 3]) # positions to pick
print(arr[idx]) # [30 10 40]
# Boolean Masking
arr = np.array([10, 20, 30, 40, 50])
mask = arr > 25
print(mask) # [False False True True True]
print(arr[mask]) # [30 40 50]
6. Broadcasting¶
a = np.array([[1, 2, 3],
[4, 5, 6]])
b = np.array([10, 20, 30])
print(a + b)
# [[11 22 33]
# [14 25 36]]
7. Random and Useful Utilities¶
# Random numbers
rand_arr = np.random.rand(3, 2) # uniform in [0,1)
randn_arr = np.random.randn(3, 2) # normal distribution (mean=0, std=1)
# Inspect shape
print(rand_arr)
print(rand_arr.shape) # (3, 2)
8. Conclusion and Next Steps¶
Array Creation & Reshaping: Master these to handle initial data processing.
Indexing & Slicing: Use these techniques to quickly extract or modify parts of your dataset.
Fancy Indexing & Boolean Masking: Powerful shortcuts for rearranging or filtering data based on conditions.
Broadcasting: Simplifies arithmetic with mismatched shapes.
Random Utilities: Ideal for simulations, data augmentation, or test scenarios.
Questions¶
Solve the following exercises to practice NumPy basics. Follow the instructions and fill in the blanks where indicated.
There is no need to submit this notebook; it's for your practice only.
1. Array Creation and Initialization¶
Task:
Create a 1D NumPy array of the first 10 positive integers.
Create a 2x5 array of zeros.
Create a 4x4 identity matrix.
# Your solution:
# 1. Create a 1D NumPy array of the first 10 positive integers
# 2. Create a 2x5 array of zeros
# 3. Create a 4x4 identity matrix
2. Array Operations¶
Task:
Given the array arr = np.array([5, 10, 15, 20, 25]), do the following:
Multiply each element by 3.
Subtract 5 from each element.
Compute the sum of all elements in the resulting array.
# Your solution:
arr = np.array([5, 10, 15, 20, 25])
# 1. Multiply each element by 3
# 2. Subtract 5 from each element
# 3. Compute the sum of all elements
3. Reshaping and Indexing¶
Task:
Create a 1D array of integers from 1 to 12.
Reshape it into a 3x4 matrix.
Extract the element in the 2nd row, 3rd column.
Extract the first column as a 1D array.
# Your solution:
# 1. Create a 1D array of integers from 1 to 12
# 2. Reshape it into a 3x4 matrix
# 3. Extract the element in the 2nd row, 3rd column
# 4. Extract the first column as a 1D array
4. Fancy Indexing¶
Task:
Given arr = np.array([100, 200, 300, 400, 500]) and indices = np.array([4, 0, 2]):
Use fancy indexing to extract the elements at positions defined by
indices.Rearrange the extracted elements into descending order.
# Your solution:
arr = np.array([100, 200, 300, 400, 500])
indices = np.array([4, 0, 2])
# 1. Use fancy indexing to extract elements
# 2. Rearrange the extracted elements into descending order
5. Boolean Masking¶
Task:
Given arr = np.array([1, 4, 7, 10, 13, 16]):
Create a boolean mask for elements greater than 8.
Use the mask to extract those elements.
Compute the mean of the extracted elements.
# Your solution:
arr = np.array([1, 4, 7, 10, 13, 16])
# 1. Create a boolean mask for elements greater than 8
# 2. Use the mask to extract those elements
# 3. Compute the mean of the extracted elements
6. Broadcasting¶
Task:
Given the 2D array a = np.array([[1, 2, 3], [4, 5, 6]]):
Add the 1D array
b = np.array([10, 20, 30])to each row ofa.Multiply each element of
aby 2.Compute the sum of all elements in the resulting array.
# Your solution:
a = np.array([[1, 2, 3], [4, 5, 6]])
b = np.array([10, 20, 30])
# 1. Add `b` to each row of `a`
# 2. Multiply each element of `a` by 2
# 3. Compute the sum of all elements
7. Random Array Utilities¶
Task:
Generate a 3x3 array of random values uniformly distributed between 0 and 1.
Generate a 3x3 array of random values drawn from a standard normal distribution.
Compute the mean and standard deviation of each array.
# Your solution:
# 1. Generate a 3x3 array of random values uniformly distributed between 0 and 1
# 2. Generate a 3x3 array of random values drawn from a standard normal distribution
# 3. Compute the mean and standard deviation of each array
Run in Google Colab
View on GitHub