0% found this document useful (0 votes)
5 views

Lab # 10

The lab manual for CS3151 - Artificial Intelligence Lab at the University of Management and Technology outlines the objectives, task distribution, and detailed instructions for performing decision tree analysis using scikit-learn. It includes key concepts, steps for building decision trees, applications, and code examples for implementation. Students are required to complete an exercise and follow specific submission instructions for their work.

Uploaded by

hamidraza
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Lab # 10

The lab manual for CS3151 - Artificial Intelligence Lab at the University of Management and Technology outlines the objectives, task distribution, and detailed instructions for performing decision tree analysis using scikit-learn. It includes key concepts, steps for building decision trees, applications, and code examples for implementation. Students are required to complete an exercise and follow specific submission instructions for their work.

Uploaded by

hamidraza
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

University Of Management and Technology, Lahore

Lab Manual 10
CS3151-Artificial Intelligence Lab

Course Instructor Mr. Junaid Abdullah


Mansoor
Lab Instructor (s) Mr. Junaid Abdullah
Mansoor
Section V6
Semester Spring 2024
CS3151: Artificial Intelligence Lab

Table of Contents
1 Objectives ............................................................................................................................... 3
2 Task Distribution ..................................................................................................................... 3
3 Decision Tree .......................................................................................................................... 3
3.1 Key Concepts ................................................................................................................... 3
3.2 Component ....................................................................................................................... 3
4 Steps in Performing Decision Tree: ........................................................................................ 4
4.1 Applications of Decision Tree .......................................................................................... 4
5 Visualization ........................................................................................................................... 4
6 Code ........................................................................................................................................ 4
7 Exercise (50 Marks) ................................................................................................................ 5
8 Submission Instructions .......................................................................................................... 5
CS3151: Artificial Intelligence Lab

1 Objectives
After performing this lab, students shall be able to understand the following:
 Linear Regression
 Linear Regression model with scikit-learn

2 Task Distribution
Total Time 170 Minutes

Decision Tree 15 Minutes

Decision Tree model with scikit- 25 Minutes


learn

Exercise 120 Minutes

Online Submission 10 Minutes

3 Decision Tree
A decision tree is a flowchart-like structure used to make decisions or predictions. It consists of
nodes representing decisions or tests on attributes, branches representing the outcome of these
decisions, and leaf nodes representing final outcomes or predictions. Each internal node
corresponds to a test on an attribute, each branch corresponds to the result of the test, and each leaf
node corresponds to a class label or a continuous value.

3.1 Key Concepts


 Root Node: The topmost node in a decision tree. It represents the entire dataset, which is then
divided into two or more homogeneous sets.

 Decision Nodes: These are the nodes where the data is split based on the features of the
dataset.

 Leaf Nodes (Terminal Nodes): These nodes represent the final outcome or decision. Each leaf
node corresponds to a class label (in classification tasks) or a continuous value (in regression
tasks).

3.2 Component
 Features: Attributes or variables used to make decisions at each node.
CS3151: Artificial Intelligence Lab

 Splits: Criteria for dividing the data at each node, typically based on feature values. Common
criteria include Gini impurity, entropy, or variance reduction.

 Branches: The outcomes of the splits, leading to the next decision node or a leaf node.

4 Steps in Performing Decision Tree:


 Selecting the Best Feature: The algorithm evaluates all the features and selects the one that
best separates the data based on a certain criterion (e.g., Gini impurity, information gain).
 Splitting the Data: The data is divided into subsets based on the selected feature and the
specific split criterion.
 Recursion: The above steps are repeated for each subset, creating smaller and smaller
branches until a stopping condition is met (e.g., maximum depth, minimum samples per leaf, or
no further improvement).

4.1 Applications of Decision Tree


 Classification Tasks: For example, determining whether an email is spam or not.
 Regression Tasks: For example, predicting house prices.
 Feature Selection: Identifying the most important features in a dataset.
 Decision Analysis: Modeling decision-making processes in business and management..

5 Visualization

Decision trees can be visualized, making them particularly useful for understanding the logic
behind predictions. Visualization helps in interpreting the results and in communicating the
decision rules derived from the model.

In summary, decision trees are a versatile and powerful tool in the machine learning toolbox,
known for their simplicity, interpretability, and ability to model complex relationships. However,
care must be taken to manage their tendency to overfit and their sensitivity to data changes.

6 Code
# Step 1: Import Libraries
import numpy as np
import pandas as pd
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score, classification_report
from sklearn.tree import export_text, plot_tree
CS3151: Artificial Intelligence Lab

import matplotlib.pyplot as plt

# Step 2: Load Dataset


iris = datasets.load_iris()
X = iris.data
y = iris.target

# Step 3: Split Dataset


X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)

# Step 4: Train Decision Tree Classifier


clf = DecisionTreeClassifier(random_state=42)
clf.fit(X_train, y_train)

# Step 5: Make Predictions


y_pred = clf.predict(X_test)

# Step 6: Evaluate Model


accuracy = accuracy_score(y_test, y_pred)
print(f'Accuracy: {accuracy:.2f}')
print('Classification Report:')
print(classification_report(y_test, y_pred, target_names=iris.target_names))

# Step 7: Visualize Decision Tree


# Print the tree in text format
tree_rules = export_text(clf, feature_names=iris.feature_names)
print(tree_rules)

# Plot the tree


plt.figure(figsize=(20,10))
plot_tree(clf, filled=True, feature_names=iris.feature_names, class_names=iris.target_names)
plt.show()

7 Exercise (50 Marks)

8 Submission Instructions
Always read the submission instructions carefully.
• Rename your Jupyter notebook to your roll number and download the notebook as
.ipynb extension.
• To download the required file, go to File->Download .ipynb
• Only submit the .ipynb file. DO NOT zip or rar your submission file.
CS3151: Artificial Intelligence Lab

• Submit this file on Google Classroom under the relevant assignment.


• Late submissions will not be accepted

You might also like