from sklearn.model_selection import KFold from sklearn import tree def computeCVAccuracy(X,y,folds): accuracy=[] foldAcc=[] depth=[None, 2, 3, 4, 5, 6, 7] for i in depth: kf = KFold(n_splits=folds) kf.get_n_splits(X) print("depth: ", i) for train_index, test_index in kf.split(X): X_train3, X_test3, Y_train3, Y_test3 = train_test_split(X, Y, train_size = 80, test_size = 20) clf3 = DecisionTreeClassifier(criterion="entropy", max_depth = i) #, max_depth = 3 clf3 = clf3.fit(X_train3, Y_train3) score = clf3.score(X_test3, Y_test3) accuracy.append(score) #Building the decision tree classifiers dot_data3 = export_graphviz(clf3, out_file=None, feature_names = X.columns, filled=True, rounded=True, special_characters=True) graph = graphviz.Source(dot_data3, format="png") fig, ax = plt.subplots(figsize=(10, 10)) tree.plot_tree(clf3, fontsize=10) plt.show() #Print Accuracy print("Accuracy: ", np.mean(accuracy)) print("\n") foldAcc.append(np.mean(accuracy)) return(foldAcc) computeCVAccuracy(X, Y, 10)