Python:可视化决策树:IndexError:列表索引超出范围

时间:2018-05-25 14:55:32

标签: python scikit-learn graphviz

我一直关注this tutorial(但是使用我自己的数据)

我试图将数据可视化为图形,但无论我输入的数据帧中的哪个标签,它都告诉我它超出了范围。

clusterDF=pd.DataFrame(data=clusterdata[:,:],index=list(range(len(clusterdata))),\
    columns=['viewed','carted','knownpurchases','totlength','avgtime','stdtime','vartime','KMP','Leven','prodnum','Class'])

X = clusterDF.drop('Class', axis=1)  
y = clusterDF['Class']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20)
regressor = DecisionTreeRegressor(max_depth=2)  
regressor.fit(X_train, y_train)

y_pred = regressor.predict(X_test)

df=pd.DataFrame({'Actual':y_test, 'Predicted':y_pred})

#Problematic line
export_graphviz(regressor, out_file='foo.dot', feature_names=['carted'])

完整错误回溯:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3/dist-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 699, in runfile
    execfile(filename, namespace)
  File "/usr/lib/python3/dist-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 88, in execfile
    exec(compile(open(filename, 'rb').read(), filename, 'exec'), namespace)
  File "/home/jess/Documents/IIIM/Discovery Stream/parser.py", line 383, in <module>
    export_graphviz(regressor, out_file='foo.dot', feature_names=[X['carted']])
  File "/usr/lib/python3/dist-packages/sklearn/tree/export.py", line 403, in export_graphviz
    recurse(decision_tree.tree_, 0, criterion=decision_tree.criterion)
  File "/usr/lib/python3/dist-packages/sklearn/tree/export.py", line 302, in recurse
    node_to_str(tree, node_id, criterion)))
  File "/usr/lib/python3/dist-packages/sklearn/tree/export.py", line 200, in node_to_str
    feature = feature_names[tree.feature[node_id]]
IndexError: list index out of range

我对此非常陌生,所有建议都非常感谢。

1 个答案:

答案 0 :(得分:2)

您需要指定所有功能的名称:

feature_names = ['viewed','carted','knownpurchases','totlength','avgtime','stdtime','vartime','KMP','Leven','prodnum']

在本教程中,仅使用了1个特征(解释变量)。