[Python] TypeError: 'int' object is not subscriptable, python, dtreeviz

Discussão em 'Python' iniciado por Stack, Fevereiro 21, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to use the dtreeviz library to visualize a decision tree, but I’m encountering an error: TypeError: 'int' object is not subscriptable

    Here’s the code I’m trying to run:

    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


    However, when I run it, I get the following error message: TypeError: 'int' object is not subscriptable

    The traceback shows:

    ----> 2 viz = dtreeviz(modelo_iris,
    --> 254 return model.view(precision, orientation,
    --> 335 if self.shouldGoLeftAtSplit(t.id, x[t.feature()])


    Context and Attempts:

    • In my dataset, "y" is the only field with an int type (binary: 0 or 1), and "x" is a DataFrame with 4 float64 features.
    • I tried converting "y" to both pd.Series(y, dtype="str") and pd.Series(y, dtype="category"), but neither of these worked.

    Other Steps Taken:

    • I installed Graphviz from Graphviz Download and added it to the PATH, as instructed in the course.
    • Initially, I was using dtreeviz 2.2.2. I downgraded to 1.4.0, but the error persisted. My current Python version is 3.12.8.

    Environment Setup:

    • I saw that there is a note below the code that says:

    In case the graph above shows an error, run the following code (remove the # before the command "conda") conda install python-graphviz

    So I created a conda environment and ran:

    conda install -c conda-forge python-graphviz

    Despite this, the error still occurs.

    What I’ve Tried So Far:

    • I confirmed that Graphviz is installed correctly and in the PATH.
    • I tried multiple versions of dtreeviz and Graphviz, but none worked.
    • I explored other possible solutions, including changing the data format for y, but I’m still stuck.

    Question:

    I would appreciate any guidance or suggestions to resolve this error. Could this be related to a conflict between libraries or an issue with my environment setup?

    Thanks in advance for any help!

    this is the full error message:

    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

    Continue reading...

Compartilhe esta Página