Introduction to NumPy
NumPy provides efficient numerical array operations in Python. It is the foundation for most data science libraries. Understanding NumPy enables fast data manipulation.
Creating Arrays
NumPy arrays are created from lists: np.array([1, 2, 3]). Special functions create arrays of zeros, ones, or ranges: np.zeros(5), np.ones((3,3)), np.arange(0, 10, 2).
The dtype parameter specifies data type: int32, float64. This affects memory use and precision.
Array shape is accessed via .shape. Reshaping changes shape: arr.reshape(2, 3).
Array Operations
Element-wise operations apply to each element. Arithmetic (+, -, *, /) works directly on arrays. This eliminates loops.
Aggregation functions compute summary statistics: sum, mean, std, min, max. Axis parameter controls direction.
Broadcasting enables operations on different shapes. Small arrays broadcast to match larger arrays.
Key Takeaways
- NumPy provides efficient array operations essential for data science
- Arrays enable vectorized operations without loops
- Broadcasting handles different-shaped arrays elegantly