Home » Home » Logistic Regression with Python

Logistic regression is a popular statistical method used to model the relationship between a dependent variable and one or more independent variables. Unlike linear regression, which predicts continuous values, it predicts the probability of a binary outcome, such as whether a customer will buy a product or not.

Read Also- Linear Regression with Python

In this article, we’ll explore how to perform logistic regression in Python using the scikit-learn library.

  1. Import Required Libraries: Start by importing the required libraries, including NumPy, Pandas, Matplotlib, and scikit-learn.
  2. Load the Data: Load your data into Python using Pandas or another data manipulation library.
  3. Preprocess the Data: Preprocess the data as required, including cleaning, scaling, and normalization.
  4. Split the Data: Split the data into training and testing sets using scikit-learn’s train_test_split function.
  5. Create the Logistic Regression Model: Create its model using the LogisticRegression class in scikit-learn.
  6. Train the Model: Fit the model to the training data using the fit method.
  7. Evaluate the Model: Evaluate the model’s performance on the testing data using evaluation metrics such as accuracy, precision, recall, and F1 score.
  8. Predict the Results: Use the predict method to make predictions on new data.
  9. Visualize the Results: Visualize the results using Matplotlib or other visualization libraries.

Let’s look at an example of how to perform logistic regression in Python using scikit-learn:

# Import Required Libraries
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score

# Load the Data
data = pd.read_csv('data.csv')

# Preprocess the Data
X = data.iloc[:, :-1].values
y = data.iloc[:, -1].values

# Split the Data
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)

# Create the Logistic Regression Model
regressor = LogisticRegression()

# Train the Model
regressor.fit(X_train, y_train)

# Evaluate the Model
y_pred = regressor.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
precision = precision_score(y_test, y_pred)
recall = recall_score(y_test, y_pred)
f1 = f1_score(y_test, y_pred)

# Predict the Results
new_data = np.array([[5.0], [10.0], [15.0]])
new_predictions = regressor.predict(new_data)

# Visualize the Results
plt.scatter(X_test, y_test, color='red')
plt.plot(X_test, y_pred, color='blue')
plt.title('Logistic Regression')
plt.xlabel('Independent Variable')
plt.ylabel('Dependent Variable')

In this example, we load a dataset from a CSV file, preprocess the data, split it into training and testing sets, create a Logistic Regression model, train the model on the training data, evaluate the model on the testing data, make predictions on new data, and visualize the results.


It is a useful statistical method for predicting binary outcomes, such as whether a customer will buy a product or not. With the scikit-learn library in Python, it is easy to perform logistic regression and other machine learning tasks on data of various types and sizes. By following the steps outlined in this article, you can start using logistic regression to solve a wide range of data analysis problems in Python.

Related Posts

One thought on “Logistic Regression with Python

Leave a Reply

%d bloggers like this: