cart-elc

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

irisAll.py (2802B)


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