PRACTICAL 6: DECISION TREE

STEPS:
1. Install the libraries:  pip install pandas numpy scikit-learn matplotlib
2. Load diabetes.csv and convert Outcome into 0 / 1.
3. Separate the features X (all columns except Outcome) and the target y (Outcome).
4. Split the data into 80% training and 20% testing.
5. Train the DecisionTreeClassifier and predict on the test data.
6. Calculate accuracy, confusion matrix, precision, sensitivity, specificity and standard error.
7. Draw the ROC curve with the AUC value and plot the decision tree (max depth 3).

CODE:
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier, plot_tree
from sklearn.metrics import (accuracy_score, confusion_matrix, precision_score,
                             recall_score, roc_curve, auc)
import matplotlib.pyplot as plt
import numpy as np
filepath = "C:/Users/Admin/Downloads/diabetes.csv"
data = pd.read_csv(filepath)
data['Outcome'] = data['Outcome'].apply(lambda x: 1 if x >= 0.5 else 0)
X = data.drop(columns=['Outcome'])
y = data['Outcome']
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)
y_pred = clf.predict(X_test)
y_pred_prob = clf.predict_proba(X_test)[:, 1]
accuracy = accuracy_score(y_test, y_pred)
conf_matrix = confusion_matrix(y_test, y_pred)
precision = precision_score(y_test, y_pred)
sensitivity = recall_score(y_test, y_pred)
TN = conf_matrix[0, 0]
FP = conf_matrix[0, 1]
specificity = TN / (TN + FP)
standard_error = np.sqrt((accuracy * (1 - accuracy)) / len(y_test))
fpr, tpr, _ = roc_curve(y_test, y_pred_prob)
roc_auc = auc(fpr, tpr)
plt.figure()
plt.plot(fpr, tpr, color='darkorange', lw=2, label='ROC curve (area = %0.2f)' % roc_auc)
plt.plot([0, 1], [0, 1], color='navy', lw=2, linestyle='--')
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('Receiver Operating Characteristic')
plt.legend(loc="lower right")
plt.show()
plt.figure(figsize=(12, 8))
plot_tree(clf, feature_names=X.columns, class_names=['0', '1'], filled=True, max_depth=3)
plt.title("Decision Tree (Max Depth = 3)")
plt.show()
print("Predictions:", y_pred)
print("Accuracy:", accuracy)
print("Confusion Matrix:\n", conf_matrix)
print("Standard Error:", standard_error)
print("Precision:", precision)
print("Sensitivity (Recall):", sensitivity)
print("Specificity:", specificity)
print("AUC:", roc_auc)

EXPLANATION:
- The decision tree classifier learns a set of if-else conditions (for example Glucose <= 127.5) from the training data to predict diabetes (1) or no diabetes (0).
- In the tree plot each box shows the condition, gini (impurity, lower means purer), samples reaching the node, value (class counts) and class.
- Orange nodes predict "No Diabetes" and blue nodes predict "Diabetes".
- The confusion matrix gives precision, sensitivity (recall) and specificity of the model.
- The ROC curve and the AUC value show how well the model separates the two classes.
