-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMl-Classification-Model.py
More file actions
35 lines (27 loc) · 1.15 KB
/
Ml-Classification-Model.py
File metadata and controls
35 lines (27 loc) · 1.15 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
import numpy as np
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, classification_report
# Load the Iris dataset
iris = load_iris()
X = iris.data # Features (sepal and petal measurements)
y = iris.target # Target (species)
# Split the dataset into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Initialize the Decision Tree Classifier
classifier = DecisionTreeClassifier(random_state=42)
# Train the model
classifier.fit(X_train, y_train)
# Make predictions
y_pred = classifier.predict(X_test)
# Evaluate the model
accuracy = accuracy_score(y_test, y_pred)
print(f"Accuracy: {accuracy:.2f}")
# Classification report
print("\nClassification Report:")
print(classification_report(y_test, y_pred, target_names=iris.target_names))
# Optional: Predict a new sample
new_sample = np.array([[5.0, 3.5, 1.3, 0.3]]) # Example flower measurements
prediction = classifier.predict(new_sample)
print(f"\nPredicted class for new sample: {iris.target_names[prediction[0]]}")