1. What is Numpy?¶
In the world of numerical computing with Python, Numpy is a core library. It provides a high-performance multidimensional array object and a set of tools for manipulating these arrays. Compared to Python’s native lists, Numpy arrays enable more efficient numerical operations, making them ideal for data science, machine learning, scientific computing, and other scenarios. If you’ve worked with Excel spreadsheets, matrix operations, or need to process large datasets, Numpy will be a valuable tool.
2. Installation and Import¶
First, ensure you have Numpy installed. If not, use pip:
pip install numpy
Import Numpy in your code (commonly aliased as np):
import numpy as np
3. Creating Numpy Arrays¶
Numpy arrays are the fundamental units for data processing. Here are common ways to create them:
3.1 Creating from Python Lists¶
The simplest method is np.array(), which converts a list to an array:
# 1D array
arr1 = np.array([1, 2, 3, 4])
print(arr1) # Output: [1 2 3 4]
# 2D array (list of lists)
arr2 = np.array([[1, 2, 3], [4, 5, 6]])
print(arr2)
# Output:
# [[1 2 3]
# [4 5 6]]
3.2 Using Numpy Built-in Functions¶
Numpy offers functions to generate arrays with specific structures:
- All-Zeros Array:
np.zeros(shape, dtype=float)
zeros_arr = np.zeros((3, 4)) # 3x4 array of zeros
print(zeros_arr)
# Output:
# [[0. 0. 0. 0.]
# [0. 0. 0. 0.]
# [0. 0. 0. 0.]]
- All-Ones Array:
np.ones(shape, dtype=float)
ones_arr = np.ones((2, 2)) # 2x2 array of ones
print(ones_arr)
# Output:
# [[1. 1.]
# [1. 1.]]
- Arithmetic Sequence:
np.arange(start, end, step)(similar to Python’srange)
arange_arr = np.arange(0, 10, 2) # 0, 2, 4, 6, 8
print(arange_arr) # Output: [0 2 4 6 8]
- Uniform Distribution Array:
np.linspace(start, end, num)(generatesnumevenly spaced values)
linspace_arr = np.linspace(0, 10, 5) # 5 values from 0 to 10
print(linspace_arr) # Output: [ 0. 2.5 5. 7.5 10. ]
- Random Array:
np.randomfunctions (e.g., normal distribution, uniform distribution)
# 3x3 standard normal distribution (mean=0, std=1)
rand_arr = np.random.randn(3, 3)
print(rand_arr)
4. Basic Array Attributes¶
Understanding array attributes helps you quickly grasp its structure:
shape: Array dimensions (rows, columns, etc.)ndim: Number of dimensionsdtype: Data type of elementssize: Total number of elements
Example:
arr = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.int32)
print("Shape:", arr.shape) # Output: (2, 3) (2 rows, 3 columns)
print("Dimensions:", arr.ndim) # Output: 2 (2D array)
print("Data Type:", arr.dtype) # Output: int32
print("Total Elements:", arr.size) # Output: 6 (2×3=6)
5. Array Indexing and Slicing¶
Indexing and slicing are core operations to access elements, more flexible than Python lists.
5.1 1D Array¶
arr = np.array([1, 3, 5, 7, 9])
print(arr[0]) # First element: 1
print(arr[-1]) # Last element: 9
print(arr[1:4]) # Slice: [3, 5, 7] (from index 1 to 4, exclusive)
5.2 2D Array¶
Treat 2D arrays as “tables” and use comma-separated indices for rows and columns:
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(arr[0, 1]) # Row 0, Column 1: 2
print(arr[1, :]) # Entire row 1: [4 5 6]
print(arr[:, 2]) # Entire column 2: [3 6 9]
print(arr[0:2, 1:3]) # Slice: first 2 rows, last 2 columns (left-inclusive, right-exclusive)
# Output:
# [[2 3]
# [5 6]]
5.3 Boolean Indexing (Conditional Filtering)¶
Use a boolean array to filter elements that meet a condition:
arr = np.array([1, 3, 5, 7, 9])
mask = arr > 3 # Boolean array: [False, False, True, True, True]
print(arr[mask]) # Output: [5 7 9] (elements > 3)
6. Basic Array Operations¶
Numpy arrays support efficient element-wise and matrix operations, cleaner than Python lists.
6.1 Arithmetic Operations (Element-wise)¶
Numpy array arithmetic operators (+, -, *, /) are element-wise:
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
print(arr1 + arr2) # Element-wise addition: [5, 7, 9]
print(arr1 * arr2) # Element-wise multiplication: [4, 10, 18]
print(arr1 / arr2) # Element-wise division: [0.25, 0.4, 0.5]
⚠️ Note: Python list + concatenates (e.g., [1,2]+[3,4]=[1,2,3,4]), while Numpy array + performs element-wise addition.
6.2 Matrix Operations (Linear Algebra)¶
- Matrix Multiplication: Use
np.dot()or@operator (corresponds to mathematical matrix multiplication)
A = np.array([[1, 2], [3, 4]]) # 2x2 matrix
B = np.array([[5, 6], [7, 8]]) # 2x2 matrix
C = A @ B # Matrix multiplication: A×B
print(C)
# Output:
# [[19 22]
# [43 50]]
- Dot Product:
np.dot(a, b)computes the dot product of two vectors (scalar result)
v1 = np.array([1, 2, 3])
v2 = np.array([4, 5, 6])
print(np.dot(v1, v2)) # 1×4 + 2×5 + 3×6 = 32
6.3 Broadcasting (Operations with Different Shapes)¶
Numpy automatically “broadcasts” smaller arrays to match larger arrays for valid operations:
- Array vs. Scalar: Scalars expand to all elements of the array
arr = np.array([1, 2, 3])
print(arr + 2) # Add 2 to each element: [3, 4, 5]
- Compatible Shapes: Axes with dimension 1 are expanded
arr1 = np.array([[1, 2, 3], [4, 5, 6]]) # 2x3 array
arr2 = np.array([10, 20, 30]) # 1x3 array (broadcasts to 2x3)
print(arr1 + arr2) # Add row-wise:
# [[11 22 33]
# [14 25 36]]
7. Simple Application Examples¶
7.1 Array Statistical Analysis¶
Numpy provides built-in statistical functions for quick insights:
arr = np.array([1, 2, 3, 4, 5])
print("Sum:", arr.sum()) # 15
print("Mean:", arr.mean()) # 3.0
print("Std Dev:", arr.std()) # ~1.4142
print("Max:", arr.max()) # 5
print("Min:", arr.min()) # 1
7.2 Data Filtering and Processing¶
Combine indexing and boolean operations to filter outliers:
data = np.array([10, 5, 20, 8, 15])
filtered = data[data > 10] # Filter elements > 10
print(filtered) # Output: [10, 20, 15] (10 is included as 10 > 10)
8. Summary and Exercises¶
Numpy is the foundation of Python numerical computing, enabling efficient data processing. Key takeaways:
- Array creation: From lists, zeros(), ones(), arange(), linspace(), etc.
- Array attributes: shape, ndim, dtype, size
- Indexing/slicing: 1D/2D arrays, boolean indexing
- Basic operations: Element-wise arithmetic, matrix multiplication, broadcasting
Exercise Recommendations:
1. Generate random arrays of various shapes and compute their mean/variance.
2. Implement matrix multiplication (3x3 matrix × 3x2 matrix) using Numpy.
3. Slice a 2D array, extract submatrices, and compute their sums.
With these basics, you can start exploring advanced features like linear algebra, Fourier transforms, and more to enhance your numerical computing skills!