i did taking max class. How to change predict
method in Leaf
class from taking the maximum class to taking the sample mean?
prediction = max(classes, key=classes.get)
CodePudding user response:
I think this should make the current code testable. I've made some assumptions, I hope they're correct:
class Leaf:
def __init__(self, data, labels):
self.data = data
self.labels = labels
self.prediction = self.predict()
def predict(self):
classes = {}
for label in self.labels:
if label not in classes:
classes[label] = 0
classes[label] = 1
prediction = max(classes, key=classes.get)
return prediction
print(Leaf(None, ["a", "b", "b"]).prediction, "is the most common leaf")
Ouput:
b is the most common leaf
So, I guess you want to find the median leaf? I don't know for average leaf.
class Leaf:
def __init__(self, data, labels):
self.data = data
self.labels = labels
self.prediction = self.predict()
def predict(self):
classes = {}
for label in self.labels:
if label not in classes:
classes[label] = 0
classes[label] = 1
prediction = sorted(classes, key=classes.get)[len(classes) // 2]
return prediction
assert Leaf(None, ["a"]).prediction == "a"
assert Leaf(None, ["a", "a"]).prediction == "a"
assert Leaf(None, ["a", "b"]).prediction == "b"
assert Leaf(None, ["a", "a", "a"]).prediction == "a"
assert Leaf(None, ["a", "a", "b"]).prediction == "a"
assert Leaf(None, ["a", "b", "c"]).prediction == "b"
assert Leaf(None, ["a", "a", "a", "a"]).prediction == "a"
assert Leaf(None, ["a", "a", "a", "b"]).prediction == "a"
assert Leaf(None, ["a", "a", "b", "b"]).prediction == "b"
assert Leaf(None, ["a", "a", "b", "c"]).prediction == "c"
assert Leaf(None, ["a", "a", "a", "a", "a"]).prediction == "a"
assert Leaf(None, ["a", "a", "a", "a", "b"]).prediction == "a"
assert Leaf(None, ["a", "a", "a", "b", "b"]).prediction == "a"
assert Leaf(None, ["a", "a", "a", "b", "c"]).prediction == "c"
assert Leaf(None, ["a", "a", "b", "b", "b"]).prediction == "b"
assert Leaf(None, ["a", "a", "b", "b", "c"]).prediction == "a"
assert Leaf(None, ["a", "a", "a", "a", "a", "a"]).prediction == "a"
assert Leaf(None, ["a", "a", "a", "a", "a", "b"]).prediction == "a"
assert Leaf(None, ["a", "a", "a", "a", "b", "b"]).prediction == "a"
assert Leaf(None, ["a", "a", "a", "a", "b", "c"]).prediction == "c"
assert Leaf(None, ["a", "a", "a", "b", "b", "b"]).prediction == "b"
assert Leaf(None, ["a", "a", "a", "b", "b", "c"]).prediction == "b"
assert Leaf(None, ["a", "a", "b", "b", "c", "c"]).prediction == "b"