How can we reshape Python Pandas DataFrame to C-Contiguous memory?
We have no direct control about how the DataFrame stores its values, which can be c-contiguous or not. However, it's easy to get C-contiguous data by using the numpy function ascontiguousarray
on the underlying numpy array, which is returned by the value
property of the array. You can test it for yourself:
X_train.flags.c_contiguous # Checks if the array is C-contiguous#>>> FalseX_train = np.ascontiguousarray(X_train) # Converts the array to C-contiguousX_train.flags.c_contiguous#>>> True
The documentation for numpy.ascontiguousarray
the can be found here:https://numpy.org/doc/stable/reference/generated/numpy.ascontiguousarray.html