0% found this document useful (0 votes)
8 views

Experiment 6 Code

Experiment 6 code

Uploaded by

maasalamix
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Experiment 6 Code

Experiment 6 code

Uploaded by

maasalamix
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Code:

# Import necessary libraries


from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import classification_report, confusion_matrix, accuracy_score

# Load the Iris dataset


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

# Split the dataset into a 70-30 ratio for training and testing
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3,
random_state=42)

# Initialize and train the Logistic Regression model


model = LogisticRegression(max_iter=200)
model.fit(X_train, y_train)

# Make predictions on the test set


y_pred = model.predict(X_test)

# Evaluate the model's performance


accuracy = accuracy_score(y_test, y_pred)
conf_matrix = confusion_matrix(y_test, y_pred)
report = classification_report(y_test, y_pred, target_names=iris.target_names)

# Print the results


print("Accuracy: {:.2f}%".format(accuracy * 100))
print("\nConfusion Matrix:\n", conf_matrix)
print("\nClassification Report:\n", report)

import matplotlib.pyplot as plt


import seaborn as sns

# Class labels for Iris dataset


class_names = iris.target_names

# Plot the confusion matrix


plt.figure(figsize=(8, 6))
sns.heatmap(conf_matrix, annot=True, fmt="d", cmap="Blues",
xticklabels=class_names, yticklabels=class_names)
plt.title("Confusion Matrix")
plt.xlabel("Predicted Label")
plt.ylabel("True Label")
plt.show()
Output:
Accuracy: 100.00%

Confusion Matrix:
[[19 0 0]
[ 0 13 0]
[ 0 0 13]]

Classification Report:
precision recall f1-score support

setosa 1.00 1.00 1.00 19


versicolor 1.00 1.00 1.00 13
virginica 1.00 1.00 1.00 13
accuracy 1.00 45
macro avg 1.00 1.00 1.00 45
weighted avg 1.00 1.00 1.00 45

You might also like