NumPy Array Split
Splitting arrays in NumPy allows you to divide an array into multiple subarrays. This is useful for segmenting datasets, such as breaking rainfall data into subsets for different regions like Madurai (Tamil Nadu, India) and Bhopal (Madhya Pradesh, India).
Key Topics
Splitting 1D Arrays
Use the array_split()
function to split a 1D array into specified parts. This function can handle uneven splits as well.
Example
# Splitting a 1D array
import numpy as np
rainfall = np.array([100, 200, 150, 175, 120, 300])
splits = np.array_split(rainfall, 3)
for i, subset in enumerate(splits):
print(f"Subset {i+1}:", subset)
Output
Subset 2: [150 175]
Subset 3: [120 300]
Explanation: The array_split()
function divides the array into 3 parts. If the split is uneven, some subsets will have more elements than others.
Splitting 2D Arrays
2D arrays can be split along rows or columns by specifying the axis
parameter. This is useful for segmenting tabular data, such as rainfall data for Coimbatore (Tamil Nadu, India) and Nagpur (Maharashtra, India).
Example
# Splitting a 2D array
rainfall = np.array([[100, 200, 150], [175, 120, 300]])
# Split along rows
row_splits = np.array_split(rainfall, 2, axis=0)
# Split along columns
column_splits = np.array_split(rainfall, 3, axis=1)
print("Row splits:")
for i, subset in enumerate(row_splits):
print(f"Subset {i+1}:")
print(subset)
print("\nColumn splits:")
for i, subset in enumerate(column_splits):
print(f"Subset {i+1}:")
print(subset)
Output
Subset 1:
[[100 200 150]]
Subset 2:
[[175 120 300]]
Column splits:
Subset 1:
[[100]
[175]]
Subset 2:
[[200]
[120]]
Subset 3:
[[150]
[300]]
Explanation: When axis=0
, the array is split along rows. When axis=1
, it is split along columns.
Key Takeaways
- 1D Splitting: Use
array_split()
to divide 1D arrays into specified parts. - 2D Splitting: Split along rows or columns by specifying
axis
. - Flexible Splits: Uneven splits are supported, ensuring all elements are included.
- Real-World Applications: Segment rainfall and population datasets for cities like Madurai, Bhopal, Coimbatore, and Nagpur.