UnboundLocalError: local variable 'animal_signals' referenced before assignment
UnboundLocalError: local variable 'animal_signals' referenced before assignment
我有一些 Cython 代码,如果变量等于列表中的值,那么另一个列表中的值将被复制到测试数组中。
double [:] signals
cdef int total_days=signals.shape[0]
cdef size_t epoch=0
cdef int total_animals
cdef int n
cdef double[:] animal_signals
for animal in range(total_animals):
individual_animal = uniq_instr[animal]
for element in range(total_days):
if list(animal_ids[n]) == individual_animal:
animal_signals.append(signals[n])
我收到一个错误:
UnboundLocalError: local variable 'animal_signals' referenced before assignment
我想有线
cdef double[:] animal_signals
意味着数组已分配。
更新
按照建议,我也尝试声明数组 animal_signals(并删除追加):
cdef int total_days=signals.shape[0]
cdef size_t epoch=0
cdef int total_animals
cdef int n
cdef int count=0
for animal in range(total_animals):
count=0
individual_animal = uniq_instr[animal]
for element in range(total_days):
if list(animal_ids[element]) == individual_animal:
cdef double[:] animal_signals[count] = signals[n]
count=count+1
但是当我编译代码时出现错误:
Error compiling Cython file:
------------------------------------------------------------
...
for element in range(total_days):
if list(animal_ids[element]) == individual_animal:
cdef double[:] animal_signals[count] = signals[n]
^
------------------------------------------------------------
project/temps.pyx:288:21: cdef statement not allowed here
我哪里错了?
的确,你的台词cdef double[:] animal_signals
将 animal_signals
声明为一个变量,但在使用它之前你永远不会给它赋值(在 Python 中赋值是用 =
运算符完成的)。
在 Cython 中,在定义变量时使用切片 ([:]
) 表示法通常是为了获取其他对象的内存视图(参见 the reference documentation)。
例如:
some_1d_numpy_array = np.zeros((10,10)).reshape(-1)
cdef double[:] animal_signals = some_1d_numpy_array
如果你想创建一个 C 数组,你必须为它 allocate the memory(这里的大小为 number
个包含 double
的条目):
cdef double *my_array = <double *> malloc(number * sizeof(double))
此外,关于您的原始代码,请注意,在这两种情况下,您都无法在此对象上使用 append
方法,因为它不是 Python list
, 你将不得不通过他们的索引访问它的成员。
我有一些 Cython 代码,如果变量等于列表中的值,那么另一个列表中的值将被复制到测试数组中。
double [:] signals
cdef int total_days=signals.shape[0]
cdef size_t epoch=0
cdef int total_animals
cdef int n
cdef double[:] animal_signals
for animal in range(total_animals):
individual_animal = uniq_instr[animal]
for element in range(total_days):
if list(animal_ids[n]) == individual_animal:
animal_signals.append(signals[n])
我收到一个错误:
UnboundLocalError: local variable 'animal_signals' referenced before assignment
我想有线
cdef double[:] animal_signals
意味着数组已分配。
更新
按照建议,我也尝试声明数组 animal_signals(并删除追加):
cdef int total_days=signals.shape[0]
cdef size_t epoch=0
cdef int total_animals
cdef int n
cdef int count=0
for animal in range(total_animals):
count=0
individual_animal = uniq_instr[animal]
for element in range(total_days):
if list(animal_ids[element]) == individual_animal:
cdef double[:] animal_signals[count] = signals[n]
count=count+1
但是当我编译代码时出现错误:
Error compiling Cython file:
------------------------------------------------------------
...
for element in range(total_days):
if list(animal_ids[element]) == individual_animal:
cdef double[:] animal_signals[count] = signals[n]
^
------------------------------------------------------------
project/temps.pyx:288:21: cdef statement not allowed here
我哪里错了?
的确,你的台词cdef double[:] animal_signals
将 animal_signals
声明为一个变量,但在使用它之前你永远不会给它赋值(在 Python 中赋值是用 =
运算符完成的)。
在 Cython 中,在定义变量时使用切片 ([:]
) 表示法通常是为了获取其他对象的内存视图(参见 the reference documentation)。
例如:
some_1d_numpy_array = np.zeros((10,10)).reshape(-1)
cdef double[:] animal_signals = some_1d_numpy_array
如果你想创建一个 C 数组,你必须为它 allocate the memory(这里的大小为 number
个包含 double
的条目):
cdef double *my_array = <double *> malloc(number * sizeof(double))
此外,关于您的原始代码,请注意,在这两种情况下,您都无法在此对象上使用 append
方法,因为它不是 Python list
, 你将不得不通过他们的索引访问它的成员。