-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathc45.py
More file actions
26 lines (19 loc) · 785 Bytes
/
c45.py
File metadata and controls
26 lines (19 loc) · 785 Bytes
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
from sklearn.tree import DecisionTreeClassifier
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
# Load dataset (Iris dataset for example)
data = datasets.load_iris()
X = data.data # Features
y = data.target # Target labels
# Split the dataset into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
# Initialize Decision Tree Classifier with 'entropy' (approximating C4.5)
clf = DecisionTreeClassifier(criterion="entropy", random_state=42)
# Train the model
clf.fit(X_train, y_train)
# Predict on test data
y_pred = clf.predict(X_test)
# Evaluate the model
accuracy = accuracy_score(y_test, y_pred)
print(f"Accuracy: {accuracy:.2f}")