title: Python: Visualizing a decision tree: IndexError: list index out of range
body: <p>I've been following <a href="https://cambridgespark.com/content/tutorials/getting-started-with-regression-and-decision-trees/index.html" rel="nofollow noreferrer">this tutorial</a> (using my own data however)</p>
  
  <p>I'm as far as trying to visualize the data as a graph, but no matter which label from my dataframe I input, it tells me that it is out of range.</p>
  
  <pre><code>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'])
  </code></pre>
  
  <p>Full error Traceback:</p>
  
  <pre><code>Traceback (most recent call last):
    File "&lt;stdin&gt;", line 1, in &lt;module&gt;
    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 &lt;module&gt;
      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
  </code></pre>
  
  <p>I'm very new to this, all advice is greatly appreciated.</p>