Python Forum
Using ID3 Estimator for Decision Trees
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using ID3 Estimator for Decision Trees
#1
Hi, I got stuck on question d and onwards some help would be very much appreciated.
Following is the code I have so far but on question d I'm totally lost :(

%%capture --no-display
# hack omwille van bug in Id3Estimator
import six
import sys
from sklearn import tree
from sklearn.tree import DecisionTreeClassifier
import matplotlib.pyplot as plt

sys.modules['sklearn.externals.six'] = six

#todo B  We are now wondering on the basis of which criteria the teacher has given his scores To do this, set up a decision tree for the score with ID3Estimator.
from IPython.core.display_functions import display

import pandas as pd
import graphviz
from id3 import Id3Estimator, export_graphviz, export_text

scores = pd.read_csv("studentsScores.csv")
model = Id3Estimator()

# X = attributes; y = target
X = scores.drop(columns='score', axis=1).to_numpy()
# X = simpsons.drop(['name', 'gender'], axis=1).values.tolist()
y = scores['score'].to_numpy()
# y = simpsons['gender'].values.tolist()

# build model
model.fit(X, y)

# plot model
model_tree = export_graphviz(model.tree_,
                             feature_names=scores.drop('score', axis=1).columns)
display(graphviz.Source(model_tree.dot_tree))
# todo c. Which subjects does the teacher teach?
# Answer:Tree structure uses only subject4 and subject1.
# So the teacher probably gives these subjects.

# todo d We are dividing the points into categories: not successful (0-9), satisfactory (10-13), honors (14-15), highest honors (16-20). Try to classify the scores as mentioned
#Divide the subject scores into categories as mentioned above:

bins = [-1, 9, 13, 15, 21]
labels = ["not successful",
          "satisfactory",
          "honors",
          "highest honors"]

subject_columns = scores.columns[:-1]
for subject in subject_columns:#Exclude the last column 'score'
    scores[subject] = pd.cut(scores[subject], bins=bins, labels=labels)

#Important: By setting right=False, the intervals will be left-inclusive and right-exclusive, meaning that the right end of each interval is not included. This ensures that scores of 0 and 20 fall within the appropriate intervals.
import sys
sys.modules['sklearn.externals.six'] = six
from id3 import Id3Estimator, export_graphviz, export_text
model = Id3Estimator()
# X = features, y = target

X = (scores.drop(columns=['score'],axis=1)).values.tolist()
y = scores['score'].values.tolist()
model.fit(X,y)
print(export_text(model.tree_, feature_names=scores.drop(['score'], axis=1).columns))
Error:
I don't get any errors when I execute the code but the ID3 Estimator doesn't show anything as it should for the question E
Output:
As the output all I got so far is the tree generated using the ID3Estimator which was the answer to the question B and I also attached that tree in the attachments
Reply
#2
can you post a sample 'studentsScores.csv' file (small)?
Reply
#3
(Jun-13-2023, 09:58 AM)Larz60+ Wrote: can you post a sample 'studentsScores.csv' file (small)?
Sample below:
subject1,"subject2","subject3","subject4","subject5","subject6","subject7","subject8","subject9","score"
13,9,17,2,17,1,0,14,18,"average"
10,13,5,20,20,14,4,2,10,"good"
13,4,2,19,3,9,16,13,7,"good"
8,3,17,7,16,10,15,9,8,"bad"
1,12,13,2,9,10,2,2,13,"bad"
17,2,19,1,13,14,5,5,2,"average"
3,19,7,13,4,5,5,15,9,"bad"
18,18,13,16,17,5,7,0,5,"good"
3,3,8,11,16,5,3,19,12,"bad"
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Value Estimator Calculator jaycuff13 1 1,840 Apr-03-2019, 12:52 PM
Last Post: j.crater
  Value Estimator Calculator getting a TypeError jaycuff13 2 2,549 Apr-01-2019, 09:40 AM
Last Post: jaycuff13
  Decision Tree Alberto 1 3,604 Oct-22-2017, 08:23 PM
Last Post: Larz60+
  binary trees Nucifera 3 3,652 Mar-10-2017, 08:07 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020