What is a NumPy Array?
A NumPy array (ndarray) is a multidimensional array object that is much faster and more efficient than Python lists for numerical operations. All elements must be of the same type.
Creating Arrays
From Lists
import numpy as np
# 1D array
arr1d = np.array([1, 2, 3, 4, 5])
# 2D array (matrix)
arr2d = np.array([[1, 2, 3], [4, 5, 6]])
# 3D array
arr3d = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
Using Array Creation Functions
# Zeros
zeros = np.zeros((3, 4)) # 3x4 array of zeros
# Ones
ones = np.ones((2, 3)) # 2x3 array of ones
# Full (fill with specific value)
full = np.full((2, 2), 7) # 2x2 array filled with 7
# Identity matrix
identity = np.eye(3) # 3x3 identity matrix
# Range
range_arr = np.arange(0, 10, 2) # [0, 2, 4, 6, 8]
# Linspace (evenly spaced)
linspace = np.linspace(0, 1, 5) # [0., 0.25, 0.5, 0.75, 1.]
Array Properties
Get information about arrays:
arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr.shape) # (2, 3) - dimensions
print(arr.size) # 6 - total elements
print(arr.ndim) # 2 - number of dimensions
print(arr.dtype) # int64 - data type
print(arr.itemsize) # 8 - bytes per element
Array Indexing
1D Array
arr = np.array([10, 20, 30, 40, 50])
print(arr[0]) # 10 (first element)
print(arr[-1]) # 50 (last element)
print(arr[1:4]) # [20, 30, 40] (slicing)
2D Array
arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr[0, 1]) # 2 (row 0, column 1)
print(arr[0]) # [1, 2, 3] (entire row 0)
print(arr[:, 1]) # [2, 5] (entire column 1)
print(arr[0:2, 1:3]) # [[2, 3], [5, 6]] (subarray)
Array Operations
Element-wise Operations
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
# Arithmetic
print(arr1 + arr2) # [5, 7, 9]
print(arr1 * 2) # [2, 4, 6]
print(arr1 ** 2) # [1, 4, 9]
# Comparison
print(arr1 > 2) # [False, False, True]
print(arr1 == arr2) # [False, False, False]
Mathematical Functions
arr = np.array([1, 2, 3, 4, 5])
print(np.sum(arr)) # 15
print(np.mean(arr)) # 3.0
print(np.max(arr)) # 5
print(np.min(arr)) # 1
print(np.std(arr)) # Standard deviation
print(np.sqrt(arr)) # Square root of each element
Reshaping Arrays
Change array dimensions:
arr = np.array([1, 2, 3, 4, 5, 6])
# Reshape to 2x3
reshaped = arr.reshape(2, 3)
# [[1, 2, 3],
# [4, 5, 6]]
# Flatten to 1D
flat = reshaped.flatten() # [1, 2, 3, 4, 5, 6]
# Transpose
transposed = reshaped.T
# [[1, 4],
# [2, 5],
# [3, 6]]
Array Concatenation
Combine arrays:
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
# Concatenate
combined = np.concatenate([arr1, arr2]) # [1, 2, 3, 4, 5, 6]
# Stack vertically
stacked = np.vstack([arr1, arr2])
# [[1, 2, 3],
# [4, 5, 6]]
# Stack horizontally
hstacked = np.hstack([arr1, arr2]) # [1, 2, 3, 4, 5, 6]
Boolean Indexing
Filter arrays using conditions:
arr = np.array([1, 2, 3, 4, 5, 6])
# Get elements greater than 3
filtered = arr[arr > 3] # [4, 5, 6]
# Multiple conditions
filtered = arr[(arr > 2) & (arr < 6)] # [3, 4, 5]
Array vs List
| Feature | NumPy Array | Python List |
|---|---|---|
| Data Type | Homogeneous | Heterogeneous |
| Speed | Very Fast | Slower |
| Memory | Efficient | Less Efficient |
| Operations | Vectorized | Element-wise |
Best Practices
- Use arrays for numerical data: Much faster than lists
- Prefer vectorized operations: Avoid loops when possible
- Use appropriate dtypes: Choose int32 vs int64 based on needs
- Be aware of memory: Large arrays can use significant memory
- Use views vs copies: Slicing creates views, not copies
Next Steps
Learn about array operations for mathematical computations, or explore advanced indexing techniques.