티스토리 뷰

반응형

Stack Overflow에 자주 검색, 등록되는 문제들과 제가 개발 중 찾아 본 문제들 중에서 나중에도 찾아 볼 것 같은 문제들을 정리하고 있습니다.

Stack Overflow에서 가장 먼저 확인하게 되는 가장 높은 점수를 받은 Solution과 현 시점에 도움이 될 수 있는 가장 최근에 업데이트(최소 점수 확보)된 Solution을 각각 정리하였습니다.

 

아래 word cloud를 통해 이번 포스팅의 주요 키워드를 미리 확인하세요.

Cython: "fatal error: numpy/arrayobject.h: No such file or directory"

Cython: "fatal error: numpy/arrayobject.h: No such file or directory" 오류 수정

 문제 내용 

I'm trying to speed up the answer here using Cython. I try to compile the code (after doing the cygwinccompiler.py hack explained here), but get a fatal error: numpy/arrayobject.h: No such file or directory...compilation terminated error. Can anyone tell me if it's a problem with my code, or some esoteric subtlety with Cython?

저는 Cython을 사용하여 여기의 답변을 빠르게 만들려고 합니다. cygwinccompiler.py hack을 수행한 후 코드를 컴파일하려고 하지만, "numpy/arrayobject.h: No such file or directory...compilation terminated"과 같은 치명적인 오류가 발생합니다. 제 코드에 문제가 있는 건가요, 아니면 Cython의 어려운 문제 때문인가요?

 

Below is my code.

아래는 제 코드입니다.
import numpy as np
import scipy as sp
cimport numpy as np
cimport cython

cdef inline np.ndarray[np.int, ndim=1] fbincount(np.ndarray[np.int_t, ndim=1] x):
    cdef int m = np.amax(x)+1
    cdef int n = x.size
    cdef unsigned int i
    cdef np.ndarray[np.int_t, ndim=1] c = np.zeros(m, dtype=np.int)

    for i in xrange(n):
        c[<unsigned int>x[i]] += 1

    return c

cdef packed struct Point:
    np.float64_t f0, f1

@cython.boundscheck(False)
def sparsemaker(np.ndarray[np.float_t, ndim=2] X not None,
                np.ndarray[np.float_t, ndim=2] Y not None,
                np.ndarray[np.float_t, ndim=2] Z not None):

    cdef np.ndarray[np.float64_t, ndim=1] counts, factor
    cdef np.ndarray[np.int_t, ndim=1] row, col, repeats
    cdef np.ndarray[Point] indices

    cdef int x_, y_

    _, row = np.unique(X, return_inverse=True); x_ = _.size
    _, col = np.unique(Y, return_inverse=True); y_ = _.size
    indices = np.rec.fromarrays([row,col])
    _, repeats = np.unique(indices, return_inverse=True)
    counts = 1. / fbincount(repeats)
    Z.flat *= counts.take(repeats)

    return sp.sparse.csr_matrix((Z.flat,(row,col)), shape=(x_, y_)).toarray()

 

 

 높은 점수를 받은 Solution 

In your setup.py, the Extension should have the argument include_dirs=[numpy.get_include()].

setup.py에서 Extension은 인수 include_dirs=[numpy.get_include()]를 가져야 합니다.

 

Also, you are missing np.import_array() in your code.

또한 코드에서 np.import_array()가 누락되어 있습니다.

 

--

 

Example setup.py:

예시 setup.py:
from distutils.core import setup, Extension
from Cython.Build import cythonize
import numpy

setup(
    ext_modules=[
        Extension("my_module", ["my_module.c"],
                  include_dirs=[numpy.get_include()]),
    ],
)

# Or, if you use cythonize() to make the ext_modules list,
# include_dirs can be passed to setup()

setup(
    ext_modules=cythonize("my_module.pyx"),
    include_dirs=[numpy.get_include()]
)    

 

 

 가장 최근 달린 Solution 

As per this answer, if you have installed numpy with pip on Linux, you will need to manually set a symbolic link to /usr/include/numpy

이 답변에 따르면 Linux에서 pip로 numpy를 설치한 경우 /usr/include/numpy에 대한 심볼릭 링크를 수동으로 설정해야 합니다.

 

In my case the path is:

제 경우 경로는 다음과 같습니다:
sudo ln -s /usr/local/lib/python3.8/dist-packages/numpy/core/include/numpy/ /usr/include/numpy

 

 

출처 : https://stackoverflow.com/questions/14657375/cython-fatal-error-numpy-arrayobject-h-no-such-file-or-directory

반응형
댓글
공지사항
최근에 올라온 글