Advanced Methods in Numpy
Posted: Sun Aug 10, 2025 3:37 am
Python + NumPy: Indexing, Stats, Reshaping, ufuncs, Random, Broadcasting
V. Indexing & Slicing (Lists)
What it does: Access elements, use negative indexes, and slice portions of a list.
VI (i). Aggregation — Summary Statistics (Pure Python)
What it does: Computes sum, mean, min, max, and median.
VI (ii). statistics Module — Mean, Median, Variance, Std Dev
What it does: Uses Python’s built-in statistics for common measures.
VII. Reshaping & Transposing (NumPy)
What it does: Build a 3×3 matrix, reshape it, flatten to 1D, and transpose rows↔columns.
VIII. Universal Functions (ufuncs)
What it does: Applies vectorized math (`exp`, `sin`, `log`) element-wise to arrays.
IX. Random Number Generation (NumPy)
What it does: Generates a 3×3 array of random values in [0, 1).
X. Broadcasting (NumPy)
What it does: Adds a scalar to every element without loops (broadcasting).
XI. Reshaping a 1D Array (NumPy)
What it does: Reshapes a 1D array of length 6 into a 2×3 matrix.
Quick checks before you run/post
V. Indexing & Slicing (Lists)
What it does: Access elements, use negative indexes, and slice portions of a list.
Code: Select all
# Indexing & Slicing (Lists)
my_list = [1, 2, 3, 4, 5]
# Access first element (index starts at 0)
first_element = my_list[0]
print("first_element:", first_element) # 1
Code: Select all
# Negative index: last element
last_element = my_list[-1]
print("last_element:", last_element) # 5
Code: Select all
# Slice: elements from index 1 up to (but not including) index 4
sub_list = my_list[1:4]
print("sub_list 1:4 ->", sub_list) # [2, 3, 4]
Code: Select all
# Slice with step of 2
step_list = my_list[0:5:2]
print("step_list 0:5:2 ->", step_list) # [1, 3, 5]
# From beginning to index 3 (exclusive)
partial_list_start = my_list[:3]
print("partial_list_start :", partial_list_start) # [1, 2, 3]
# From index 2 to the end
partial_list_end = my_list[2:]
print("partial_list_end :", partial_list_end) # [3, 4, 5]
What it does: Computes sum, mean, min, max, and median.
Code: Select all
# Aggregation with pure Python
exam_scores = [85, 92, 88, 78, 95, 89, 92, 87, 91, 84]
total = sum(exam_scores)
mean = total / len(exam_scores)
minimum = min(exam_scores)
maximum = max(exam_scores)
Code: Select all
# Correct median for even-length lists: average of the two middle values
sorted_scores = sorted(exam_scores)
mid = len(sorted_scores) // 2
median = (sorted_scores[mid - 1] + sorted_scores[mid]) / 2
print("total :", total) # 881
print("mean :", mean) # 88.1
print("min :", minimum) # 78
print("max :", maximum) # 95
print("median :", median) # 88.5
What it does: Uses Python’s built-in statistics for common measures.
Code: Select all
# Using the statistics module
import statistics as stats
exam_scores = [85, 92, 88, 78, 95, 89, 92, 87, 91, 84]
print("mean :", stats.mean(exam_scores)) # 88.1
print("median :", stats.median(exam_scores)) # 88.5
print("variance :", stats.variance(exam_scores)) # sample variance
print("stdev :", stats.stdev(exam_scores)) # sample standard deviation
What it does: Build a 3×3 matrix, reshape it, flatten to 1D, and transpose rows↔columns.
Code: Select all
# Reshaping & Transposing with NumPy
import numpy as np
matrix = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
# Reshape (stays 3x3 here; pattern shown)
reshaped = matrix.reshape((3, 3))
print("reshaped:\n", reshaped)
Code: Select all
# Flatten to 1D
flattened = matrix.flatten()
print("flattened:", flattened)
Code: Select all
# Transpose
transposed = np.transpose(matrix) # or matrix.T
print("transposed:\n", transposed)
What it does: Applies vectorized math (`exp`, `sin`, `log`) element-wise to arrays.
Code: Select all
# Universal functions (ufuncs)
import numpy as np
arr = np.array([1, 2, 3])
exp_arr = np.exp(arr) # e^x for each element
sin_arr = np.sin(arr) # sine of each element (radians)
log_arr = np.log(arr) # natural log of each element
print("exp :", exp_arr)
print("sin :", sin_arr)
print("log :", log_arr)
What it does: Generates a 3×3 array of random values in [0, 1).
Code: Select all
# Random number generation
import numpy as np
random_array = np.random.rand(3, 3)
print("random 3x3 array:\n", random_array)
What it does: Adds a scalar to every element without loops (broadcasting).
Code: Select all
# Broadcasting
import numpy as np
arr = np.array([1, 2, 3])
result = arr + 5 # adds 5 to each element
print("result:", result) # [6 7 8]
What it does: Reshapes a 1D array of length 6 into a 2×3 matrix.
Code: Select all
# Reshape a 1D array into 2x3
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6])
reshaped = np.reshape(arr, (2, 3))
print("reshaped 2x3:\n", reshaped)
- Does your reshape size match the number of elements? (Else it errors.)
- Need population metrics? Use pvariance/pstdev instead of variance/stdev.
- Broadcasting needs compatible shapes—double-check with arr.shape.