NumPy Notes
1. Introduction to NumPy Arrays
NumPy arrays (ndarray) are N-dimensional array objects optimized for speed and efficient
mathematical operations.
Creating Arrays: Use [Link]() to convert lists or nested lists into arrays.
Key Attributes:
- ndim: Number of dimensions.
- shape: Tuple of array dimensions (rows, columns).
- size: Total number of elements.
2. Array Initialization Methods
Instead of manually typing lists, NumPy provides functions for automatic array creation.
Fixed Values:
- [Link](): Creates an array filled with zeros.
- [Link](): Creates an array filled with ones.
- [Link](): Creates an array filled with a specific value.
- [Link]() / [Link](): Creates an identity matrix.
- [Link](): Creates an array without initializing values (random content).
Ranges:
- [Link](start, stop, step): Creates a range of values with a step.
- [Link](start, stop, num): Creates evenly spaced values between two limits.
3. Random Number Generation
Using [Link]:
- randint: Random integers in a given range.
- randn: Values from standard normal distribution.
- rand: Random floating values between 0 and 1.
4. Reshaping and Restructuring
- reshape(): Change array shape without changing data.
- flatten() / ravel(): Convert multi-dimensional arrays to 1D.
- transpose() or .T: Swap axes.
5. Mathematical & Statistical Operations
Common operations:
- min(), max(), mean(), sum()
Index operations:
- argmin(), argmax()
Cumulative:
- cumsum(): Running total
- cumprod(): Running product
6. Indexing and Slicing
- Basic slicing: array[start:stop]
- 2D indexing: array[row, col]
- 3D indexing: array[layer, row, col]
7. Boolean Indexing and Filtering
- Create masks: arr > 30
- Filter values: arr[arr > 30]
- Combine conditions: &, |
- Detect NaN: [Link]()
- Remove NaN: arr[~[Link](arr)]
- [Link](): Returns indices matching a condition