<jupyter_start><jupyter_text><jupyter_code>import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
df = pd.read_csv("elec.csv.txt")
df.head()
df.columns
selected_cols = ['0', '0.056443', '0.439155', '0.003467', '0.422915', '0.414912', '1']
data = df.loc[:, selected_cols]
data.head()
data.info()
X = data.loc[:, ['0', '0.056443', '0.439155', '0.003467', '0.422915', '0.414912']]
y = data['1']
def entropy(column):
    vals, counts = np.unique(column, return_counts=True)
    total = len(column)
    
    acc = 0
    for count in counts:
        acc += (count/total)*np.log2(count/total)

    return -acc
entropy(y)
def info_gain(X, y, label):
    
    pivot = np.mean(X[label])
    
    y_left = y[X[label] < pivot]
    y_right = y[X[label] >= pivot]
    
    if (len(y_left) == 0) or (len(y_right) == 0):
        return -1000
    
    left = (len(y_left)/len(y))*entropy(y_left)
    right = (len(y_right)/len(y))*entropy(y_right)
    
    return entropy(y) - left - right
for label in X.columns:
    print(label, info_gain(X, y, label))
class Node:
    def __init__(self, label=None, value=None, result=None):
        self.label = label
        self.value = value
        self.result = result
class DecisionTree:
    
    def __init__(self, max_depth=10):
        self.max_depth = max_depth
        
    def fit(self, X, y):
        self.root = self.generate(X, y, self.max_depth)
        
    def generate(self, X, y, depth):
        if depth == 1:
            return Node(result=np.mean(y))
        
        gains = []
        for label in X.columns:
            gain = info_gain(X, y, label)
            gains.append((gain, label))
  
        selected_label = max(gains)[1]
    
        pivot = np.mean(X[selected_label])
        left = (X[selected_label] < pivot)
        right = (X[selected_label] >= pivot)
        X_left, y_left = X[left], y[left]
        X_right, y_right = X[right], y[right]

        node = Node(selected_label, pivot)
        node.left = self.generate(X_left, y_left, depth-1)
        node.right = self.generate(X_right, y_right, depth-1)
        
        return node
    
    def display(self, node, indent=0):
        if node.label == None:
            if node.result < .5:
                print("\t"*indent, "UP :(")
            else:
                print("\t"*indent, "DOWN :)))))")
            return
        
        print(indent*"\t", node.label, node.value)
        self.display(node.left, indent+1)
        self.display(node.right, indent+1)
        
        
    def predict_point(self, row, node):
        if node.label == None:
            return node.result
        
        if row[node.label] < node.value:
            return self.predict_point(row, node.left)
        else:
            return self.predict_point(row, node.right)
        
    def predict(self, X):
        y = []
        for index, row in X.iterrows():
            res = int(self.predict_point(row, self.root) > .5)
            y.append(res)
        
        return np.array(y)
    
    def score(self, X, y):
        yp = self.predict(X)
        
        return np.sum(y.values == yp) / len(y)
X_train, X_test, y_train, y_test = train_test_split(
...     X, y, test_size=0.33, random_state=42)
model = DecisionTree()
model.fit(X_train, y_train)
model.score(X_test, y_test)
model.score(X_train, y_train)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14256795835743205, 'fraction_numerical': 0.02834008097165992, 'mean_word_length': 2.223671947809879, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 8, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '577960', 'license': 'Apache-2.0', 'n_tokens_mistral': 1272, 'n_tokens_neox': 1215, 'n_words': 267, 'path': 'Decision_tree.ipynb', 'repo_name': 'mihirsh73/Decision_tree', 'size': '3458'}