"""Supervised Learning Algorithms - Logistic Regression (Univariant):
Implement logistic
regression and test it using any dataset. Give new test data and
predict the classification
output. Print the confusion matrix, accuracy, precision, recall, MSE ,
RMSE etc. Analyze
and write the inference."""
'Supervised Learning Algorithms - Logistic Regression (Univariant):
Implement logistic\nregression and test it using any dataset. Give new
test data and predict the classification\noutput. Print the confusion
matrix, accuracy, precision, recall, MSE , RMSE etc. Analyze\nand
write the inference.'
import numpy as np
import pandas as pd
from [Link] import load_iris
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from [Link] import confusion_matrix, accuracy_score,
precision_score, recall_score, mean_squared_error
from [Link] import StandardScaler
import [Link] as plt
# Load the Iris dataset
iris = load_iris()
X = [Link][:, :1] # Only one feature (first column)
y = [Link] # Target labels
# Binary classification: Let's filter for class 0 and class 1
(versicolor and setosa)
X = X[y != 2]
y = y[y != 2]
# Split the data into training and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y,
test_size=0.2, random_state=42)
# Standardize the feature data
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = [Link](X_test)
# Initialize and train the logistic regression model
model = LogisticRegression()
[Link](X_train, y_train)
LogisticRegression()
# Predict using the trained model
y_pred = [Link](X_test)
# Confusion Matrix
conf_matrix = confusion_matrix(y_test, y_pred)
# Accuracy
accuracy = accuracy_score(y_test, y_pred)
# Precision
precision = precision_score(y_test, y_pred)
# Recall
recall = recall_score(y_test, y_pred)
# Mean Squared Error (MSE)
mse = mean_squared_error(y_test, y_pred)
# Root Mean Squared Error (RMSE)
rmse = [Link](mse)
# Print the metrics
print(f"Confusion Matrix:\n{conf_matrix}")
print(f"Accuracy: {accuracy:.4f}")
print(f"Precision: {precision:.4f}")
print(f"Recall: {recall:.4f}")
print(f"MSE: {mse:.4f}")
print(f"RMSE: {rmse:.4f}")
Confusion Matrix:
[[8 4]
[0 8]]
Accuracy: 0.8000
Precision: 0.6667
Recall: 1.0000
MSE: 0.2000
RMSE: 0.4472
# New test data
new_data = [Link]([[0.5]]) # Sample new data point
new_data_scaled = [Link](new_data)
# Predict the class for the new data point
new_pred = [Link](new_data_scaled)
print(f"Predicted class for the new data: {new_pred[0]}")
Predicted class for the new data: 0
# Plotting decision boundary for univariate logistic regression
x_min, x_max = X_train.min() - 1, X_train.max() + 1
xx = [Link](x_min, x_max, 0.01).reshape(-1, 1) # Only use one
feature (1D grid)
# Predict the class probabilities (use the logistic model's prediction
probabilities)
probs = model.predict_proba(xx)[:, 1] # Get probability for the
positive class (class 1)
# Plot the decision boundary: class 1 if probability > 0.5, class 0 if
probability < 0.5
[Link](xx, probs, label="Decision Boundary", color='blue')
# Scatter plot for training and test data
[Link](X_train, y_train, c=y_train, marker='o', edgecolor='k',
label='Training data')
[Link](X_test, y_test, c=y_test, marker='s', edgecolor='k',
label='Test data')
[Link]("Logistic Regression - Decision Boundary")
[Link]('Feature')
[Link]('Probability of Class 1')
[Link]()
[Link]()