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

Ai Labtask13

The document outlines a task for the AI Lab at SZABIST-ISB, focusing on implementing a Decision Tree Classifier using Python and the scikit-learn library. It includes a sample dataset for predicting default risk based on customer attributes, along with code for data preparation, model training, and user input for predictions. The task emphasizes the importance of encoding categorical variables and evaluating model accuracy.

Uploaded by

Abdul Moix
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)
0 views3 pages

Ai Labtask13

The document outlines a task for the AI Lab at SZABIST-ISB, focusing on implementing a Decision Tree Classifier using Python and the scikit-learn library. It includes a sample dataset for predicting default risk based on customer attributes, along with code for data preparation, model training, and user input for predictions. The task emphasizes the importance of encoding categorical variables and evaluating model accuracy.

Uploaded by

Abdul Moix
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

Shaheed Zulfikar Ali Bhutto Institute of Science & Technology

COMPUTER SCIENCE DEPARTMENT

Total Marks: 03

Obtained Marks:

AI Lab
Task # 13

Submitted To: Sir Jawad Naseer


_______________________________________________________________________________________________________________________________________________________________________________________________________________________________________

Student Name:
_______________________________________________________________________________________________________________________________________________________________________________________________________________________________________

Reg Number:
_______________________________________________________________________________________________________________________________________________________________________________________________________________________________________

AI LAB BS(CS) 6C SZABIST-ISB


Shaheed Zulfikar Ali Bhutto Institute of Science & Technology
COMPUTER SCIENCE DEPARTMENT

Q1.
Code
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.preprocessing import LabelEncoder
from sklearn.metrics import accuracy_score

# Sample data (replace with actual dataset)


data = pd.DataFrame({
'age': [25, 45, 35, 50, 23, 40, 30, 60],
'job': ['admin', 'blue-collar', 'technician', 'management', 'student', 'retired', 'services', 'unemployed'],
'marital': ['single', 'married', 'divorced', 'married', 'single', 'divorced', 'married', 'single'],
'education': ['secondary', 'tertiary', 'primary', 'tertiary', 'secondary', 'primary', 'secondary', 'tertiary'],
'housing': ['yes', 'no', 'yes', 'no', 'yes', 'no', 'yes', 'no'],
'balance': [1000, -500, 200, -1000, 1500, 300, -200, 4000],
'default': ['no', 'yes', 'no', 'yes', 'no', 'no', 'yes', 'no']
})

# a. Create derived column default_risk


data['default_risk'] = np.where((data['default'] == 'yes') | (data['balance'] < 0), 'yes', 'no')

# b. Prepare features and target


features = ['age', 'job', 'marital', 'education', 'housing']
X = data[features]
y = data['default_risk']

# Encode categorical variables


le_dict = {}
for col in ['job', 'marital', 'education', 'housing']:
le = LabelEncoder()
X[col] = le.fit_transform(X[col])
le_dict[col] = le

# Split data
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Train Decision Tree Classifier


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

# Evaluate model
y_pred = clf.predict(X_test)
print(f"Model Accuracy: {accuracy_score(y_test, y_pred):.2f}")

# c. User input and prediction


def predict_default_risk():
print("\nEnter customer details:")
age = int(input("Age: "))
job = input("Job (admin/blue-collar/technician/management/student/retired/services/unemployed): ")
marital = input("Marital status (single/married/divorced): ")
education = input("Education (primary/secondary/tertiary): ")
housing = input("Housing loan (yes/no): ")

# Prepare input data


input_data = pd.DataFrame({

AI LAB BS(CS) 6C SZABIST-ISB


Shaheed Zulfikar Ali Bhutto Institute of Science & Technology
COMPUTER SCIENCE DEPARTMENT
'age': [age],
'job': [job],
'marital': [marital],
'education': [education],
'housing': [housing]
})

# Encode categorical inputs


for col in ['job', 'marital', 'education', 'housing']:
try:
input_data[col] = le_dict[col].transform(input_data[col])
except ValueError:
print(f"Invalid input for {col}. Please use one of the specified categories.")
return

# Make prediction
prediction = clf.predict(input_data)
print(f"\nPrediction: Customer is {'at risk' if prediction[0] == 'yes' else 'not at risk'} of defaulting.")

# Run prediction
predict_default_risk()

screenshot 1

screenshot 2

AI LAB BS(CS) 6C SZABIST-ISB

You might also like