When reshaping arrays, it is important to consider whether the original array is stored in row-major or column-major order, since this can affect the resulting reshaped array.
In row-major order, also known as "C-order", the elements of the array are stored row-by-row, with the first index changing fastest. In column-major order, also known as "Fortran-order", the elements of the array are stored column-by-column, with the last index changing fastest.
In NumPy, arrays are stored in row-major order by default. However, some libraries and file formats use column-major order. For example, the MATLAB software uses column-major order to store arrays.
When reshaping an array, the order of the dimensions in the reshaped array should match the order of the dimensions in the original array. If the original array is stored in row-major order, then the dimensions should be ordered from left to right in the order that they change fastest. If the original array is stored in column-major order, then the dimensions should be ordered from right to left in the order that they change fastest.
Here is an example of how to reshape a two-dimensional NumPy array of time series data that is stored in column-major order into a three-dimensional array with dimensions (num_vars, num_dates, num_obs_per_var):
import numpy as np# Create a datetime64 index for the time series dataindex = np.arange('2022-01-01', '2022-01-11', dtype='datetime64[D]')# Create a two-dimensional NumPy array to store the data, in column-major orderdata = np.array([ [1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0, 10.1], [11.2, 12.3, 13.4, 14.5, 15.6, 16.7, 17.8, 18.9, 19.0, 20.1]], order='F')# Reshape the data into a three-dimensional array with dimensions (2, 10, 1)reshaped_data = data.reshape((1, 10, 2), order='F').swapaxes(0, 2)# Print the original and reshaped dataprint("Original data:")print(data)print("Reshaped data:")print(reshaped_data) |
In this example, we first create a two-dimensional NumPy array to store the data in column-major order. Then, we reshape the data into a three-dimensional array with dimensions (1, 10, 2) using the reshape function and specifying the order parameter as 'F' to indicate that the array is stored in column-major order. Finally, we use the swapaxes function to change the order of the dimensions so that the resulting array has dimensions (2, 10, 1).