Question: Please solve this ERROR!!!!!!! KeyError Traceback ( most recent call last ) in ( ) 9 0 9 1 # Build the decision tree -
Please solve this ERROR!!!!!!!
KeyError Traceback most recent call last
in
# Build the decision tree
decisiontree builddecisiontreedf
# Print the decision tree
frames
in builddecisiontreedata tree
# Set the outcome for the current branch
if lencounts:
treebestsplitattributevalue outcomes
else:
treebestsplitattributevalue outcomesindex
KeyError: 'House Type'
I checked my csv file but House Type data is well contained in the csv file.
import pandas as pd
import numpy as np
# Load dataset into a pandas dataframe
df pdreadcsvdatasetcsv
# Define a function to calculate entropy
def entropytargetcol:
elements, counts npuniquetargetcol, returncountsTrue
probs countslentargetcol
entropy npsumprobs nplogprobs
return entropy
# Define a function to calculate information gain
def infogaindata splitattributename, targetname"Outcome":
# Calculate the entropy of the entire dataset
totalentropy entropydatatargetname
# Calculate the values and corresponding counts for the split attribute
vals, counts npuniquedatasplitattributename returncountsTrue
# Calculate the weighted entropy of the split data
weightedentropy npsumcountsinpsumcounts entropydatawheredatasplitattributenamevalsidropnatargetname for i in rangelenvals
# Calculate the information gain
infogain totalentropy weightedentropy
return infogain
# Define a function to get the best split attribute
def getbestsplitdata:
# Get the list of column names
columns listdatacolumns
# Remove the target column name
columns.removeOutcome
# Calculate the information gain for each column
infogains infogaindata column for column in columns
# Get the index of the column with the highest information gain
bestcolumnindex npargmaxinfogains
# Return the name of the best split attribute
return columnsbestcolumnindex
# Define the decision tree building function
def builddecisiontreedata treeNone:
# Get the best split attribute
bestsplitattribute getbestsplitdata
# Get the unique values for the best split attribute
values npuniquedatabestsplitattribute
# Create a new tree node with the best split attribute
if tree is None:
tree
treebestsplitattribute
# For each value of the best split attribute, create a new branch
for value in values:
# Create a new branch for the current value
subdata data.wheredatabestsplitattribute valuedropna
# Get the most common outcome for the current branch
outcomes, counts npuniquesubdataOutcome returncountsTrue
index npargmaxcounts
# Set the outcome for the current branch
if lencounts:
treebestsplitattributevalue outcomes
else:
treebestsplitattributevalue outcomesindex
# Recursively build the subtree for the current branch
if lensubdata.dropcolumnsbestsplitattribute, 'Outcome':
subtree builddecisiontreesubdata.dropcolumnsbestsplitattribute
treebestsplitattributevalue subtree
# Return the tree
return tree
# Build the decision tree
decisiontree builddecisiontreedf
# Print the decision tree
decisiontree
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
