Implement a decision tree classifier You are an incredibly talented gardner, but
ID: 3815892 • Letter: I
Question
Implement a decision tree classifier
You are an incredibly talented gardner, but you really need to work on your ability to identify different plants. Three flowers in your garden look incredibly similar: Iris-setosa, Iris-virginica, and Iris-versicolor. They look so similar that you decide to use your computer science knowledge to write a classifier that tells you which flower is which. You will be implementing a decision tree classifier for flower classification data.
The dataset required (name Iris.data) is here:
https://pastebin.com/raw/Km7cHCR2
The first four values in each line of this dataset are the values of each of the following four attributes repectively: sepal length, sepal width, petal length, petal width. The last value is the class label: "Iris-setosa", "Iris-viriginica", or "Iris-versicolor."
--------------
Using the provided starter code, implement a decision tree classifier. You should implement the train() function before the classify() function. These two functions are required as written in order to test your code. You are free to implement a decision tree however you wish, as long as you write the algorithm from scratch. Do not use premade packages such as scikit-learn.
I've pasted the starter code here (already formatted).
https://pastebin.com/raw/4bgdnJgW
Explanation / Answer
predict(node, row):
if row[node['index']] < node['value']:
if isinstance(node['left'], dict):
return predict(node['left'], row)
else:
return node['left']
else:
if isinstance(node['right'], dict):
return predict(node['right'], row)
else:
return node['right']
dataset = [[2.771,1.784,0],
[1.728,1.1697,0],
[3.678,2.812,0],
[3.9610,2.61995,0],
[2.999,2.20901,0],
[7.497,3.16295,1],
# predict with a stump
stump = {'index': 0, 'right': 1, 'value': 6.642287351, 'left': 0}
for row in dataset:
prediction = predict(stump, row)
print('Expected=%d, Got=%d' % (row[-1], prediction))
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.