Practical 5 Decision Tree
Practical 5 Decision Tree
CODE :
import pandas as pd
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import train_test_split
from sklearn import metrics
# for plotting tree
from sklearn.tree import export_graphviz
from sklearn.externals.six import StringIO
from IPython.display import Image
import pydotplus
col_names = ['CB', 'PR', 'DP','BN','EW','CRML']
hoteldata = pd.read_csv("/content/crimeCNG - crimeCNG.csv",
header=0, names=col_names)
print(hoteldata)
feature_cols = ['CB', 'PR', 'DP','BN','EW']
#print(hoteldata)
X = hoteldata[feature_cols] # Feature Columns
y = hoteldata.CRML # Target variable
X_train, X_test, y_train, y_test = train_test_split(X, y,
test_size=0.3, random_state=1)
# 70% training and 30% test
#clf = DecisionTreeClassifier()
clf = DecisionTreeClassifier(criterion="entropy", max_depth=5)
#clf = clf.fit(X_train,y_train)
clf= clf.fit(X_train,y_train)
#Predict the response for test dataset
y_pred = clf.predict(X_test)
print("ytest = ", y_test)
print("ypred = ", y_pred)
# Accuracy of the model
print("Accuracy:",metrics.accuracy_score(y_test, y_pred))
dot_data = StringIO()
export_graphviz(clf, out_file=dot_data, filled=True, rounded=True,
feature_names = feature_cols,
class_names=['1','0'])
graph = pydotplus.graph_from_dot_data(dot_data.getvalue())
graph.write_png('criminal23.png')
Output :-
ytest = 34 1
42 0
21
21 0
30
28 1
23 0
31 0
24 0
22 0
35 0
38 0
32 0
19 0
Name: CRML, dtype: int64
ypred = [0 0 0 0 0 0 0 1 0 0 0 0 0 0]
Accuracy: 0.7142857142857143
True
PNG :