-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6f5d347
commit c7b0a30
Showing
1 changed file
with
35 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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]]}") |