2-Building the Logistic Regression Model

Introduction 

Description: Logistic regression is a supervised machine learning algorithm that can be used for classification tasks. It is a special case of linear regression where the target variable is categorical. Logistic regression predicts the probability of a data point belonging to a particular class Once we have extracted the features, we can build our logistic regression model. We will use the sci-kit-learn library in Python to implement logistic regression. This involves splitting the data into training and testing sets, fitting the model on the training data, and evaluating its performance on the testing data. In scikit-learn, logistic regression is implemented in the LogisticRegression class.

Section 1: Implementation 

To perform logistic regression with scikit-learn, you can follow these steps:

  1. Import the LogisticRegression class from the sklearn.linear_model module.
  2. Create an instance of the LogisticRegression class and specify the parameters of the model. The most important parameter is the solver parameter, which specifies the algorithm used to train the model. Other parameters include the penalty parameter, which specifies the type of regularization to use, and the C parameter, which controls the strength of the regularization.
  3. In this example, we are using the lbfgs solver, and the default value of C. The lbfgs solver is a relatively slow but accurate solver. The C parameter controls the strength of the regularization. A higher value of C will result in a less regularized model, which may be more accurate but also more prone to overfitting.
  4. Fit the model to the training data.
  5. Use the model to predict the labels of new data.

Here is an example of how to perform logistic regression with scikit-learn:

import numpy as np
import pandas as pd
from sklearn.datasets import load_iris
from sklearn.linear_model import LogisticRegression
# Load the iris dataset
iris = load_iris()
# Split the data into a training set and a test set
X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target, test_size=0.25)
clf = LogisticRegression(C=1.0, solver='lbfgs', multi_class='ovr')
# Train the model on the training set
clf.fit(X_train, y_train)
# Evaluate the model on the test set
score = clf.score(X_test, y_test)
print("Accuracy:", score)
# Make predictions on new data
X_new = np.array([[5.1, 3.5, 1.4, 0.2]])
prediction = clf.predict(X_new)
print("Prediction:", prediction)

The accuracy of logistic regression with scikit-learn depends on a number of factors, including the quality of the training data, the choice of parameters, and the complexity of the model. In general, logistic regression can achieve good accuracy on simple classification problems. However, it may not be as accurate on complex problems.

The logistic regression cost function in scikit-learn is the negative log-likelihood function. This function is minimized during the training process to find the parameters of the model that best fit the data.

References

Last modified: Tuesday, 30 December 2025, 12:32 PM