나는 대답을 가속화하기 위해 노력하고있어 여기 사이 썬를 사용하여. 코드를 컴파일하려고 시도 했지만 ( herecygwinccompiler.py
설명 된 핵 을 수행 한 후 ) 오류가 발생합니다. 누구나 내 코드에 문제가 있거나 Cython의 난해한 미묘한 부분이 있는지 말해 줄 수 있습니까?fatal error: numpy/arrayobject.h: No such file or directory...compilation terminated
아래는 내 코드입니다.
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()
답변
당신에 setup.py
의는 Extension
인수가 있어야합니다 include_dirs=[numpy.get_include()]
.
또한 np.import_array()
코드에서 누락되었습니다 .
–
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()]
)
답변
귀하와 같은 단일 파일 프로젝트의 경우 다른 대안을 사용하는 것 pyximport
입니다. 당신은 만들 필요가 없습니다 setup.py
당신이 IPython을 사용하는 경우에도 명령 줄을 열려면 당신은 그것을 모두 아주 편리합니다 … 필요하지 않습니다 …. 귀하의 경우 IPython 또는 일반 Python 스크립트에서 다음 명령을 실행하십시오.
import numpy
import pyximport
pyximport.install(setup_args={"script_args":["--compiler=mingw32"],
"include_dirs":numpy.get_include()},
reload_support=True)
import my_pyx_module
print my_pyx_module.some_function(...)
...
물론 컴파일러를 편집해야 할 수도 있습니다. .pyx
파일 가져 오기 및 다시로드 작업은 파일 작업과 동일 .py
합니다.
출처 : http://wiki.cython.org/InstallingOnWindows
답변
이 오류는 컴파일하는 동안 numpy 헤더 파일을 찾을 수 없음을 의미합니다.
을 수행 export CFLAGS=-I/usr/lib/python2.7/site-packages/numpy/core/include/
한 다음 컴파일하십시오. 이것은 몇 가지 다른 패키지의 문제입니다. 동일한 문제로 ArchLinux에 버그가 제기되었습니다. https://bugs.archlinux.org/task/22326
답변
간단한 답변
보다 간단한 방법은 파일 경로를 추가하는 것 distutils.cfg
입니다. Windows 7의 경로는 기본적으로 C:\Python27\Lib\distutils\
입니다. 다음 내용을 주장하면 문제가 해결됩니다.
[build_ext]
include_dirs= C:\Python27\Lib\site-packages\numpy\core\include
전체 구성 파일
구성 파일이 어떻게 생겼는지 예를 들어, 전체 파일은 다음과 같습니다.
[build]
compiler = mingw32
[build_ext]
include_dirs= C:\Python27\Lib\site-packages\numpy\core\include
compiler = mingw32
답변
답변
설정 파일을 작성하고 include 디렉토리의 경로를 파악하기에 너무 게으른 경우 cyper를 시도 하십시오 . Cython 코드를 컴파일하고 설정할 수 있습니다include_dirs
하고 Numpy로 자동 .
코드를 문자열에로드 한 다음 간단히 실행 cymodule = cyper.inline(code_string)
하면 함수를 cymodule.sparsemaker
즉시 사용할 수 있습니다 . 이 같은
code = open(your_pyx_file).read()
cymodule = cyper.inline(code)
cymodule.sparsemaker(...)
# do what you want with your function
를 통해 cyper를 설치할 수 있습니다 pip install cyper
.
답변
