If your array has shape (X, H:M), where H:M represents a combined time axis of hours and minutes (for example, 24 hours × 60 minutes = 1440 total time steps), and you want to create a new array of shape (X, H) by summing all the minute values within each hour, here is the conceptual NumPy-like approach:
Conceptual Approach
- The second axis currently flattens hours and minutes into a single dimension of length
H * M.
- You want to reshape this axis into two separate axes:
(H, M) so that each hour groups its 60 minutes.
- Then sum along the minute axis (the last axis) to aggregate minute values into hourly sums.
- This reduces the shape from
(X, H*M) to (X, H).
Example Code Snippet
import numpy as np
# Suppose:
X = 10 # number of other dimensions (e.g., samples)
H = 24 # number of hours
M = 60 # number of minutes per hour
# Example data: shape (X, H*M)
data = np.random.rand(X, H * M)
# Step 1: reshape the time axis into (H, M)
data_reshaped = data.reshape(X, H, M)
# Step 2: sum over the minute axis (axis=2)
hourly_sum = np.sum(data_reshaped, axis=2) # shape: (X, H)
Explanation
reshape(X, H, M) splits the combined time axis into two axes: hours and minutes.
np.sum(..., axis=2) sums all minute values within each hour.
- The result
hourly_sum has shape (X, H), with each element representing the total for that hour.
This method assumes the minutes are contiguous and ordered within each hour in the original flattened axis.
This approach is a standard multidimensional aggregation technique in NumPy and similar array-processing libraries[2][3][6].