ValueError: ndarray is not C-contiguous in cython

后端 未结 1 903
谎友^
谎友^ 2021-02-19 02:40

I have written the following function in cython to estimate the log-likelihood

@cython.boundscheck(False)
@cython.wraparound(False)
def li         


        
1条回答
  •  不思量自难忘°
    2021-02-19 02:47

    Just before you get the error, try printing the flags attribute of the numpy array(s) you're passing to likelihood. You'll probably see something like:

    In [2]: foo.flags
    Out[2]: 
      C_CONTIGUOUS : False
      F_CONTIGUOUS : True
      OWNDATA : True
      WRITEABLE : True
      ALIGNED : True
      UPDATEIFCOPY : False
    

    Note where it says C_CONTIGUOUS : False, because that's the issue. To fix it, simply convert it to C-order:

    In [6]: foo = foo.copy(order='C')
    
    In [7]: foo.flags
    Out[7]: 
      C_CONTIGUOUS : True
      F_CONTIGUOUS : False
      OWNDATA : True
      WRITEABLE : True
      ALIGNED : True
      UPDATEIFCOPY : False
    

    0 讨论(0)
提交回复
热议问题