Docsity

Artificial Intelligence Labs, Exercises of Artificial Intelligence

Toggle sidebar
bg1
QUESTION# 1: ( 15
marks)
Write a program in Python to implement the decision tree algorithm. You should read a comma
delimited dataset (csv file), and output to the screen your decision tree and the training set
accuracy in some readable format.
Here are csv dataset of given table :
#of reported
covid19 cases
temperature humid Followed
sopfollowe
d followed
GO OUTSIDE
LOW cool high weak no
LOW hot high strong yes
AVERAGE cool high weak no
HIGH mild high weak no
HIGH hot normal weak no
HIGH hot normal strong no
AVERAGE hot normal strong yes
LOW mild high weak no
LOW hot normal weak yes
HIGH mild normal weak no
LOW mild normal strong yes
AVERAGE mild high strong yes
AVERAGE hot normal weak no
HIGH mild high strong no
BAHRIA UNIVERSITY (KARACHI CAMPUS)
FINAL TERMEXAMINATION – SPRINGSEMESTER – 2020
(COURSE TITLE Artificial Intelligence LabCOURSE CODECSL-411)
Class: BS (CS)-6 (Morning / Evening)
Course Instructor: Irfan Mustafa
Last Date to submit: 26-6-2020 (12:00 pm) Max Marks:15
Student’s Name: Muhammad Jawwad Enrollment No:02-134172-021
Note:create a folder on your desktop of your name and enrollment (abc-001).Then Create a word(.doc) file in the
folder. The name of the file is in same pattern you create a folder (your name and enrollment)and store all output
and code screen shot in the file. Add complete coding of your program (android project folder) in main folder, and
create a .zip file of main folder and send email .zip file to ([email protected]) with the subject of
your name and enrollment (abc-001-AI(sec-“A/C”)MID20), And submit word(.doc) file on LMS before due
date.In order to avoid any run time electricity and internet unavailability situation, it is suggested that keep
yourlaptop charged. Also activate 3G/4G connection as an alternative of Wifi/internet option to upload your
solution.
covid19.csv
pf3
pf4
pf5

Partial preview of the text

Download Artificial Intelligence Labs and more Exercises Artificial Intelligence in PDF only on Docsity!

QUESTION# 1: ( 15

marks) Write a program in Python to implement the decision tree algorithm. You should read a comma delimited dataset (csv file), and output to the screen your decision tree and the training set accuracy in some readable format. Here are csv dataset of given table : #of reported covid19 cases temperature humid Followed sopfollowe d followed

GO OUTSIDE

LOW cool high weak no LOW hot high strong yes AVERAGE cool high weak no HIGH mild high weak no HIGH hot normal weak no HIGH hot normal strong no AVERAGE hot normal strong yes LOW mild high weak no LOW hot normal weak yes HIGH mild normal weak no LOW mild normal strong yes AVERAGE mild high strong yes AVERAGE hot normal weak no HIGH mild high strong no

BAHRIA UNIVERSITY (KARACHI CAMPUS )

FINAL TERMEXAMINATION – SPRINGSEMESTER – 2020 (COURSE TITLE Artificial Intelligence LabCOURSE CODECSL-411) Class: BS (CS)-6 (Morning / Evening) Course Instructor: Irfan Mustafa Last Date to submit: 26-6-2020 (12:00 pm) Max Marks: 15 Student’s Name: Muhammad Jawwad Enrollment No:02-134172- Note: create a folder on your desktop of your name and enrollment (abc-001).Then Create a word(.doc) file in the folder. The name of the file is in same pattern you create a folder ( your name and enrollment) and store all output and code screen shot in the file. Add complete coding of your program (android project folder) in main folder, and create a .zip file of main folder and send email .zip file to ([email protected]) with the subject of your name and enrollment (abc-001- AI(sec-“A/C”)MID20 ), And submit word(.doc) file on LMS before due date. In order to avoid any run time electricity and internet unavailability situation, it is suggested that keep yourlaptop charged. Also activate 3G/4G connection as an alternative of Wifi/internet option to upload your solution. covid19.csv

For output, you can choose how to draw the tree so long as it is clear what the tree is. For example: #of reported covid19 cases = LOW | humid = high: no | humid = normal: yes #of reported covid19 cases = HIGH: NO #of reported covid19 cases = AVERAGE | Followed sopfollowed followed = weak: no | Followed sopfollowed followed = strong: yes #of reported covid19 cases HIGH NO LOW TEMPRATURE COOL NO MILD HUMIDITY NORMAL YES HIGH NO HOT YES AVERAGE SOPs FOLLOWED STRONG YES WEAK NO

print ("Dataset Length: ", len(balance_data)) print ("Dataset Shape: ", balance_data.shape)

Printing the processed dataset obseravtions

print ("Dataset: ",balance_data.head()) return balance_data

Function to split the dataset

def splitdataset(balance_data):

Separating the target variable

X = balance_data.values[:, 1:5] Y = balance_data.values[:, 0]

Splitting 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

Output: