<jupyter_start><jupyter_text>Implementação da Decision Tree<jupyter_code>import numpy as np
import pandas as pd
import math
df = pd.read_csv('tenis.csv')
df<jupyter_output><empty_output><jupyter_text>Entropia e Montagem da Arvore<jupyter_code>def uncertainty(df, given):
    return entropy(df, None) - entropy(df, given)
def entropy(df, given):
    result = 0
    label = df.columns[len(df.columns)-1]
    distinct_label_values = df[label].unique()
    if given == None:
        for value in distinct_label_values:
            prob = df[df[label] == value].shape[0]/df.shape[0]
            x = df[df[label] == value].shape[0]/df.shape[0]
            log2 = 0 if x == 0 else math.log(x, 2)
            result += -prob*log2
    else:
        distinct_values = [distinct_label_values, df[given].unique()]
        for info_label in distinct_values[0]:
            for info_given in distinct_values[1]:            
              prob = df[(df[label] == info_label) & (df[given] == info_given)].shape[0]/df.shape[0]
              x = df[(df[label] == info_label) & (df[given] == info_given)].shape[0]/df[df[given] == info_given].shape[0]
              log2 = 0 if x == 0 else math.log(x, 2)
              result += -prob*log2
    return result
def decision_tree(df):
    return ['root', decision_tree_recursive(df, None)]
def decision_tree_recursive(df, parent):
    df = df if parent is None else df.drop([parent], axis=1)
    lowest_uncertainty = 0
    next_child = None
    for attr in df.columns[0:len(df.columns)-1]:
        u = uncertainty(df, attr)
        if u > lowest_uncertainty:
            lowest_uncertainty = u
            next_child = attr
    if lowest_uncertainty == 0:
        return df.iloc[0,len(df.columns)-1]
    child_values = df[next_child].unique()
    return [next_child, list(map(lambda c: [c, decision_tree_recursive(df[df[next_child] == c], next_child)], child_values))]<jupyter_output><empty_output><jupyter_text>Imprimir arvore<jupyter_code>dec_test = decision_tree(df)<jupyter_output><empty_output><jupyter_text>Formato:Dados de Array dentro de Array<jupyter_code>dec_test<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13010803193987788, 'fraction_numerical': 0.013621418506341005, 'mean_word_length': 3.4190871369294604, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '614844', 'license': 'MIT', 'n_tokens_mistral': 753, 'n_tokens_neox': 732, 'n_words': 153, 'path': 'Decision Tree.ipynb', 'repo_name': 'syslogg/classifier-algorithms', 'size': '2129'}