-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path203_Classification_Tree_Basic.py
More file actions
127 lines (38 loc) · 1.37 KB
/
203_Classification_Tree_Basic.py
File metadata and controls
127 lines (38 loc) · 1.37 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Jul 30 09:32:54 2025
@author: kingsleylee
"""
# Classification Tree - Basic Template
# Import required Python packages
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
import pandas as pd
# Import sample data
my_df = pd.read_csv("data/sample_data_classification.csv")
# Split data into input and output objects
X = my_df.drop(["output"], axis = 1)
y = my_df["output"]
# Split data into training and text sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 42, stratify = y)
# Instantiate our model object
clf = DecisionTreeClassifier(random_state = 42, min_samples_leaf = 7)
# Train our model
clf.fit(X_train, y_train)
# Assess model accuracy
y_pred = clf.predict(X_test)
accuracy_score(y_test, y_pred)
# A Demonstration of Overfitting
y_pred_training = clf.predict(X_train)
accuracy_score(y_train, y_pred_training)
# Plot our Decision Tree
import matplotlib.pyplot as plt
from sklearn.tree import plot_tree
plt.figure(figsize = (25, 15))
tree = plot_tree(clf,
feature_names = list(X.columns),
filled = True,
rounded = True,
fontsize = 24)