def decision_tree_algorithm(df, counter=0, min_samples=2, max_depth=5, random_subspace=None): # data preparations if counter == 0: global COLUMN_HEADERS, FEATURE_TYPES COLUMN_HEADERS = df.columns FEATURE_TYPES = determine_type_of_feature(df) data = df.values else: data = df # base cases if (check_purity(data)) or (len(data) < min_samples) or (counter == max_depth): classification = classify_data(data) return classification # recursive part else: counter += 1 # helper functions potential_splits = get_potential_splits(data, random_subspace) split_column, split_value = determine_best_split(data, potential_splits) data_below, data_above = split_data(data, split_column, split_value) # check for empty data if len(data_below) == 0 or len(data_above) == 0: classification = classify_data(data) return classification # determine question feature_name = COLUMN_HEADERS[split_column] type_of_feature = FEATURE_TYPES[split_column] if type_of_feature == "continuous": question = "{} <= {}".format(feature_name, split_value) # feature is categorical else: question = "{} = {}".format(feature_name, split_value) # instantiate sub-tree sub_tree = {question: []} # find answers (recursion) yes_answer = decision_tree_algorithm(data_below, counter, min_samples, max_depth, random_subspace) no_answer = decision_tree_algorithm(data_above, counter, min_samples, max_depth, random_subspace) # If the answers are the same, then there is no point in asking the qestion. # This could happen when the data is classified even though it is not pure # yet (min_samples or max_depth base case). if yes_answer == no_answer: sub_tree = yes_answer else: sub_tree[question].append(yes_answer) sub_tree[question].append(no_answer) return sub_tree
def decision_tree_algorithm(df, counter=0, min_samples=2, max_depth=5, random_subspace=None): # data preparations if counter == 0: global COLUMN_HEADERS, FEATURE_TYPES COLUMN_HEADERS = df.columns FEATURE_TYPES = determine_type_of_feature(df) data = df.values else: data = df if (check_purity(data)) or (len(data) < min_samples) or (counter == max_depth): classification = classify_data(data) return classification else: counter += 1 potential_splits = get_potential_splits(data, random_subspace) split_column, split_value = determine_best_split(data, potential_splits) data_below, data_above = split_data(data, split_column, split_value) if len(data_below) == 0 or len(data_above) == 0: classification = classify_data(data) return classification feature_name = COLUMN_HEADERS[split_column] type_of_feature = FEATURE_TYPES[split_column] if type_of_feature == "continuous": question = "{} <= {}".format(feature_name, split_value) else: question = "{} = {}".format(feature_name, split_value) sub_tree = {question: []} yes_answer = decision_tree_algorithm(data_below, counter, min_samples, max_depth, random_subspace) no_answer = decision_tree_algorithm(data_above, counter, min_samples, max_depth, random_subspace) if yes_answer == no_answer: sub_tree = yes_answer else: sub_tree[question].append(yes_answer) sub_tree[question].append(no_answer) return sub_tree