使用dtreeviz可视化决策树时遭遇"TypeError: 'int' object is not subscriptable"错误的解决方案求助
dtreeviz可视化决策树时遭遇"TypeError: 'int' object is not subscriptable"错误的解决方案求助
我最近在尝试用dtreeviz库可视化决策树的时候,碰到了一个TypeError: 'int' object is not subscriptable的错误,折腾了好几种方法都没解决,想请大家帮忙看看问题出在哪。
我运行的代码
viz = dtreeviz(modelo_iris, X_train=x, y_train=y, target_name='Tipo_Orquidea', feature_names=features, class_names=['Não Orquídea', 'Orquídea']) viz
错误信息
运行这段代码后,直接抛出了TypeError: 'int' object is not subscriptable的错误,完整的错误回溯如下:
TypeError Traceback (most recent call last) Cell In[26], line 2 1 #%matplotlib inline ----> 2 viz = dtreeviz(modelo_iris, 3 X_train= x, 4 y_train= y, 5 target_name='Tipo_Orquidea', 6 feature_names=features, 7 class_names=['Não Orquídea', 'Orquídea']) 8 viz File c:\Users\rocki\Miniconda3\lib\site-packages\dtreeviz\compatibility.py:254, in dtreeviz(tree_model, X_train, y_train, feature_names, target_name, class_names, tree_index, precision, orientation, instance_orientation, show_root_edge_labels, show_node_labels, show_just_path, fancy, histtype, highlight_path, X, max_X_features_LR, max_X_features_TD, depth_range_to_display, label_fontsize, ticks_fontsize, fontname, title, title_fontsize, colors, scale) 251 shadow_tree = ShadowDecTree.get_shadow_tree(tree_model, X_train, y_train, feature_names, target_name, class_names, 252 tree_index) 253 model = DTreeVizAPI(shadow_tree) --> 254 return model.view(precision, orientation, 255 instance_orientation, 256 show_root_edge_labels, show_node_labels, show_just_path, fancy, histtype, highlight_path, X, 257 max_X_features_LR, max_X_features_TD, depth_range_to_display, label_fontsize, ticks_fontsize, 258 fontname, title, title_fontsize, colors=colors, scale=scale) File c:\Users\rocki\Miniconda3\lib\site-packages\dtreeviz\trees.py:523, in DTreeVizAPI.view(self, precision, orientation, instance_orientation, show_root_edge_labels, show_node_labels, show_just_path, fancy, histtype, leaftype, highlight_path, x, max_X_features_LR, max_X_features_TD, depth_range_to_display, label_fontsize, ticks_fontsize, fontname, title, title_fontsize, colors, scale) 521 tmp = tempfile.gettempdir() 522 if x is not None: ... --> 335 if self.shouldGoLeftAtSplit(t.id, x[t.feature()]): 336 return walk(t.left, x, path) 337 return walk(t.right, x, path) TypeError: 'int' object is not subscriptable
我的数据集情况和已尝试的解决步骤
- 数据集里,
y是唯一的int类型字段(二分类:0或1),x是包含4个float64特征的DataFrame。 - 尝试过将
y转换为pd.Series(y, dtype="str")和pd.Series(y, dtype="category"),均无效。 - 已按教程安装Graphviz并添加到系统PATH,同时在conda环境中执行了
conda install -c conda-forge python-graphviz命令。 - 先后使用过dtreeviz 2.2.2和降级后的1.4.0版本,错误依旧存在;当前Python版本为3.12.8。
- 确认过Graphviz安装正确且已在PATH中,也尝试过不同版本的Graphviz,问题未解决。
我的疑问
我现在实在找不到问题所在了,想请教大家:这个错误会不会是库之间的冲突导致的?还是我的环境配置有什么遗漏的地方?有没有其他可能的解决方案可以尝试?
麻烦各位帮忙指点一下,谢谢!
备注:内容来源于stack exchange,提问作者Rodrigo Higashi De Figueiredo
相关产品推荐
相关产品推荐

