gpt4 book ai didi

python - 类型错误 : Arrays must have consistent types in assignment

转载 作者:太空宇宙 更新时间:2023-11-04 10:32:24 24 4
gpt4 key购买 nike

跟进自 here ,我有如下代码:

@jit(float_[:,:,:](float_[:,:], int_[:], int_)) 
def train_function(X, y, H):
# do lots of stuff, including setting the arrays g and g_per_round like this:
g = np.zeros((no_features, no_classes))
g_per_round = np.zeros((H, no_features, no_classes))

# do more stuff, then:
g_h = None
j = 0
print "Calculating regression coefficients per class. .."
# building the parameters per j class
for y1_w in zip(z.T, weights.T):
y1, w = y1_w
temp_g = sm.WLS(y1, X, w).fit() # Step 2(a)(ii)
if g_h is None: # sometimes g *is* None, and that's fine
g_h = temp_g.params # this is an array of floats
else:
g_h = np.c_[g_h, temp_g.params]
j = j + 1

if np.allclose(g,0) or g is None:
g = g_h
else:
g = g + g_h

# do lots more stuff, then finally:
return g_per_round

class GentleBoostC(object):
# init functions and stuff
def train(self, X, y, H):
self.g_per_round = train_function(X, y, H)

现在我收到以下错误:

 @jit(float_[:,:,:](float_[:,:], int_[:], int_))
more lines, etc etc etc, last few lines:
unresolved_types, var_name)
File "C:\Users\app\Anaconda\lib\site-packages\numba\typesystem\ssatypes.py", line 767, in promote_arrays
assert_equal(non_array_types[0])
File "C:\Users\app\Anaconda\lib\site-packages\numba\typesystem\ssatypes.py", line 764, in assert_equal
var_name, result_type, other_type))
TypeError: Arrays must have consistent types in assignment for variable 'g': 'float64[:, :]' and 'none'

在尝试添加 @jit 来加速我的代码之前,我实际上对此没有任何问题。

最佳答案

问题是 Numba 将 g_h 推断为 NoneType;将它初始化为一个向量,它会正确地编译它:

g_h = np.zeroes((H, no_features, no_classes))

关于python - 类型错误 : Arrays must have consistent types in assignment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25688384/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com