def __init__(self): self.class_a_regression = TwoDimensionLogisticRegression() self.class_b_regression = TwoDimensionLogisticRegression()
class TriClassTwoDimensionLogisticRegression: def __init__(self): self.class_a_regression = TwoDimensionLogisticRegression() self.class_b_regression = TwoDimensionLogisticRegression() def train(self, example, classification, weight=1): if classification[0] == 1: self.class_a_regression.train(example, True, weight) self.class_b_regression.train(example, False, weight) elif classification[1] == 1: self.class_a_regression.train(example, False, weight) self.class_b_regression.train(example, False, weight) else: self.class_a_regression.train(example, False, weight) self.class_b_regression.train(example, True, weight) def predict(self, example): a_probability = self.class_a_regression.predict(example) b_probability = self.class_b_regression.predict(example) probability_sum = a_probability + b_probability if (probability_sum >= 1): c_probability = 0 a_probability = a_probability / probability_sum b_probability = b_probability / probability_sum else: c_probability = 1 - probability_sum return [a_probability, c_probability, b_probability]