Implementation of Logistic Regression using Python and Scikit-Learn

Posted by


Logistic regression is a commonly used statistical technique for binary classification problems in machine learning. In logistic regression, the dependent variable is a binary variable that takes on the value of 0 or 1. It models the relationship between the independent variables and the probabilities of the outcomes.

In this tutorial, we will learn how to implement logistic regression using Python and the Scikit-Learn library. Scikit-Learn is a powerful open-source machine learning library that provides simple and efficient tools for data analysis and modeling.

Step 1: Import Libraries and Dataset
First, we need to import the necessary libraries for logistic regression and data manipulation. We will use the famous Iris dataset for this tutorial.

import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
from sklearn.datasets import load_iris
# Load the Iris dataset
iris = load_iris()
X = iris.data
y = iris.target

Step 2: Split the Data into Training and Testing Sets
Next, we need to split the dataset into training and testing sets. We will use 80% of the data for training and the remaining 20% for testing.

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

Step 3: Train the Logistic Regression Model
Now, we can create a logistic regression model and train it using the training data.

# Create the logistic regression model
model = LogisticRegression()

# Train the model
model.fit(X_train, y_train)

Step 4: Make Predictions and Evaluate the Model
Once the model is trained, we can make predictions on the test data and evaluate the performance of the model.

# Make predictions
y_pred = model.predict(X_test)

# Calculate the accuracy of the model
accuracy = accuracy_score(y_test, y_pred)
print(f"Accuracy: {accuracy}")

Step 5: Interpret the Results
Finally, we can interpret the results of the logistic regression model. The accuracy score tells us how well the model performed on the test dataset. Additionally, we can also examine the coefficients of the independent variables to understand their impact on the dependent variable.

# Get the coefficients of the model
coefficients = model.coef_
intercept = model.intercept_
print(f"Coefficients: {coefficients}")
print(f"Intercept: {intercept}")

By following these steps, you have successfully implemented logistic regression using Python with Scikit-Learn. Logistic regression is a powerful algorithm that is commonly used for binary classification tasks. It is important to note that logistic regression assumes a linear relationship between the independent variables and the log-odds of the dependent variable. It is also sensitive to outliers and multicollinearity, so it is important to preprocess the data appropriately before fitting the model.