By the end of this lesson, you will be able to create, inspect, and perform basic vectorized operations on NumPy arrays to accelerate numerical data analysis.
What it is
A NumPy array (ndarray) is a grid of values, all of the same type, indexed by a tuple of non-negative integers. Unlike Python lists, which can hold mixed types and store pointers to objects, NumPy arrays store contiguous blocks of raw binary data in memory. This structure allows for highly optimized mathematical operations using C-level loops rather than slow Python interpreters.
Key terms include shape (dimensions), dtype (data type), and vectorization (applying an operation to entire arrays at once).
Why it matters
- Performance: Operations are 10-100x faster than equivalent Python list comprehensions due to memory locality and compiled code.
- Conciseness: Complex mathematical expressions become single-line statements without explicit loops.
- Interoperability: It is the standard data structure for libraries like Pandas, Scikit-Learn, and TensorFlow.
- Memory Efficiency: Storing homogeneous data reduces overhead compared to Python objects.
Syntax or steps
- Import the library:
import numpy as np. - Create an array from a list:
np.array([1, 2, 3]). - Inspect properties: use
.shape,.dtype, and.size. - Perform element-wise math:
arr * 2multiplies every element by 2.
Example
import numpy as np
# Create a 1D array
scores = np.array([85, 90, 78, 92, 88])
# Basic inspection
print(f"Shape: {scores.shape}")
print(f"Dtype: {scores.dtype}")
# Vectorized operation: Normalize scores to 0-1 range
min_val = scores.min()
max_val = scores.max()
normalized = (scores - min_val) / (max_val - min_val)
print(f"Normalized: {normalized}")
# Boolean indexing: Find scores above average
avg = scores.mean()
above_avg = scores[scores > avg]
print(f"Above Average: {above_avg}")
Explanation:
np.array(...)converts a Python list into a fixed-type integer array.scores.min()andscores.max()compute statistics efficiently.- The normalization line uses broadcasting; the scalar results of
min_valandmax_valare applied to every element inscoressimultaneously. scores > avgcreates a boolean mask array. Passing this mask back intoscoresfilters the original array, returning only elements where the condition was true.
Common mistakes
- Mixed Types: If you pass
[1, "two", 3]tonp.array, NumPy casts everything to strings (<U21dtype), breaking math operations. Always ensure input data is numeric if intended for calculation. - In-place Modification Confusion:
arr + 1creates a new array. To modify the original, usearr += 1. - Indexing Errors: NumPy uses zero-based indexing like Python, but negative indices wrap around. Ensure bounds checks when slicing large datasets.
- Ignoring Dtype: Integer division truncates results. Use
astype(float)before dividing if you need decimal precision.
When to use it
| Scenario | Use NumPy Array | Use Python List |
|---|---|---|
| Numerical Computation | Yes (Fast, vectorized) | No (Slow loops) |
| Heterogeneous Data | No (Requires object dtype) | Yes (Native support) |
| Fixed Size/Type | Yes (Efficient memory) | No (Dynamic resizing) |
| Data Science Pipeline | Yes (Standard format) | No (Incompatible with ML libs) |
Practice
Guided Exercise: Create an array of numbers 1 through 10. Calculate the square of each number using vectorization (no loops). Print the result.
Hint: Use np.arange(1, 11) and multiply the array by itself (arr * arr or arr ** 2).
Challenge: Given an array of temperatures in Celsius, convert them to Fahrenheit using the formula $F = C \times 1.8 + 32$. Filter out any temperatures below freezing (0°C).
Quick check
Q: What happens if you try to add two NumPy arrays with different shapes that cannot be broadcast together?
A: A ValueError is raised because the dimensions are incompatible for element-wise operations.
Summary
NumPy arrays provide a high-performance foundation for numerical computing by storing homogeneous data contiguously and enabling vectorized operations. Mastering array creation, inspection, and broadcasting is essential for efficient data analytics and machine learning workflows.