-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecision_tree.py
More file actions
93 lines (76 loc) · 2.66 KB
/
Copy pathdecision_tree.py
File metadata and controls
93 lines (76 loc) · 2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# -*- coding: utf-8 -*-
"""Decision Tree.ipynb
Automatically generated by Colab.
Original file is located at
https://colab.research.google.com/drive/1Ho399oSWvvbOABhO33zJsVsRXP79rzxg
"""
# CODTECH Internship - TASK 1
# Decision Tree Classifier with Visualization and Evaluation
# Dataset: Iris (built-in from sklearn)
# ===============================================================
# Step 1: Import Required Libraries
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.datasets import load_iris
from sklearn.tree import DecisionTreeClassifier, plot_tree
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, classification_report, confusion_matrix
# Step 2: Load and Explore the Dataset
iris = load_iris()
X = pd.DataFrame(iris.data, columns=iris.feature_names)
y = pd.Series(iris.target, name="species")
print("✅ Feature sample:")
print(X.head())
print("\n✅ Target classes:", iris.target_names)
print("\n✅ Dataset shape:", X.shape)
# Step 3: Check for Null Values
print("\n🔍 Null value check:")
print(X.isnull().sum())
# Step 4: Train-Test Split
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.3, random_state=42, stratify=y
)
print("\n✅ Data split:")
print("Training samples:", len(X_train))
print("Testing samples:", len(X_test))
# Step 5: Model Initialization and Training
clf = DecisionTreeClassifier(criterion="entropy", max_depth=4, random_state=42)
clf.fit(X_train, y_train)
# Step 6: Prediction
y_pred = clf.predict(X_test)
# Step 7: Model Evaluation
acc = accuracy_score(y_test, y_pred)
print(f"\n🎯 Accuracy of the model: {acc:.2f}")
print("\n📊 Classification Report:")
print(classification_report(y_test, y_pred, target_names=iris.target_names))
print("📉 Confusion Matrix:")
cm = confusion_matrix(y_test, y_pred)
sns.heatmap(cm, annot=True, cmap="Blues", xticklabels=iris.target_names, yticklabels=iris.target_names)
plt.title("Confusion Matrix")
plt.xlabel("Predicted")
plt.ylabel("Actual")
plt.show()
# Step 8: Visualize the Decision Tree
plt.figure(figsize=(16, 10))
plot_tree(
clf,
feature_names=iris.feature_names,
class_names=iris.target_names,
filled=True,
rounded=True,
fontsize=12
)
plt.title("🌳 Decision Tree Visualization")
plt.show()
# Step 9: Feature Importance
feature_importance = pd.Series(clf.feature_importances_, index=X.columns).sort_values(ascending=False)
print("\n🔥 Feature Importances:")
print(feature_importance)
# Plot Feature Importance
feature_importance.plot(kind="bar", color="green")
plt.title("Feature Importance")
plt.ylabel("Importance Score")
plt.xlabel("Features")
plt.show()