0% found this document useful (0 votes)
37 views4 pages

22IZ023 Nikhil - Exercise 7 a_ Decision Trees

Uploaded by

nikhildeutsch03
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views4 pages

22IZ023 Nikhil - Exercise 7 a_ Decision Trees

Uploaded by

nikhildeutsch03
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Exercise-7a Decision Trees

Aim

To implement a Decision Tree Classifier for predicting the species of the Iris
flower based on its features using Python.

Logic Description

Decision Trees are a type of supervised learning algorithm used for classification
tasks. The algorithm splits the dataset into branches based on feature conditions,
forming a tree-like structure for decision-making.

Algorithm

1.​ Load the dataset using Pandas.


2.​ Define the feature set (sepal and petal dimensions) and target variable
(species).
3.​ Split the dataset into training (80%) and testing (20%) sets.
4.​ Train a Decision Tree Classifier using the training data.
5.​ Predict the species of a flower based on user-input values.
6.​ Ensure input constraints are met before making predictions.
7.​ Display the predicted species.

Package/Tools Description

SI.N Name of Description


O Package/Tool

1 Pandas Data manipulation and analysis

2 Scikit-learn Machine learning library for


classification
Source Code:-
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier

# Step 1: Load the dataset


filename = "iris.csv" # Ensure the file is in the same directory
iris_df = pd.read_csv(filename)

# Step 2: Define features (X) and target variable (y)


X = iris_df[['sepal_length', 'sepal_width', 'petal_length',
'petal_width']]
y = iris_df['species']

# Step 3: Split dataset into training and testing 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)

# Step 4: Train a Decision Tree Classifier


clf = DecisionTreeClassifier(random_state=42)
clf.fit(X_train, y_train)

# Step 5: Function to predict species based on user input


def predict_iris_species():
sepal_length = float(input("Enter sepal length: "))
sepal_width = float(input("Enter sepal width: "))
petal_length = float(input("Enter petal length: "))
petal_width = float(input("Enter petal width: "))

# Ensure input values are within the given constraints


if not (1.0 <= sepal_length <= 8.0 and 0.1 <= sepal_width <= 4.0 and
1.0 <= petal_length <= 7.0 and 0.1 <= petal_width <= 2.5):
print("Error: Input values are out of range.")
return

# Prepare input for prediction


new_flower = [[sepal_length, sepal_width, petal_length, petal_width]]
predicted_species = clf.predict(new_flower)[0]
print(f"Predicted species for the new flower: {predicted_species}")
# Step 6: Run prediction
predict_iris_species()

Output:-

Test Cases

Test Input Expected Output


Case

1 Sepal Length: 5.1, Sepal Width: 3.5, Petal Predicted species:


Length: 1.4, Petal Width: 0.2 Setosa

2 Sepal Length: 6.0, Sepal Width: 3.0, Petal Predicted species:


Length: 4.8, Petal Width: 1.8 Versicolor

3 Sepal Length: 7.2, Sepal Width: 3.6, Petal Predicted species:


Length: 6.1, Petal Width: 2.5 Virginica

4 Sepal Length: 9.0, Sepal Width: 3.2, Petal Error: Input values are
Length: 5.8, Petal Width: 2.0 out of range

Inferences

●​ Decision Trees effectively classify Iris flower species based on petal and
sepal measurements.
●​ Proper input validation ensures meaningful predictions.
●​ Model accuracy depends on dataset size and quality.
Result

A Decision Tree Classifier was successfully implemented for classifying Iris


flowers, demonstrating decision-making through a tree-based structure.

You might also like