RuntimeWarning:无法在不连续的输入数组上提供视图而无需复制 [英] RuntimeWarning: Cannot provide views on a non-contiguous input array without copying

查看:186
本文介绍了RuntimeWarning:无法在不连续的输入数组上提供视图而无需复制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用skimage时出现以下错误:

when using skimage I get the following error:

win = skimage.util.view_as_windows(x, windowSize, windowShift)

C:\Program Files\Anaconda2\lib\site-packages\skimage\util\shape.py:247: RuntimeWarning: Cannot provide views on a non-contiguous input array without copying.
  warn(RuntimeWarning("Cannot provide views on a non-contiguous input "

据我了解,这是因为x是非连续数组.

as far I understood this is because x is a non contiguous array.

我想我已经解决了添加代码np.ascontiguousarray的问题,如下所示:

I think I solved the problem adding in my code np.ascontiguousarray as below:

win = skimage.util.view_as_windows(np.ascontiguousarray(x), windowSize, windowShift)

这是正确的做法吗?注意:我一直在从skimage调用此函数时执行此操作...它有什么特殊含义吗?

Is this the right thing to do? Note: I do it all the time I call this function from skimage...does it have any particular implication?

推荐答案

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]: 

FLAGS属性:

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只是

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

仅在需要获得正确顺序时才做(数据缓冲区的)副本.有关更多详细信息,请参见np.array文档. x.copy()会进行复制.

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.

如果要在需要C连续数组的skimage代码中使用它们,则对所有loadmat数组进行ascontiguous调用是有意义的. view_as_windows可能正在做一些大步小把戏来制作一个(滑动的)窗口.

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.

这篇关于RuntimeWarning:无法在不连续的输入数组上提供视图而无需复制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆