I've been following this tutorial (using my own data however)
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.
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'])
Full error Traceback:
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
I'm very new to this, all advice is greatly appreciated.
You need to specify the name of all your features:
feature_names = ['viewed','carted','knownpurchases','totlength','avgtime','stdtime','vartime','KMP','Leven','prodnum']
In the tutorial, only 1 feature (explanatory variable) is used.