0

I am having a problem in my code with non contiguous arrays. In particular I get the following warning message:

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 "

I am using np.column_stack

import numpy as np
x = np.array([1,2,3,4])

y = np.array([5,6,7,8])

stack = np.column_stack((x,y))

stack.flags.f_contiguous
Out[2]: False

but I get a non contiguous array

Do you know how can I get contigous array? should I use always ascontiguousarray after column_stack?

gabboshow
  • 5,359
  • 12
  • 48
  • 98
  • 1
    What do you mean by `contiguous` here? Do you mean something [like this](http://stackoverflow.com/questions/26998223/what-is-the-difference-between-contiguous-and-non-contiguous-arrays)? `np.column_stack` would simply stack the inputs as columns, which is what it's doing, whereas hstack would stack those horizontally in the sequence as the inputs are fed, two different operations. – Divakar Apr 18 '17 at 19:36
  • [Can't reproduce.](http://ideone.com/OgF4Xd) The result is contiguous for me. – user2357112 Apr 18 '17 at 19:37
  • please see my edit... – gabboshow Apr 18 '17 at 19:45
  • @Divakar Yes, sorry I thought hstack would also stack the columns... what I need is to concatenate x and y by columns – gabboshow Apr 18 '17 at 19:49
  • Are you getting this error with `stack` or `stack.T`. The `stack/concatenate` variations should all produce 2d C-contiguous arrays. `.T` will make it `F contiguous`. – hpaulj Apr 18 '17 at 20:40
  • http://stackoverflow.com/questions/43365826/runtimewarning-cannot-provide-views-on-a-non-contiguous-input-array-without-cop deals with the same warning - for a Fcontiguous array from MATLAB (via loadmat). Look at the source code for `skimage\util\shape.py`. What property is it checking? – hpaulj Apr 18 '17 at 20:43

2 Answers2

0

np.stack([x, y]) is not contiguous. However, np.stack([x, y]).T is.

np.stack([x, y])  # Transpose of what you want and not contiguous

array([[1, 2, 3, 4],
       [5, 6, 7, 8]])

Instead:

stack = np.stack([x, y]).T
piRSquared
  • 285,575
  • 57
  • 475
  • 624
0
In [276]: xy=np.column_stack((x,y))
In [277]: np.info(xy)
class:  ndarray
shape:  (4, 2)
strides:  (8, 4)
itemsize:  4
aligned:  True
contiguous:  True
fortran:  False
data pointer: 0xa836ec0
byteorder:  little
byteswap:  False
type: int32

The skimage code, https://github.com/scikit-image/scikit-image/blob/master/skimage/util/shape.py

# -- build rolling window view
if not arr_in.flags.contiguous:
    warn(RuntimeWarning("Cannot provide views on a non-contiguous input "
                        "array without copying."))

arr_in = np.ascontiguousarray(arr_in)

That test, on the column_stack is ok:

In [278]: xy.flags.contiguous
Out[278]: True
In [279]: xy.T.flags.contiguous
Out[279]: False

Normally constructed 2d arrays are contiguous. But their transpose is F-contiguous. The warning is that np.ascontiguousarray will produce a copy. For very large arrays that could be a problem.

If this warning comes up often you could either suppress it, or routinely use ascontiguousarray before calling this function.

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

Community
  • 1
  • 1
hpaulj
  • 221,503
  • 14
  • 230
  • 353