0% found this document useful (0 votes)
9 views2 pages

Write A Python Program To Perform Multiclass Classification On Iris Dataset

The document outlines a Python program for multiclass classification using the Iris dataset. It includes steps for loading the dataset, splitting it into training and testing sets, scaling the data, training a Logistic Regression model, and evaluating its accuracy. Additionally, it allows user input for predicting the class of a new Iris flower sample based on its features.

Uploaded by

rasak98391
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)
9 views2 pages

Write A Python Program To Perform Multiclass Classification On Iris Dataset

The document outlines a Python program for multiclass classification using the Iris dataset. It includes steps for loading the dataset, splitting it into training and testing sets, scaling the data, training a Logistic Regression model, and evaluating its accuracy. Additionally, it allows user input for predicting the class of a new Iris flower sample based on its features.

Uploaded by

rasak98391
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/ 2

ENROLLMENT NO: 202203103510400

PRACTICAL 4
AIM: - Write a python program to perform multiclass classification on iris dataset.

CODE: -
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, classification_report
from sklearn.preprocessing import StandardScaler

# Load the Iris dataset


iris = load_iris()
X, Y = iris.data, iris.target

# Split data into training and testing sets


X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.3, random_state=0)

# Scale the data


scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)

# Train a Logistic Regression model


model = LogisticRegression(multi_class='multinomial', solver='lbfgs') # Use multinomial for
multiclass
model.fit(X_train_scaled, Y_train)

# Make predictions
Y_pred = model.predict(X_test_scaled)

UTU/CGPIT/CE/SEM-6/MACHINE INTELLIGENCE[CE5008]
ENROLLMENT NO: 202203103510400

# Evaluate the model


accuracy = accuracy_score(Y_test, Y_pred)
print(f"Accuracy: {accuracy}")
print(classification_report(Y_test, Y_pred))

# Get test sample


print("\nEnter the following features for the Iris flower (in cm):")
sepal_length = float(input("Sepal length: "))
sepal_width = float(input("Sepal width: "))
petal_length = float(input("Petal length: "))
petal_width = float(input("Petal width: "))

# Create the sample array from user input


user_sample = [[sepal_length, sepal_width, petal_length, petal_width]]

# Scale the input sample


user_sample_scaled = scaler.transform(user_sample)
predicted_class = model.predict(user_sample_scaled)
print(f"\nPredicted class for the entered sample: {iris.target_names[predicted_class][0]}")

OUTPUT

UTU/CGPIT/CE/SEM-6/MACHINE INTELLIGENCE[CE5008]

You might also like