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

Logistic Regression

The document outlines a Python script that uses logistic regression to classify the Iris dataset. It includes data preprocessing steps such as label encoding, scaling, and splitting the dataset into training and testing sets. Finally, it evaluates the model's performance using accuracy score, classification report, and a confusion matrix visualized with a heatmap.

Uploaded by

241ce054
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)
12 views2 pages

Logistic Regression

The document outlines a Python script that uses logistic regression to classify the Iris dataset. It includes data preprocessing steps such as label encoding, scaling, and splitting the dataset into training and testing sets. Finally, it evaluates the model's performance using accuracy score, classification report, and a confusion matrix visualized with a heatmap.

Uploaded by

241ce054
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

import pandas as pd

import matplotlib.pyplot as plt

from sklearn.linear_model import LogisticRegression

from sklearn.model_selection import train_test_split

from sklearn.preprocessing import LabelEncoder

from sklearn.metrics import classification_report, confusion_matrix, accuracy_score

from sklearn.preprocessing import StandardScaler

import seaborn as sns

df = pd.read_csv(r"C:\Users\Swathi Krishna\Downloads\iris (1).csv")

print(df.head())

print(df.columns)

print(df.isnull().sum())

X = df.drop('species', axis=1)

y = df['species']

le = LabelEncoder()

y_encoded = le.fit_transform(y)

X_train, X_test, y_train, y_test = train_test_split(X, y_encoded, test_size=0.2, random_state=42)

scaler = StandardScaler()

X_scaled = scaler.fit_transform(X_train)

X_tscaled= scaler.transform(X_test)

model = LogisticRegression()

model.fit(X_scaled, y_train)

y_pred = model.predict(X_tscaled)

print("Accuracy:", accuracy_score(y_test, y_pred))

print("Classification Report:\n", classification_report(y_test, y_pred))

cm = confusion_matrix(y_test, y_pred)

sns.heatmap(cm, annot=True)

plt.xlabel('Predicted')

plt.ylabel('Actual')

plt.title('Confusion Matrix')

plt.show()
OUTPUT:

You might also like