def vote(grid_file): trees = {} x_p, y_p, x_n, y_n = [], [], [], [] for j in range(1,102): trees[outname(j)] = dt.TDIDT(outname(j)) examples = dt.examplesFromFile(grid_file) for true_label in examples: for example in examples[true_label]: classifications = defaultdict(int) for j in range(1,102): label = trees[outname(j)].classify(example) classifications[label] += 1 maximum = -sys.maxint-1 for label in classifications: if classifications[label] > maximum: maximum = classifications[label] prediction = label if prediction == 1: x_p.append(example[0]) y_p.append(example[1]) else: x_n.append(example[0]) y_n.append(example[1]) x_p = np.array(x_p) y_p = np.array(y_p) x_n = np.array(x_n) y_n = np.array(y_n) matlab.plot(x_n,y_n,'go') matlab.plot(x_p,y_p,'ro') matlab.show()
def main(data_file="circle.train", grid_file="grid", show=True): tdidt = dt.TDIDT(data_file) examples = dt.examplesFromFile(grid_file) x_p, y_p, x_n, y_n = [], [], [], [] for label in examples: for example in examples[label]: prediction = tdidt.classify(example) if prediction == 1: x_p.append(example[0]) y_p.append(example[1]) else: x_n.append(example[0]) y_n.append(example[1]) x_p = np.array(x_p) y_p = np.array(y_p) x_n = np.array(x_n) y_n = np.array(y_n) matlab.plot(x_n,y_n,'go') matlab.plot(x_p,y_p,'ro') if show: matlab.show() else: pylab.savefig(data_file, bbox_inches=0)