我一直试图建立我的第一个决策树可视化。我已经能够创建算法,甚至计算它的准确性,但我从来没有设法产生一个节点分裂的良好可视化。我在网上找到了这段代码:
import pandas as pd
df = pd.read_csv('data_clean.csv')
print(df.columns)
y = df['OR']
x = df.drop(['OR', 'Unnamed: 0'], axis=1)
print(y.head())
print(x.head())
from sklearn.tree import DecisionTreeClassifier
dtree = DecisionTreeClassifier()
dtree.fit(x, y)
from sklearn.externals.six import StringIO
from IPython.display import Image, display
from sklearn.tree import export_graphviz
import pydotplus
dot_data = StringIO()
export_graphviz(dtree, out_file=dot_data,
filled=True, rounded=True,
special_characters=True)
G = pydotplus.graph_from_dot_data(dot_data.getvalue())
plt = Image(G.create_png())
display(plt)
它不会提供任何有价值的输出(特别是错误),除了控制台中的输出:
/Users/arturdzieciol/anaconda3/envs/uniqa_scenarios/bin/python /Users/arturdzieciol/PycharmProjects/drzewo_decyzyjne/titanic_kaggle.py
Index(['Unnamed: 0', 'OR', 'production_year', 'mileage', 'car_worth',
'main_driver_age'],
dtype='object')
0 0
1 1
2 1
3 1
4 0
Name: OR, dtype: int64
production_year mileage car_worth main_driver_age
0 2000.0 198500.0 4770.0 59.0
1 2003.0 156000.0 7380.0 44.0
2 2014.0 30530.0 43080.0 46.0
3 2007.0 223000.0 16040.0 31.0
4 2003.0 380000.0 6930.0 42.0
<IPython.core.display.Image object>
Process finished with exit code 0
我做错什么了?