TypeError:数组在赋值时必须具有一致的类型(TypeError: Arrays must have consistent types in assignment)

编程入门 行业动态 更新时间:2024-10-12 20:26:55
TypeError:数组在赋值时必须具有一致的类型(TypeError: Arrays must have consistent types in assignment)

从这里开始 ,我有以下代码:

@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以加速我的代码之前,我实际上没有遇到任何问题。

Following up from here, I've got code like the following:

@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)

Now I'm getting the following error:

@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'

I actually had no issues with this before trying to add @jit to speed up my code.

最满意答案

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

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

The issue is that Numba is inferring g_h to be NoneType; initialize it to a vector and it will compile it properly:

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

更多推荐

本文发布于:2023-08-05 20:26:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1437928.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:有一   赋值   数组   类型   TypeError

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!