8

 import numpy as np 

import matplotlib.pyplot as plt 

from sklearn.datasets import load_breast_cancer 

from sklearn.model_selection import train_test_split 

from sklearn.tree import DecisionTreeClassifier, plot_tree 

from sklearn.metrics import accuracy_score 

# Load dataset 

data = load_breast_cancer() 

X, y = data.data, data.target 

# Train-test split 

X_train, X_test, y_train, y_test = train_test_split(X, y, 

test_size=0.2, random_state=42) 

# Train Decision Tree 

clf = DecisionTreeClassifier(random_state=42) 

clf.fit(X_train, y_train) 

# Accuracy 

y_pred = clf.predict(X_test) 

print(f"Accuracy: {accuracy_score(y_test, y_pred) * 100:.2f}%") 

# Predict one sample 

sample = X_test[0] 

predicted_label = clf.predict([sample])[0] 

print("Predicted Class:", "Benign" if predicted_label == 1 else 

"Malignant") 

# Plot tree 

plt.figure(figsize=(12, 8)) 

plot_tree(clf, filled=True, feature_names=data.feature_names, 

class_names=data.target_names) 

plt.title("Decision Tree - Breast Cancer") 

plt.show()

Comments

Popular posts from this blog

2

1

3