Python code for executing decision tree algorithm

0 votes
Can I please have python code for executing decision tree algorithm? Thank you
May 13, 2019 in Machine Learning by Nayank
3,559 views

1 answer to this question.

0 votes

Hey! Try this:

# Run this program on your local python 
# interpreter, provided you have installed 
# the required libraries. 
# Importing the required packages 
import numpy as np 
import pandas as pd 
from sklearn.metrics import confusion_matrix 
from sklearn.cross_validation import train_test_split 
from sklearn.tree import DecisionTreeClassifier 
from sklearn.metrics import accuracy_score 
from sklearn.metrics import classification_report 
# Function importing Dataset 
def importdata(): 
balance_data = pd.read_csv( 
'https://archive.ics.uci.edu/ml/machine-learning-'+
'databases/balance-scale/balance-scale.data', 
sep= ',', header = None) 
# Printing the dataswet shape 
print ("Dataset Lenght: ", len(balance_data)) 
print ("Dataset Shape: ", balance_data.shape) 
# Printing the dataset obseravtions 
print ("Dataset: ",balance_data.head()) 
return balance_data 
# Function to split the dataset 
def splitdataset(balance_data): 
# Seperating the target variable 
X = balance_data.values[:, 1:5] 
Y = balance_data.values[:, 0] 
# Spliting the dataset into train and test 
X_train, X_test, y_train, y_test = train_test_split( 
X, Y, test_size = 0.3, random_state = 100) 
return X, Y, X_train, X_test, y_train, y_test 
# Function to perform training with giniIndex. 
def train_using_gini(X_train, X_test, y_train): 
# Creating the classifier object 
clf_gini = DecisionTreeClassifier(criterion = "gini", 
random_state = 100,max_depth=3, min_samples_leaf=5) 
# Performing training 
clf_gini.fit(X_train, y_train) 
return clf_gini 
# Function to perform training with entropy. 
def tarin_using_entropy(X_train, X_test, y_train): 
# Decision tree with entropy 
clf_entropy = DecisionTreeClassifier( 
criterion = "entropy", random_state = 100, 
max_depth = 3, min_samples_leaf = 5) 
# Performing training 
clf_entropy.fit(X_train, y_train) 
return clf_entropy 
# Function to make predictions 
def prediction(X_test, clf_object): 
# Predicton on test with giniIndex 
y_pred = clf_object.predict(X_test) 
print("Predicted values:") 
print(y_pred) 
return y_pred 
# Function to calculate accuracy 
def cal_accuracy(y_test, y_pred): 
print("Confusion Matrix: ", 
confusion_matrix(y_test, y_pred)) 
print ("Accuracy : ", 
accuracy_score(y_test,y_pred)*100) 
print("Report : ", 
classification_report(y_test, y_pred)) 
# Driver code 
def main(): 
# Building Phase 
data = importdata() 
X, Y, X_train, X_test, y_train, y_test = splitdataset(data) 
clf_gini = train_using_gini(X_train, X_test, y_train) 
clf_entropy = tarin_using_entropy(X_train, X_test, y_train) 
# Operational Phase 
print("Results Using Gini Index:") 
# Prediction using gini 
y_pred_gini = prediction(X_test, clf_gini) 
cal_accuracy(y_test, y_pred_gini) 
print("Results Using Entropy:") 
# Prediction using entropy 
y_pred_entropy = prediction(X_test, clf_entropy) 
cal_accuracy(y_test, y_pred_entropy) 
# Calling main function 
if __name__=="__main__": 
main() 
Discover the Power of AI, Enroll now in the midjourney course today!
answered May 13, 2019 by Haseeb

Related Questions In Machine Learning

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Example to run KNN algorithm using python

Have a look at this one: from sklearn.datasets ...READ MORE

answered May 9, 2019 in Machine Learning by Harvy
1,609 views
0 votes
1 answer
0 votes
1 answer

Real world applications of Machine Learning

Few real-world applications of machine learning are  Have ...READ MORE

answered May 10, 2019 in Machine Learning by Jinu
853 views
0 votes
1 answer

Advantages of using decision tree?

These are the advantages of using a ...READ MORE

answered May 13, 2019 in Machine Learning by Mahima
4,226 views
0 votes
1 answer

Disadvantages of using decision tree?

Even though the decision tree algorithm has ...READ MORE

answered May 13, 2019 in Machine Learning by Jinu
4,712 views
0 votes
1 answer

What is decision tree algorithm?

A decision tree is a map of ...READ MORE

answered May 13, 2019 in Machine Learning by Upadhya
2,572 views
0 votes
1 answer

What is greedy approach in Decision tree algorithm?

“Greedy Approach is based on the concept ...READ MORE

answered May 13, 2019 in Machine Learning by Upadhya
14,899 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP