0% found this document useful (0 votes)
68 views3 pages

Practical 5 Decision Tree

Decision Tree Implementation This section presents the Python code for implementing a Decision Tree algorithm. The code includes functions for constructing the decision tree, making predictions, and evaluating the model's performance. You can customize the code by adjusting parameters like the splitting criterion, maximum depth, and minimum number of samples per leaf. Experiment with different datasets and configurations to explore the effectiveness of decision trees in various classification a

Uploaded by

luckyshinde2000
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views3 pages

Practical 5 Decision Tree

Decision Tree Implementation This section presents the Python code for implementing a Decision Tree algorithm. The code includes functions for constructing the decision tree, making predictions, and evaluating the model's performance. You can customize the code by adjusting parameters like the splitting criterion, maximum depth, and minimum number of samples per leaf. Experiment with different datasets and configurations to explore the effectiveness of decision trees in various classification a

Uploaded by

luckyshinde2000
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

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 :

You might also like