CS3EL15(P): Machine learning Laboratory Experiment no- 4
Experiment : To execute Logistic Regression using a properly identified dataset and Page 1 of 5
analyze the model's performance on the test set. b) To implement Logistic Regression using
Python.
PRACTICAL - 4
Aim:
a) To execute Logistic Regression using a properly identified dataset and analyze the model's
performance on the test set.
b) To implement Logistic Regression using Python.
Theory:
Logistic Regression is a supervised learning algorithm used for binary classification problems. Unlike
linear regression, it predicts the probability that a given input belongs to a particular category using the
sigmoid function.
For example, we have two classes Class 0 and Class 1 if the value of the logistic function for an input is
greater than 0.5 (threshold value) then it belongs to Class 1 otherwise it belongs to Class 0. It’s referred
to as regression because it is the extension of linear regression but is mainly used for classification
problems.
Mathematically,
Function in Linear Regression
Department of Computer Science & Engineering
Student Name: Yuvraj Singh Sikarwar Enrollment No: EN22CS3011129
CS3EL15(P): Machine learning Laboratory Experiment no- 4
Experiment : To execute Logistic Regression using a properly identified dataset and Page 1 of 5
analyze the model's performance on the test set. b) To implement Logistic Regression using
Python.
Department of Computer Science & Engineering
Student Name: Yuvraj Singh Sikarwar Enrollment No: EN22CS3011129
CS3EL15(P): Machine learning Laboratory Experiment no- 4
Experiment : To execute Logistic Regression using a properly identified dataset and Page 1 of 5
analyze the model's performance on the test set. b) To implement Logistic Regression using
Python.
Program:
Step 1: Import Libraries
pandas: Used for loading and handling the dataset.
train_test_split: Splits data into training and testing sets.
StandardScaler: Normalizes (standardizes) feature values.
LogisticRegression: Implements Logistic Regression for binary classification.
accuracy_score, classification_report: Evaluates model performance.
Step 2: Load and Explore the Dataset
Assuming you have a CSV file named '[Link]'.
The dataset is loaded from a CSV file using pandas.read_csv().
Step 3: Select Features and Target Variable
X (Features): Hours Studied and Hours Slept.
Y (Target variable): Whether the student passed
Step 4: Split the data into training and tes ng sets (80% train, 20% test)
80% training, 20% testing (test_size=0.2).
Random_state=42 ensures reproducibility.
Step 5: Standardize the features
Department of Computer Science & Engineering
Student Name: Yuvraj Singh Sikarwar Enrollment No: EN22CS3011129
CS3EL15(P): Machine learning Laboratory Experiment no- 4
Experiment : To execute Logistic Regression using a properly identified dataset and Page 1 of 5
analyze the model's performance on the test set. b) To implement Logistic Regression using
Python.
StandardScaler normalizes features to have a mean of 0 and standard deviation of 1.
This helps Logistic Regression converge faster and perform better.
Step 6: Train logistic regression model
The Logistic Regression model is trained using the scaled training data.
Step 7:-Evaluate the model
The trained model predicts labels (Pass/Fail) for the test data.
Step 8: Create a mesh grid for plotting decision boundary
Accuracy Score: Measures the percentage of correct predictions.
Classification Report: Shows precision, recall, and F1-score.
Step 9: Transform mesh grid using the same scaler
Standardizes the grid points using the same scaler used for training data.
Step 10: Scatter plot of the actual data points
Department of Computer Science & Engineering
Student Name: Yuvraj Singh Sikarwar Enrollment No: EN22CS3011129
CS3EL15(P): Machine learning Laboratory Experiment no- 4
Experiment : To execute Logistic Regression using a properly identified dataset and Page 1 of 5
analyze the model's performance on the test set. b) To implement Logistic Regression using
Python.
Plots students' actual study and sleep hours, with color representing Pass/Fail.
cmap=[Link] uses blue for 0 (Fail) and red for 1 (Pass).
Output:-
Department of Computer Science & Engineering
Student Name: Yuvraj Singh Sikarwar Enrollment No: EN22CS3011129