cart-elc

Source code for CART-ELC
git clone git://git.laack.co/cart-elc.git
Log | Files | Refs | README | LICENSE

brightAll.py (2775B)


      1 import numpy as np
      2 import pandas as pd
      3 import decision_tree
      4 from sklearn.model_selection import KFold
      5 from sklearn.metrics import accuracy_score
      6 
      7 df = pd.read_csv('../../datasets/bright.csv')
      8 y = df[df.columns[-1]]
      9 df = df.drop(df.columns[-1], axis=1)
     10 X = df.to_numpy(dtype=np.float64)
     11 
     12 # Define hyperparameters
     13 depths = [1,2,3,4,5]
     14 criteria = ["gini", "twoing", "information gain"]
     15 lcs = [1,2]
     16 n_splits = 5
     17 n_trials = 10
     18 seed = 71
     19 
     20 # Store results
     21 accuracies = {lc: {depth: [] for depth in depths} for lc in lcs}
     22 treeSizes = {lc: {depth: [] for depth in depths} for lc in lcs}
     23 foldSizes = []
     24 foldAccuracies = []
     25 
     26 # Store results
     27 results = {criterion: {lc: {depth: [] for depth in depths} for lc in lcs} for criterion in criteria}
     28 tree_sizes = {criterion: {lc: {depth: [] for depth in depths} for lc in lcs} for criterion in criteria}
     29 
     30 for trial in range(n_trials):
     31     kf = KFold(n_splits=n_splits, shuffle=True, random_state=(trial + seed))
     32     
     33     for criterion in criteria:
     34         for lc in lcs:
     35             for depth in depths:
     36                 fold_accuracies = []
     37                 fold_sizes = []
     38                 
     39                 for train_index, test_index in kf.split(X):
     40                     X_train, X_test = X[train_index], X[test_index]
     41                     y_train, y_test = y[train_index], y[test_index]
     42                     
     43                     clf = decision_tree.ELCClassifier(depth, lc, 100, criterion)
     44                     clf.fit(X_train.ravel(), y_train.shape[0], y_train, int(X_train.size / y_train.shape[0]))
     45                     preds = clf.predict(X_test.ravel(), y_test.shape[0], int(X_test.size / y_test.shape[0]))
     46                     
     47                     fold_accuracies.append(100 * accuracy_score(y_pred=preds, y_true=y_test))
     48                     fold_sizes.append(clf.getSplits() + 1) # leaves not splits
     49                 
     50                 results[criterion][lc][depth].append(np.mean(fold_accuracies))
     51                 tree_sizes[criterion][lc][depth].append(np.mean(fold_sizes))
     52                                                                                  
     53 with open("resultsBright.txt", "w") as f:
     54     f.write("Results:\n")
     55     for criterion in criteria:
     56         f.write("\n")
     57         f.write(f"{criterion}:\n")
     58         for lc in lcs:
     59             for depth in depths:
     60                 avg_accuracy = np.mean(results[criterion][lc][depth])
     61                 std_accuracy = np.std(results[criterion][lc][depth])
     62                 avg_size = np.mean(tree_sizes[criterion][lc][depth])
     63                 std_size = np.std(tree_sizes[criterion][lc][depth])
     64 
     65                 f.write(f"LCs: {lc}, Depth: {depth} Avg Accuracy: {avg_accuracy:.1f} (Std: {std_accuracy:.1f})")
     66                 f.write(f", Avg # of Leaves: {avg_size:.1f} (Std: {std_size:.1f})\n")