Answered step by step
Verified Expert Solution
Question
1 Approved Answer
# Import necessary libraries from sklearn.model _ selection import train _ test _ split from sklearn.tree import DecisionTreeClassifier, plot _ tree from sklearn.metrics import classification
# Import necessary libraries from sklearn.modelselection import traintestsplit from sklearn.tree import DecisionTreeClassifier, plottree from sklearn.metrics import classificationreport import pandas as pd import matplotlib.pyplot as plt # Sample data replace this with your actual data data 'Age': 'Gender': Woman 'Man', 'Man', 'Woman', 'Woman', 'Woman', 'Man', 'Woman', 'Man', 'Man' 'Health Status': Good 'Sick', 'Good', 'Sick', 'Sick', 'Good', 'Good', 'Sick', 'Good', 'Good' 'Employment Status': Manager 'Worker', 'Unemployed', 'Worker', 'Manager', 'Unemployed', 'Worker', 'Official', 'Official', 'Manager' 'Salary': 'Loan Granted': Yes 'Yes', No 'Yes', No 'Yes', No 'Yes', No 'Yes' # Convert the data to a DataFrame df pdDataFramedata # Convert categorical variables to numerical using onehot encoding dfencoded pdgetdummiesdf columnsGender 'Health Status', 'Employment Status' # Separate the features X and the target variable y X dfencoded.dropLoan Granted', axis y dfencodedLoan Granted' # Split the dataset into training and testing sets Xtrain, Xtest, ytrain, ytest traintestsplitX y testsize randomstate # Initialize the Decision Tree Classifier clf DecisionTreeClassifier # Train the model on the training set clffitXtrain, ytrain # Predict the target variable for the test set ypred clfpredictXtest # Evaluate the model's performance report classificationreportytest, ypred printClassification Report: report # Visualize the decision tree pltfigurefigsize plottreeclf filledTrue, featurenamesXcolumns, classnamesNo 'Yes' plttitleDecision Tree Visualization" pltshow
# Import necessary libraries
from sklearn.modelselection import traintestsplit
from sklearn.tree import DecisionTreeClassifier, plottree
from sklearn.metrics import classificationreport
import pandas as pd
import matplotlib.pyplot as plt
# Sample data replace this with your actual data
data
'Age':
'Gender': Woman 'Man', 'Man', 'Woman', 'Woman', 'Woman', 'Man', 'Woman', 'Man', 'Man'
'Health Status': Good 'Sick', 'Good', 'Sick', 'Sick', 'Good', 'Good', 'Sick', 'Good', 'Good'
'Employment Status': Manager 'Worker', 'Unemployed', 'Worker', 'Manager', 'Unemployed', 'Worker', 'Official', 'Official', 'Manager'
'Salary':
'Loan Granted': Yes 'Yes', No 'Yes', No 'Yes', No 'Yes', No 'Yes'
# Convert the data to a DataFrame
df pdDataFramedata
# Convert categorical variables to numerical using onehot encoding
dfencoded pdgetdummiesdf columnsGender 'Health Status', 'Employment Status'
# Separate the features X and the target variable y
X dfencoded.dropLoan Granted', axis
y dfencodedLoan Granted'
# Split the dataset into training and testing sets
Xtrain, Xtest, ytrain, ytest traintestsplitX y testsize randomstate
# Initialize the Decision Tree Classifier
clf DecisionTreeClassifier
# Train the model on the training set
clffitXtrain, ytrain
# Predict the target variable for the test set
ypred clfpredictXtest
# Evaluate the model's performance
report classificationreportytest, ypred
printClassification Report:
report
# Visualize the decision tree
pltfigurefigsize
plottreeclf filledTrue, featurenamesXcolumns, classnamesNo 'Yes'
plttitleDecision Tree Visualization"
pltshow
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started