Given an array with shape ((X, H:M)), where (H:M) represents a combined axis of hours and minutes (for example, (H) hours each with (M) minutes), you want to create a new array of shape ((X, H)) by summing all minute values within each hour.
Conceptual Steps Using NumPy-like Logic
-
Understand the shape:
The second axis length is (H \times M), representing (H) hours each subdivided into (M) minutes.
-
Reshape the array:
Reshape the array from ((X, H \times M)) to ((X, H, M)). This groups the minutes into their respective hours.
-
Sum over the minute axis:
Sum along the minute axis (axis=2) to aggregate all minute values into hourly totals.
-
Result:
The resulting array will have shape ((X, H)), where each element is the sum of the minute values for that hour.
Example Code Snippet
import numpy as np
# Suppose your original array has shape (X, H*M)
# For example, X=5 samples, H=24 hours, M=60 minutes
X, H, M = 5, 24, 60
data = np.random.rand(X, H * M) # Example data
# Step 1: reshape to (X, H, M)
reshaped = data.reshape(X, H, M)
# Step 2: sum over the minutes axis (axis=2)
hourly_sums = reshaped.sum(axis=2) # shape will be (X, H)
print(hourly_sums.shape) # Should print (5, 24)
Explanation
reshape(X, H, M) groups the minute data into hours.
sum(axis=2) collapses the minute dimension by summing all minute values within each hour.
- This approach assumes the minute data is contiguous and ordered by hour, then minute.
This method is efficient and leverages NumPy's fast vectorized operations without explicit loops.
This approach directly addresses your problem of converting a time axis with minute granularity into hourly aggregated values, producing a new array with shape ((X, H)).