0% found this document useful (0 votes)
2 views3 pages

Prog 5

The document outlines the implementation of univariate logistic regression using the Iris dataset to classify two classes. It includes steps for training the model, evaluating its performance with metrics such as confusion matrix, accuracy, precision, recall, MSE, and RMSE, and predicting a new data point. Additionally, it features a plot of the decision boundary for the logistic regression model.

Uploaded by

sandeepswamy54
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)
2 views3 pages

Prog 5

The document outlines the implementation of univariate logistic regression using the Iris dataset to classify two classes. It includes steps for training the model, evaluating its performance with metrics such as confusion matrix, accuracy, precision, recall, MSE, and RMSE, and predicting a new data point. Additionally, it features a plot of the decision boundary for the logistic regression model.

Uploaded by

sandeepswamy54
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/ 3

"""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 sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import confusion_matrix, accuracy_score,
precision_score, recall_score, mean_squared_error
from sklearn.preprocessing import StandardScaler
import matplotlib.pyplot as plt

# Load the Iris dataset


iris = load_iris()
X = iris.data[:, :1] # Only one feature (first column)
y = iris.target # 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 = scaler.transform(X_test)

# Initialize and train the logistic regression model


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

LogisticRegression()

# Predict using the trained model


y_pred = model.predict(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 = np.sqrt(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 = np.array([[0.5]]) # Sample new data point
new_data_scaled = scaler.transform(new_data)

# Predict the class for the new data point


new_pred = model.predict(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 = np.arange(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
plt.plot(xx, probs, label="Decision Boundary", color='blue')

# Scatter plot for training and test data


plt.scatter(X_train, y_train, c=y_train, marker='o', edgecolor='k',
label='Training data')
plt.scatter(X_test, y_test, c=y_test, marker='s', edgecolor='k',
label='Test data')

plt.title("Logistic Regression - Decision Boundary")


plt.xlabel('Feature')
plt.ylabel('Probability of Class 1')
plt.legend()
plt.show()

You might also like