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
from sklearn.metrics import classification_report, confusion_matrixLogistic Regression Classifiction on the Iris Dataset
This notebook demonstrates how to use Logistic Regression to classify iris flowers into three species. The dataset contains 150 samples with 4 features measuring the dimensions of iris flowers.
Dataset Information: - 150 samples total (50 per species) - 4 features: sepal length, sepal width, petal length, petal width - 3 target classes: Setosa, Versicolor, Virginica
Target Classes: - 0 = Iris Setosa - 1 = Iris Versicolor - 2 = Iris Virginica
Import Modules
Load the Iris Dataset
# Load the dataset from sklearn datasets
iris = load_iris()# Print data shape (150 samples, 4 features)
iris.data.shape(150, 4)
# Print the names of the 4 features
print(iris.feature_names)['sepal length (cm)', 'sepal width (cm)', 'petal length (cm)', 'petal width (cm)']
# Print the categories of Iris flowers
# 0 is Iris Setosa, 1 is Iris Versicolor, 2 is Iris Virginica
print(iris.target)[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
2 2]
Prepare Training and Testing Datasets
# Define features (X) and targets (y) variables
X, y = iris.data, iris.target# Split the dataset into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=123, test_size=0.25, stratify=y)# Print the shapes of data and label sets
print('Training data features', X_train.shape)
print('Training labels', y_train.shape)
print('Testing data features', X_test.shape)
print('Testing labels', y_test.shape)Training data features (112, 4)
Training labels (112,)
Testing data features (38, 4)
Testing labels (38,)
Train a Logistic Regression Model
# Create an instance of a Logistic Regression model
model = LogisticRegression(max_iter=1000)# Fit the model to the training dataset
model.fit(X_train, y_train)LogisticRegression(max_iter=1000)In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
LogisticRegression(max_iter=1000)
Evaluate the Model
# Predict on test dataset
logreg_predictions = model.predict(X_test)# Calculate and print the accuracy
accuracy = accuracy_score(y_test, logreg_predictions)
print(f"Accuracy on test dataset: {accuracy*100:5.2f}%")Accuracy on test dataset: 97.37%
# Print confusion matrix
print("Confusion Matrix:")
print(confusion_matrix(y_test, logreg_predictions))Confusion Matrix:
[[12 0 0]
[ 0 12 1]
[ 0 0 13]]
The confusion matrix shows how well the model predicted each species: - Rows: Actual species - Columns: Predicted species - Diagonal values: Correct predictions - Off-diagonal values: Misclassifications
# Print classification report
print("Classification report:")
print(classification_report(y_test, logreg_predictions, target_names=iris.target_names))Classification report:
precision recall f1-score support
setosa 1.00 1.00 1.00 12
versicolor 1.00 0.92 0.96 13
virginica 0.93 1.00 0.96 13
accuracy 0.97 38
macro avg 0.98 0.97 0.97 38
weighted avg 0.98 0.97 0.97 38
The report provides detailed metrics: - Precision: Of all predicted positives, how many were correct? - Recall: Of all actual positives, how many were detected? - F1-Score: Harmonic mean of precision and recall.