8. Develop a program to demonstrate the working of the decision tree algorithm. Use Breast Cancer Data set for building the decision tree and apply this knowledge to classify a new sample.
import numpy as np
import matplotlib.pyplot as plt
from sklearn.tree import DecisionTreeClassifier, plot_tree
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
import matplotlib.pyplot as plt
data = load_breast_cancer()
X_train, X_test, y_train, y_test = train_test_split(data.data, data.target, test_size=0.2, random_state=42)
model = DecisionTreeClassifier(max_depth=4, random_state=42)
model.fit(X_train, y_train)
print(f"Accuracy: {accuracy_score(y_test, model.predict(X_test)):.2f}")
plot_tree(model, filled=True, feature_names=data.feature_names, class_names=data.target_names)
plt.show()
sample = [[15.3, 20.5, 85.2, 521, 0.1, 0.1, 0.08, 0.18, 0.19, 0.06, 0.32, 0.43, 2.5, 20.3, 0.007, 0.018, 0.016, 0.003, 0.015, 0.002, 17.5, 25.0, 110.5, 900, 0.14, 0.25, 0.18, 0.25, 0.27, 0.09]]
print("Predicted Class:", data.target_names[model.predict(sample)[0]])
Accuracy: 0.95
Predicted Class: malignant