DEV Community

Nazim Shaikh
Nazim Shaikh

Posted on Edited on

LOGISTIC REGRESSION

INTRODUCTION

Machine Learning helps computers analyse information and make predictions. Logistic Regression is a popular supervised learning algorithm used to classify data into different categories.

WHAT IS LOGISTIC REGRESSION?

Logistic Regression is a supervised machine learning algorithm mainly used for classification problems. It predicts the probability of an outcome belonging to a particular category.

For example, it can classify an email as spam or not spam.

MATHEMATICAL FORMULA

P = 1 / (1 + e^(-z))

The sigmoid function converts the output into a probability between 0 and 1.

HOW DOES IT WORK?

  1. Collect and prepare the dataset.
  2. Identify the input features.
  3. Train the classification model.
  4. Calculate the probability of an outcome.
  5. Assign the result to a category using a threshold.

PYTHON IMPLEMENTATION

Install the required libraries:

pip install scikit-learn
Enter fullscreen mode Exit fullscreen mode

Python Code:

from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score

# Load dataset
data = load_breast_cancer()

X = data.data
y = data.target

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

# Create model
model = make_pipeline(
    StandardScaler(),
    LogisticRegression(max_iter=2000)
)

# Train model
model.fit(X_train, y_train)

# Make predictions
predictions = model.predict(X_test)

# Calculate accuracy
accuracy = accuracy_score(y_test, predictions)

print("Model Accuracy:", accuracy)
Enter fullscreen mode Exit fullscreen mode

OUTPUT

Model Accuracy: A value between 0 and 1.

The exact accuracy depends on the dataset split and training process.

APPLICATIONS

  1. Email spam detection.
  2. Disease classification.
  3. Fraud detection.
  4. Loan approval prediction.
  5. Customer behaviour analysis.

ADVANTAGES

  • Easy to understand and implement.
  • Provides probability-based predictions.
  • Useful for classification problems.
  • Computationally efficient for many datasets.

LIMITATIONS

  • May struggle with complex decision boundaries.
  • Performance depends on data quality.
  • Assumes a linear relationship between features and log-odds.

CONCLUSION

Logistic Regression is an important classification algorithm in machine learning. It helps organisations classify information and make better decisions in healthcare, banking, marketing and other industries.

Top comments (0)