from sklearn.datasets import load_iris from sklearn.model_selection import train_test_split from sklearn.tree import DecisionTreeClassifier from sklearn.metrics import accuracy_score, confusion_matrix def prune_tree(tree, X, y): return tree iris = load_iris() X = iris.data y = iris.target X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) clf = DecisionTreeClassifier(random_state=42) clf.fit(X_train, y_train) pruned_tree = prune_tree(clf, X_train, y_train) y_train_pred = pruned_tree.predict(X_train) train_accuracy = accuracy_score(y_train, y_train_pred) train_conf_matrix = confusion_matrix(y_train, y_train_pred) y_test_pred = pruned_tree.predict(X_test) test_accuracy = accuracy_score(y_test, y_test_pred) test_conf_matrix = confusion_matrix(y_test, y_test_pred) print(f"Train Accuracy: {train_accuracy:.2f}") print("Train Confusion Matrix:") print(train_conf_matrix) print(f"\nTest Accuracy: {test_accuracy:.2f}") print("Test Confusion Matrix:") print(test_conf_matrix)