Practical 5 Decision Tree
CODE :
import pandas as pd
from [Link] import DecisionTreeClassifier
from sklearn.model_selection import train_test_split
from sklearn import metrics
# for plotting tree
from [Link] import export_graphviz
from [Link] import StringIO
from [Link] import Image
import pydotplus
col_names = ['CB', 'PR', 'DP','BN','EW','CRML']
hoteldata = pd.read_csv("/content/crimeCNG - [Link]",
header=0, names=col_names)
print(hoteldata)
feature_cols = ['CB', 'PR', 'DP','BN','EW']
#print(hoteldata)
X = hoteldata[feature_cols] # Feature Columns
y = [Link] # 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 = [Link](X_train,y_train)
clf= [Link](X_train,y_train)
#Predict the response for test dataset
y_pred = [Link](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('[Link]')
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 :