column_stack返回非cotiguous数组

时间:2017-04-18 19:34:33

标签: pandas numpy contiguous

我的代码中存在不连续数组的问题。 特别是我收到以下警告信息:

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 "

我正在使用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

但我得到一个不连续的数组

你知道我怎么能得到连续阵列?我应该在ascontiguousarray之后始终使用column_stack吗?

2 个答案:

答案 0 :(得分:0)

np.stack([x, y])不是连续的。但是,np.stack([x, y]).T是。

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

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

相反:

stack = np.stack([x, y]).T

答案 1 :(得分:0)

require 'test_helper'
class LogosControllerTest < ActionDispatch::IntegrationTest
  setup do
    @logo = plogos(:main_logo)
  end
  #...
  test "should get edit" do
    get edit_logo_url(@logo)
    assert_response :success
  end
  #... 
end

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 代码,https://github.com/scikit-image/scikit-image/blob/master/skimage/util/shape.py

skimage

# -- 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) 上的那个测试还可以:

column_stack

通常构造的2d数组是In [278]: xy.flags.contiguous Out[278]: True In [279]: xy.T.flags.contiguous Out[279]: False 。但他们的转置是contiguous。警告是F-contiguous将生成副本。对于可能有问题的非常大的阵列。

如果经常出现此警告,您可以禁止它,或者在调用此函数之前例行使用np.ascontiguousarray

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