RuntimeWarning: Cannot provide views on a non-contiguous input array without copying

前提是你 提交于 2019-12-06 01:42:14
In [44]: from scipy.io import loadmat
In [45]: d = loadmat('test7.mat')
In [46]: d
Out[46]: 
{'__globals__': [],
 '__header__': b'MATLAB 5.0 MAT-file, written by Octave 4.0.0, 2016-09-01 15:43:02 UTC',
 '__version__': '1.0',
 'x': array([[ 1.,  2.,  3.],
        [ 4.,  5.,  6.]])}

In [48]: np.info(d['x'])
class:  ndarray
shape:  (2, 3)
strides:  (8, 16)
itemsize:  8
aligned:  True
contiguous:  False
fortran:  True
data pointer: 0xabfa13d8
byteorder:  little
byteswap:  False
type: float64
In [49]: 

or the FLAGS attribute:

In [52]: x.flags
Out[52]: 
  C_CONTIGUOUS : False
  F_CONTIGUOUS : True
  OWNDATA : False
  WRITEABLE : True
  ALIGNED : True
  UPDATEIFCOPY : False
In [54]: d['x'].flags['C_CONTIGUOUS']
Out[54]: False
In [55]: d['x'].flags['F_CONTIGUOUS']
Out[55]: True

np.ascontiguous just does

array(a, dtype, copy=False, order='C', ndmin=1)

It only does a copy (of the databuffer) if needed to get the right order. See the np.array docs for more details. x.copy() would make a copy regardless.

An ascontiguous call for all loadmat arrays makes sense if you are going to use them in skimage code that expects C contiguous arrays. view_as_windows is probably doing some sort of a striding tricks to make a (sliding) window.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!