Python Forum
Using ID3 Estimator for Decision Trees - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Using ID3 Estimator for Decision Trees (/thread-40165.html)



Using ID3 Estimator for Decision Trees - student6306 - Jun-12-2023

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



RE: Using ID3 Estimator for Decision Trees - deanhystad - Jun-13-2023

You might get some help if your question was more specific.


RE: Using ID3 Estimator for Decision Trees - student6306 - Jun-15-2023

(Jun-13-2023, 01:52 PM)deanhystad Wrote: You might get some help if your question was more specific.

It was specific enough.