-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathdecision_tree.py
More file actions
31 lines (23 loc) · 963 Bytes
/
decision_tree.py
File metadata and controls
31 lines (23 loc) · 963 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
27
28
29
30
31
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score
# Load dataset
iris = load_iris()
X = iris.data # Sepal and petal measurements
y = iris.target # Flower species (0, 1, 2)
# Split data into training and test sets (80% train, 20% test)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Create a Decision Tree Classifier
model = DecisionTreeClassifier()
# Train the model
model.fit(X_train, y_train)
# Make predictions
y_pred = model.predict(X_test)
# Evaluate model
accuracy = accuracy_score(y_test, y_pred)
print(f"Model Accuracy: {accuracy * 100:.2f}%")
# Try predicting a new sample
sample = [[5.1, 3.5, 1.4, 0.2]] # Example flower measurements
predicted_class = iris.target_names[model.predict(sample)[0]]
print(f"Predicted Flower Species: {predicted_class}")