Python Forum

Full Version: Classification and Regression tree (CART)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
import sklearn
from sklearn.preprocessing import LabelEncoder
import pandas as pd
import numpy as np
data=pd.read_excel('CART.xlsx')
le_age=LabelEncoder()
le_income=LabelEncoder()
le_student=LabelEncoder()
le_credit_rating=LabelEncoder()
le_buys_computer=LabelEncoder()
data['age_n']=le_age.fit_transform(data['age'])
data['income_n']=le_income.fit_transform(data['income'])
data['student_n']=le_student.fit_transform(data['student'])
data['credit_rating_n']=le_credit_rating.fit_transform(data['credit_rating'])
data['buys_computer_n']=le_credit_rating.fit_transform(data['buys_computer'])

from sklearn.tree import DecisionTreeClassifier
clf=DecisionTreeClassifier()
dt=clf.fit(x,y)
dt
DecisionTreeClassifier(class_weight=None,criterion='gini',max_depth=None,max_features=None,max_leaf_nodes=None,min_impurity_decrease=0.0,
                      min_impurity_split=None,min_samples_leaf=1,min_samples_split=2,min_weight_fraction_leaf=0.0,presort=False,random_state=None,splitter='best')

from sklearn.tree import export_graphviz
from sklearn.externals.six import StringIO
from IPython.display import Image
import pydotplus

dot_data=StringIO()
export_graphviz(dt,out_file=dot_data,filled=True,rounded=True,special_characters=True,feature_names=feature_cols,
                class_names=['0','1'])
graph=pydotplus.graph_from_dot_data(dot_data.getvalue())
graph.write_png('buys_computer.png')
Error:
--------------------------------------------------------------------------- InvocationException Traceback (most recent call last) <ipython-input-17-5ddeb7a2766f> in <module> 3 class_names=['0','1']) 4 graph=pydotplus.graph_from_dot_data(dot_data.getvalue()) ----> 5 graph.write_png('buys_computer.png') C:\ProgramData\Anaconda3\lib\site-packages\pydotplus\graphviz.py in <lambda>(path, f, prog) 1808 lambda path, 1809 f=frmt, -> 1810 prog=self.prog: self.write(path, format=f, prog=prog) 1811 ) 1812 C:\ProgramData\Anaconda3\lib\site-packages\pydotplus\graphviz.py in write(self, path, prog, format) 1916 1917 else: -> 1918 fobj.write(self.create(prog, format)) 1919 finally: 1920 if close: C:\ProgramData\Anaconda3\lib\site-packages\pydotplus\graphviz.py in create(self, prog, format) 1958 if self.progs is None: 1959 raise InvocationException( -> 1960 'GraphViz\'s executables not found') 1961 1962 if prog not in self.progs: InvocationException: GraphViz's executables not found
Please help in short out above issue
Can u please support to resolve the above program error
Have you installed and imported GraphViz? https://pypi.org/project/graphviz/