python - numpy inserting axis makes data non-contiguous -
why inserting new axis make data non-contiguous?
>>> = np.arange(12).reshape(3,4,order='f') >>> array([[ 0, 3, 6, 9], [ 1, 4, 7, 10], [ 2, 5, 8, 11]]) >>> a.reshape((3,1,4)).flags c_contiguous : false f_contiguous : false owndata : false writeable : true aligned : true updateifcopy : false >>> a[np.newaxis,...].flags c_contiguous : false f_contiguous : false owndata : false writeable : true aligned : true updateifcopy : false >>> a.flags c_contiguous : false f_contiguous : true owndata : false writeable : true aligned : true updateifcopy : false
note if use c
ordering, maintain contiguous data when reshape, not when add new axis:
>>> array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]]) >>> a.flags c_contiguous : true f_contiguous : false owndata : false writeable : true aligned : true updateifcopy : false >>> a.reshape(3,1,4).flags c_contiguous : true f_contiguous : false owndata : false writeable : true aligned : true updateifcopy : false >>> a[np.newaxis,...].flags c_contiguous : false f_contiguous : false owndata : false writeable : true aligned : true updateifcopy : false
update might find in search, keep current array order in reshape, a.reshape(3,1,4,order='a')
works , keeps contiguous array contiguous.
for asking "why care?", part of script passing arrays in fortran order fortran subroutines compiled via f2py
. fortran routines require 3d data i'm padding arrays new dimensions them required number of dimensions. i'd keep contiguous data avoid copy-in/copy-out behavior.
this doesn't answer question may of use: can make use of numpy.require np.require(a[np.newaxis,...], requirements='fa').flags
c_contiguous : false
f_contiguous : true
owndata : true
writeable : true
aligned : true
updateifcopy : false
Comments
Post a Comment