from (‘%s[X%d < %.3f]' %((node_depth*'', (node['index']+1), node['value']))) print_tree(node['left'],

Published by admin on

from csv import readerfrom random import randrangefrom random import seeddef impurity_measure(groups, classes): n_instances = float(sum(len(group) for group in groups)) impurity = 0.0 for group in groups: size = float(len(group)) if size == 0: continue score = 0.0 for class_val in classes: p = row-1 for row in group.count(class_val)/size score += p*p impurity += (1.0 – score)*(size/n_instances) return impuritydef split_data(index, value, csv_file): left, right = list(), list() for row in csv_file: if rowindex < value: left.append(row) else: right.append(row) return left, rightdef evaluate_split(csv_file): class_values = list(set(row-1 for row in csv_file)) b_index, b_value, b_score, b_groups = 999,999,999, None for index in range(len(csv_file0)-1): for row in csv_file: groups = split_data(index, rowindex, csv_file) impurity = impurity_measure(groups, class_values) if impurity < b_score: b_index, b_value, b_score, b_groups = index, rowindex, impurity, groups return {'index': b_index, 'value':b_value, 'groups':b_groups}def most_common_output(group): outcomes = row-1 for row in group return max(set(outcomes), key=outcomes.count)def split(node, tree_depth, min_rows_per_node, node_depth): left, right = node'groups' del(node'groups') if not left or not right: node'left' = node'right' = most_common_output(left+right) return if node_depth >= tree_depth: node’left’, node’right’ = most_common_output(left), most_common_output(right) return if len(left)<= min_rows_per_node: node'left' = most_common_output(left) else: node'left' = evaluate_split(left) split(node'left', tree_depth, min_rows_per_node, node_depth+1) if len(right)<= min_rows_per_node: node'right' = most_common_output(right) else: node'right' = evaluate_split(right) split(node'right', tree_depth, min_rows_per_node, node_depth+1)def build_cart(train, tree_depth, min_rows_per_node): root = evaluate_split(train) split(root, tree_depth, min_rows_per_node, 1) return rootdef printout_cart_tree(node, node_depth=0): if isinstance(node, dict): print ('%sX%d < %.3f' %((node_depth*'', (node'index'+1), node'value'))) print_tree(node'left', node_depth+1) print_tree(node'right', node_depth+1) else: print(' %s%s' %((node_depth*'', node)))def predict(node, row): if rownode'index'

Categories: Articles

x

Hi!
I'm Iren!

Would you like to get a custom essay? How about receiving a customized one?

Check it out