0% found this document useful (0 votes)
0 views4 pages

Lab Exam ... Roll No 24cs4103

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 4

Logistic Regression

Implement the logistic regression in Python using the dataset available in


Kaggel or UCI. You must submit two programs with and without using the
inbuilt function (from sklearn.linear_model import Linear Regression ).

1. Logistic Regression Using Scikit-Learn

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn import datasets
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 = datasets.load_iris()
X = iris.data[:, :2] # Using only the first two features for 2D plotting
y = iris.target

# Create a DataFrame for better visualization


iris_df = pd.DataFrame(data=X, columns=iris.feature_names[:2])
iris_df['target'] = y

# Split the dataset into training and testing sets


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

# Create and train the logistic regression model


model = LogisticRegression(multi_class='multinomial', solver='lbfgs', max_iter=200)
model.fit(X_train, y_train)

# Plot the original data points


plt.figure(figsize=(12, 6))

plt.subplot(1, 2, 1)
plt.scatter(X[:, 0], X[:, 1], c=y, edgecolors='k', cmap=plt.cm.Set1)
plt.title('Iris Dataset: Original Data Points')
plt.xlabel(iris.feature_names[0])
plt.ylabel(iris.feature_names[1])
plt.grid()

# Plotting decision boundaries


x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
xx, yy = np.meshgrid(np.arange(x_min, x_max, 0.01),
np.arange(y_min, y_max, 0.01))

Z = model.predict(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)

plt.subplot(1, 2, 2)
plt.contourf(xx, yy, Z, alpha=0.8, cmap=plt.cm.Set1)
plt.scatter(X[:, 0], X[:, 1], c=y, marker='o', label='Data Points')
plt.scatter(X_test[:, 0], X_test[:, 1], c='black', marker='x', label='Test Data')
plt.title('Logistic Regression Decision Boundary')
plt.xlabel(iris.feature_names[0])
plt.ylabel(iris.feature_names[1])
plt.legend()
plt.grid()

plt.tight_layout()
plt.show()
2. Logistic Regression Without Using Inbuilt Functions

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report, confusion_matrix, accuracy_score

# Load the Iris dataset


iris = datasets.load_iris()
X = iris.data[:, :2] # Using only the first two features for 2D plotting
y = iris.target

# Split the dataset into training and testing sets


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

class LogisticRegressionScratch:
def __init__(self, learning_rate=0.01, num_iterations=1000):
self.learning_rate = learning_rate
self.num_iterations = num_iterations
self.weights = None
self.bias = None

def softmax(self, z):


exp_z = np.exp(z - np.max(z, axis=1, keepdims=True)) # Stability improvement
return exp_z / np.sum(exp_z, axis=1, keepdims=True)

def fit(self, X, y):


num_samples, num_features = X.shape
num_classes = len(np.unique(y))
self.weights = np.zeros((num_features, num_classes))
self.bias = np.zeros(num_classes)

for _ in range(self.num_iterations):
# Linear model
linear_model = np.dot(X, self.weights) + self.bias
probabilities = self.softmax(linear_model)

# One-hot encode the target


y_one_hot = np.zeros((num_samples, num_classes))
y_one_hot[np.arange(num_samples), y] = 1

# Compute gradients
dw = (1 / num_samples) * np.dot(X.T, (probabilities - y_one_hot))
db = (1 / num_samples) * np.sum(probabilities - y_one_hot, axis=0)

# Update weights and bias


self.weights -= self.learning_rate * dw
self.bias -= self.learning_rate * db

def predict(self, X):


linear_model = np.dot(X, self.weights) + self.bias
probabilities = self.softmax(linear_model)
return np.argmax(probabilities, axis=1)

# Create and train the logistic regression model


model = LogisticRegressionScratch(learning_rate=0.1, num_iterations=1000)
model.fit(X_train, y_train)
# Plot the original data points
plt.figure(figsize=(12, 6))

plt.subplot(1, 2, 1)
plt.scatter(X[:, 0], X[:, 1], c=y, edgecolors='k', cmap=plt.cm.Set1)
plt.title('Iris Dataset: Original Data Points')
plt.xlabel(iris.feature_names[0])
plt.ylabel(iris.feature_names[1])
plt.grid()

# Plotting decision boundaries


x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
xx, yy = np.meshgrid(np.arange(x_min, x_max, 0.01),
np.arange(y_min, y_max, 0.01))

Z = model.predict(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)

plt.subplot(1, 2, 2)
plt.contourf(xx, yy, Z, alpha=0.8, cmap=plt.cm.Set1)
plt.scatter(X[:, 0], X[:, 1], c=y, marker='o', label='Data Points')
plt.scatter(X_test[:, 0], X_test[:, 1], c='black', marker='x', label='Test Data')
plt.title('Logistic Regression Decision Boundary')
plt.xlabel(iris.feature_names[0])
plt.ylabel(iris.feature_names[1])
plt.legend()
plt.grid()

plt.tight_layout()
plt.show()

You might also like