<jupyter_start><jupyter_text>1.Best Tree<jupyter_code>best_tree, average_accuracy, plot_x, plot_y = return_best_tree(df)
update_decision_count(best_tree)
print("")
print("Best Depth = " + str(tree_depth(best_tree))+"," + "Best Accuracy = "+ str(predict_accuracy(best_tree,training_data)))
print(plot_x, plot_y)
def plot_graph(plot_x, plot_y):
    """
    Here Plot of Depth Vs Accuracy 
    """
    plot = []
    for i in range(len(plot_x)):
        plot.append((plot_x[i],plot_y[i])) 
    plot.sort()
    
    for i in range(len(plot)):
        plot_x[i] = plot[i][0]
        plot_y[i] = plot[i][1]
    
    plt.plot(plot_x,plot_y)
    plt.xlabel('Depth of the Tree')
    plt.ylabel('Achieved Accuracy')
    plt.title('Depth Vs Accuracy')
    plt.show()
plot_graph(plot_x,plot_y)
print_tree(best_tree)<jupyter_output>Is marital_status ==  Married-civ-spouse ?
--> True
 Is capital_gain >= 5178 ?
 --> True
  Is occupation ==  Farming-fishing ?
  --> True
   Is fnlwgt >= 214169 ?
   --> True
    Predict {' >50K': 1}
   --> False
    Predict {' <=50K': 2}
  --> False
   Is age >= 71 ?
   --> True
    Is hours_per_week >= 40 ?
    --> True
     Predict {' >50K': 1}
    --> False
     Predict {' <=50K': 1}
   --> False
    Is fnlwgt >= 82521 ?
    --> True
     Predict {' >50K': 125}
    --> False
     Is occupation ==  Craft-repair ?
     --> True
      Predict {' <=50K': 1}
     --> False
      Predict {' >50K': 9}
 --> False
  Is education_num >= 12 ?
  --> True
   Is capital_loss >= 1848 ?
   --> True
    Predict {' >50K': 46}
   --> False
    Is occupation ==  Exec-managerial ?
    --> True
     Is hours_per_week >= 25 ?
     --> True
      Is capital_gain >= 5013 ?
      --> True
       Predict {' <=50K': 2}
      --> False
       Is age >= 48 ?
       --> True
        Is age >= 90 ?
        --> Tr[...]<jupyter_text>2. Best Height Possible over 20 possible Depth and max_depth Input taken from the user<jupyter_code>def return_best_depth(df):
    
    best_tree = None
    best_depth = 0.0
    sum_of_accuracy = 0.0
    best_accuracy = 0.0
    
    plot_x = []
    plot_y = []
    infinity = 100; ### edit for more than hundread if you want
    
    for i in range(20):
        
        """
        Keep taking input unless maximum accuracy achived
        """
        max_depth = int(input("Give a max_depth:"))
#         max_depth = -1;
        
#         train, test = split_data_random(df)
        train, test = split_data_random(df[:4000]) # uncomment to use for lesser data as 40K of instances will take huge time 
        training_data = newList_from_df(train)
        testing_data = newList_from_df(test)
        
        if max_depth == -1:
            max_depth = infinity
            
        my_tree_ = build_tree_max_depth(training_data,1, max_depth)
        
        new_accuracy = predict_accuracy(my_tree_,testing_data)
                
        sum_of_accuracy += new_accuracy
        
        plot_x.append(tree_depth(my_tree_))
        plot_y.append(new_accuracy)
        
        if new_accuracy > best_accuracy:
            best_accuracy = new_accuracy
            best_tree = my_tree_
            
        
    return tree_depth(best_tree), plot_x,plot_y
best_depth, depth_x, acc_y = return_best_depth(df)
print("Best Depth Obtained:"+ str(best_depth))
plot_graph(depth_x, acc_y)
def pruning_result(tree,training_data,validation_data):
    
    if len(training_data) == 0 or len(validation_data)==0:
        return tree
    acc_dT = predict_accuracy(tree, validation_data)
    new_Leaf = Leaf(training_data)
    acc_leaf = predict_accuracy(new_Leaf,validation_data)
    
    if acc_leaf >= acc_dT :
        return new_Leaf
    else:
        return tree
    
def post_pruning(tree,training_data,validation_data):
    
    true_branch, false_branch = tree.true_branch, tree.false_branch
    
    if isinstance(true_branch,Leaf) and isinstance(false_branch,Leaf):
        return pruning_result(tree, training_data, validation_data)
    
    else:
        
        train_true, train_false = partition(training_data, tree.question)
        valid_true, valid_false = partition(validation_data, tree.question)
        
        if not isinstance(true_branch, Leaf):
            
            tree.true_branch = post_pruning(true_branch, train_true, valid_true)
        
        if not isinstance(false_branch, Leaf):
            
            tree.false_branch = post_pruning(false_branch, train_false, valid_false)

        return pruning_result(tree, training_data, validation_data)<jupyter_output><empty_output><jupyter_text>Pruning Operation to be performed by creating a tree of best_depth obtained from part 2<jupyter_code>trainP,valP= split_data_random(df[4000:8000])
trainingP_data, validationP_data = newList_from_df(trainP), newList_from_df(valP)
tree_to_be_prunned = build_tree_max_depth(trainingP_data,1,best_depth)

print("accuracy_on_training_data_before_prunning: "+ str(predict_accuracy(tree_to_be_prunned,trainingP_data)))
tree_prunned = post_pruning(tree_to_be_Prunned, trainingP_data, validationP_data)
print("accuracy_on_training_data_after_prunning: "+ str(predict_accuracy(tree_prunned,trainingP_data)))
print_tree(tree_pruned) ## printing prunned tree<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10943468296409473, 'fraction_numerical': 0.019671504965622612, 'mean_word_length': 2.80319535221496, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 54, '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': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '192886', 'license': 'MIT', 'n_tokens_mistral': 1857, 'n_tokens_neox': 1761, 'n_words': 358, 'path': 'ML CS60050/Assignment/Group_56_ML_A1/assignment_1.ipynb', 'repo_name': 'avi-dak-hyd/Machine-Learning', 'size': '5236'}