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
I got stuck on d and onwards some help would be very much appreciated :)))

%%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))
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
Error:
As for the errors I got no errors for all the code I executed
Larz60+ write Jun-12-2023, 08:42 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
We need to be able to copy code.

Attached Files

Thumbnail(s)
               

.csv   studentsScores.csv (Size: 3.07 KB / Downloads: 119)
Reply
#2
You might get some help if your question was more specific.
Reply
#3
(Jun-13-2023, 01:52 PM)deanhystad Wrote: You might get some help if your question was more specific.

It was specific enough.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Binary expression trees CristinaSpanoche 1 2,133 Oct-23-2020, 08:37 AM
Last Post: bowlofred
  Decision statement problems erfanakbari1 2 2,353 Mar-06-2019, 12:27 AM
Last Post: woooee
  What's the full answer to this question in python? (bayesian decision theory) Hblaugrana 1 2,513 Oct-31-2018, 02:22 PM
Last Post: j.crater
  Cross-validation: evaluating estimator performance Grin 1 2,671 Jun-29-2018, 05:15 AM
Last Post: scidam

Forum Jump:

User Panel Messages

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