Total Pageviews

Monday, June 29, 2026

Logistic Regression Using Python

 

🟦🟩🟨 Logistic Regression Using Python



🎯 Aim

To implement the Logistic Regression algorithm using Python to classify whether a patient has a disease based on their age.


🧠 Theory

Logistic Regression is a Supervised Machine Learning algorithm used for classification problems. Unlike Linear Regression, which predicts continuous values, Logistic Regression predicts categories or classes.

Examples

  • 📧 Spam / Not Spam
  • 🏥 Disease / Healthy
  • 🎓 Pass / Fail
  • 💳 Fraud / Not Fraud

The output is usually:

  • 0 → No
  • 1 → Yes

Logistic Regression uses the Sigmoid Function to convert predictions into probabilities between 0 and 1.


🌍 Real-Life Example

A hospital wants to predict whether a patient has diabetes based only on Age.

Training Data

AgeDisease
200
250
300
350
401
451
501
551

Here,

  • 0 = Healthy
  • 1 = Disease

📝 Step 1: Import Required Libraries

from sklearn.linear_model import LogisticRegression

Explanation

  • sklearn → Machine Learning library.
  • linear_model → Contains regression algorithms.
  • LogisticRegression → Used for classification.

📝 Step 2: Create Input Data

X = [
[20],
[25],
[30],
[35],
[40],
[45],
[50],
[55]
]

Explanation

X stores the input feature (Age).

Each value is written inside another list because Scikit-learn expects 2D input.

X

20
25
30
35
40
45
50
55

📝 Step 3: Create Output Data

y = [
0,
0,
0,
0,
1,
1,
1,
1
]

Explanation

y stores the output labels.

ValueMeaning
0Healthy
1Disease

📝 Step 4: Create the Model

model = LogisticRegression()

Explanation

This creates an empty Logistic Regression model.

At this stage:

✔ No learning

✔ No prediction

✔ Just an empty model


📝 Step 5: Train the Model

model.fit(X, y)

Explanation

fit() teaches the model using the training data.

The model learns:

  • Relationship between Age and Disease
  • Probability of Disease

This is called the Training Phase.

Training Data


Model Learning


Trained Model

📝 Step 6: Predict New Data

Suppose a new patient is 42 years old.

prediction = model.predict([[42]])

Explanation

The model compares age 42 with the learned pattern and predicts:

Either

0

or

1

📝 Step 7: Display Prediction

print(prediction)

Output

[1]

Meaning

Disease

📝 Step 8: Display User-Friendly Output

if prediction[0] == 1:
print("Patient has Disease")
else:
print("Patient is Healthy")

Output

Patient has Disease

✅ Complete Python Program

# Logistic Regression Example

from sklearn.linear_model import LogisticRegression

# Input Data (Age)
X = [
[20],
[25],
[30],
[35],
[40],
[45],
[50],
[55]
]

# Output Data
# 0 = Healthy
# 1 = Disease

y = [
0,
0,
0,
0,
1,
1,
1,
1
]

# Create Model
model = LogisticRegression()

# Train Model
model.fit(X, y)

# Predict
prediction = model.predict([[42]])

# Display Prediction
if prediction[0] == 1:
print("Patient has Disease")
else:
print("Patient is Healthy")

💻 Expected Output

Patient has Disease

🔍 Step-by-Step Flow

Import Library


Create Dataset


Create Logistic Regression Model


Train Model using fit()


Predict using predict()


Display Result

📌 Understanding fit()

model.fit(X, y)

This is the most important line.

It means:

Teach the computer using
X → Input

y → Output

After this line, the model becomes trained.


📌 Understanding predict()

model.predict([[42]])

Meaning:

Predict the output for a new patient whose age is 42 years.


📌 Understanding prediction[0]

predict() returns a list (or array).

Example:

prediction = [1]

To access the first value:

prediction[0]

Result

1

📊 Training Data Visualization

AgeDisease
20Healthy
25Healthy
30Healthy
35Healthy
40Disease
45Disease
50Disease
55Disease

The model learns that higher ages in this small example are associated with the "Disease" class.


📈 Why Logistic Regression?

Linear RegressionLogistic Regression
Predicts numbersPredicts categories
Output can be any valueOutput is a probability (0–1) and a class
Used for RegressionUsed for Classification

⭐ Advantages

  • ✔ Easy to implement
  • ✔ Fast training
  • ✔ Works well for binary classification
  • ✔ Produces probability estimates
  • ✔ Easy to interpret

❌ Limitations

  • ❌ Assumes a linear relationship between features and the log-odds
  • ❌ Less effective for highly complex, non-linear data
  • ❌ Sensitive to outliers in some situations

🌍 Applications

  • 🏥 Disease Prediction
  • 📧 Spam Detection
  • 💳 Credit Card Fraud Detection
  • 🎓 Student Pass/Fail Prediction
  • 🛒 Customer Purchase Prediction
  • 🏦 Loan Approval Prediction

🎯 Viva Questions

  1. What is Logistic Regression?
  2. Why is it called "Regression" if it is used for classification?
  3. What is the role of the Sigmoid Function?
  4. What does fit() do?
  5. What does predict() do?
  6. Why is the input written as [[42]] instead of [42]?
  7. What is the meaning of prediction[0]?
  8. What types of problems can Logistic Regression solve?

📝 One-Line Revision

Logistic Regression is a supervised machine learning algorithm that predicts the probability of a data point belonging to a particular class and is mainly used for binary classification problems.

Linear Regression Using Python

 

Experiment 1: Linear Regression Using Python



🎯 Aim

To implement the Linear Regression algorithm using Python and predict the value of a dependent variable based on an independent variable.


📖 Theory

Linear Regression is one of the simplest Supervised Machine Learning algorithms. It is used to predict continuous numerical values by finding a best-fit straight line between the input (independent variable) and the output (dependent variable).

It assumes a linear relationship between the variables.

Mathematical Equation

Y=mX+cY = mX + c

Where:

  • Y = Predicted Output (Dependent Variable)
  • X = Input (Independent Variable)
  • m = Slope of the Line
  • c = Intercept

🌍 Real-Life Example

A company wants to predict an employee's salary based on their years of experience.

Experience (Years)Salary (₹)
125,000
230,000
335,000
445,000
550,000
660,000
765,000
870,000

Now, we want to predict the salary of an employee with 9 years of experience.


🪜 Step-by-Step Algorithm

Step 1️⃣ Import Required Libraries

Import the necessary libraries.

import pandas as pd
from sklearn.linear_model import LinearRegression

Explanation

  • pandas → Used to create and manage datasets.
  • LinearRegression → Imports the Linear Regression model from scikit-learn.

Step 2️⃣ Create the Dataset

data = {
"Experience": [1,2,3,4,5,6,7,8],
"Salary": [25000,30000,35000,45000,50000,60000,65000,70000]
}

df = pd.DataFrame(data)

Explanation

We create a simple dataset using a Python dictionary.

The dataset has two columns:

  • Experience → Independent Variable (X)
  • Salary → Dependent Variable (Y)

The data is converted into a DataFrame for easy processing.


Step 3️⃣ Display the Dataset

print(df)

Output

   Experience  Salary
0 1 25000
1 2 30000
2 3 35000
3 4 45000
4 5 50000
5 6 60000
6 7 65000
7 8 70000

Step 4️⃣ Separate Input and Output Variables

X = df[["Experience"]]
y = df["Salary"]

Explanation

Machine Learning models require:

  • X → Input Features (Independent Variable)
  • y → Target Variable (Dependent Variable)

Here:

X = Experience

y = Salary

Step 5️⃣ Create the Linear Regression Model

model = LinearRegression()

Explanation

This creates an empty Linear Regression model.

At this stage, the model has not learned from the data.


Step 6️⃣ Train the Model

model.fit(X, y)

Explanation

The fit() function trains the model.

During training:

  • Reads all training data
  • Calculates the best-fit line
  • Finds the slope (m)
  • Finds the intercept (c)

The model is now ready for prediction.


Step 7️⃣ Predict Salary

experience = [[9]]

prediction = model.predict(experience)

Explanation

We ask the model:

"Predict the salary of an employee with 9 years of experience."

The predict() function uses the learned line to estimate the salary.


Step 8️⃣ Display the Prediction

print("Predicted Salary =", prediction[0])

Sample Output

Predicted Salary = 78809.52

(The exact value may vary slightly depending on the fitted line.)


💻 Complete Python Program

# Step 1: Import Libraries
import pandas as pd
from sklearn.linear_model import LinearRegression

# Step 2: Create Dataset
data = {
"Experience": [1,2,3,4,5,6,7,8],
"Salary": [25000,30000,35000,45000,50000,60000,65000,70000]
}

df = pd.DataFrame(data)

# Step 3: Display Dataset
print("Dataset:")
print(df)

# Step 4: Separate Input and Output
X = df[["Experience"]]
y = df["Salary"]

# Step 5: Create Model
model = LinearRegression()

# Step 6: Train Model
model.fit(X, y)

# Step 7: Predict Salary
experience = [[9]]
prediction = model.predict(experience)

# Step 8: Display Result
print("\nPredicted Salary for 9 years experience = ₹", round(prediction[0],2))

🔄 Workflow

Start


Import Libraries


Create Dataset


Display Dataset


Separate X and y


Create Linear Regression Model


Train Model using fit()


Predict using predict()


Display Prediction


End

📌 Explanation of Important Functions

FunctionPurpose
pd.DataFrame()Creates a table from data
LinearRegression()Creates the regression model
fit(X, y)Trains the model using the dataset
predict()Predicts the output for new input

✅ Advantages

  • Simple and easy to implement
  • Fast training and prediction
  • Easy to interpret results
  • Works well for linear relationships

❌ Limitations

  • Only models linear relationships
  • Sensitive to outliers
  • Performance decreases if data is non-linear

🌍 Applications

  • Salary Prediction
  • House Price Prediction
  • Sales Forecasting
  • Stock Trend Analysis
  • Weather Forecasting
  • Business Revenue Prediction


⭐ Memory Trick

Import Libraries

Create Dataset

Separate X and y

Create Model

Train using fit()

Predict using predict()

Display Result

Easy Formula to Remember:
Import → Data → X & y → Model → Fit → Predict → Output


🎓 Viva Questions

  1. What is Linear Regression?
  2. Why is it called a supervised learning algorithm?
  3. What are the independent and dependent variables?
  4. What is the purpose of fit()?
  5. What is the purpose of predict()?
  6. What is the equation of a regression line?
  7. What is the role of X and y?
  8. Give two real-life applications of Linear Regression.

Comparison of Different Types of Machine Learning

 

🌈 Comparison of Different Types of Machine Learning

📊 Complete Comparison Table

📌 Feature🟦 Supervised Learning🟩 Unsupervised Learning🟪 Reinforcement Learning🟨 Semi-Supervised Learning
📖 DefinitionLearns from labeled data where the correct output is already known.Learns from unlabeled data to discover hidden patterns and relationships.Learns by interacting with the environment using rewards and penalties.Learns using both labeled and unlabeled data.
🏷️ Data TypeLabeled DataUnlabeled DataReward-based DataPartially Labeled Data
👨‍🏫 Teacher / Supervisor✅ Required❌ Not Required❌ Not Required✅ Small Amount of Guidance
🎯 GoalPredict the correct output.Find hidden patterns or groups.Learn the best action to maximize reward.Improve prediction using limited labeled data.
🧠 Learning MethodLearns from examples with known answers.Learns by finding similarities among data.Learns through trial and error.Learns from labeled data and improves using unlabeled data.
📤 OutputPredicted Class or ValueClusters, Groups, PatternsBest Action (Optimal Policy)Improved Prediction
📂 Data Labels✅ Available❌ Not Available❌ Not Required⚠️ Partially Available
📈 Accuracy EvaluationEasy to evaluateDifficult to evaluateBased on total rewardModerate
⚙️ Main TechniquesClassification, RegressionClustering, Association, Dimensionality ReductionQ-Learning, Deep Q Network (DQN), Policy LearningSelf-Training, Co-Training, Label Propagation
💰 Cost of Data PreparationHigh (Labeling Required)LowMediumMedium
⏱️ Training TimeMediumMediumHighMedium
🎓 Best Used WhenCorrect output is already known.No labels are available.Sequential decision-making is required.Only a small amount of labeled data is available.

🌍 Real-Life Examples

Learning TypeExample
🟦 Supervised Learning🏦 Bank Loan Approval
🟩 Unsupervised Learning🛒 Customer Segmentation in a Shopping Mall
🟪 Reinforcement Learning🤖 Delivery Robot Learning the Best Route
🟨 Semi-Supervised Learning🏥 Medical X-ray Disease Detection

🌟 Advantages Comparison

Learning TypeMajor Advantages
🟦 Supervised✅ High Accuracy, Easy Evaluation
🟩 Unsupervised✅ Finds Hidden Patterns, No Labels Needed
🟪 Reinforcement✅ Learns Best Decisions Through Experience
🟨 Semi-Supervised✅ Reduces Labeling Cost, Better Accuracy

❌ Limitations Comparison

Learning TypeMajor Limitations
🟦 Supervised❌ Requires Large Labeled Dataset
🟩 Unsupervised❌ Results Can Be Difficult to Interpret
🟪 Reinforcement❌ Training Takes Long Time and Many Trials
🟨 Semi-Supervised❌ Depends on the Quality of Labeled Data

📚 Common Algorithms

Learning TypePopular Algorithms
🟦 SupervisedLinear Regression, Logistic Regression, Decision Tree, Random Forest, SVM, KNN, Naïve Bayes
🟩 UnsupervisedK-Means, Hierarchical Clustering, DBSCAN, Apriori, PCA
🟪 ReinforcementQ-Learning, SARSA, Deep Q Network (DQN), Actor-Critic
🟨 Semi-SupervisedSelf-Training, Label Propagation, Co-Training, Semi-Supervised SVM

🎯 Quick Revision Table

Question🟦 Supervised🟩 Unsupervised🟪 Reinforcement🟨 Semi-Supervised
Uses Labeled Data?✅ Yes❌ No❌ No✅ Partially
Uses Unlabeled Data?❌ No✅ Yes❌ No✅ Yes
Uses Rewards?❌ No❌ No✅ Yes❌ No
Needs a Teacher?✅ Yes❌ No❌ No✅ Partially
Learns by Trial & Error?❌ No❌ No✅ Yes❌ No
Finds Hidden Patterns?❌ No✅ Yes❌ No⚠️ Partially
Makes Predictions?✅ Yes❌ No✅ Yes (Best Action)✅ Yes

📝 Exam Tip (Easy Memory Trick)

Learning TypeRemember As
🟦 Supervised Learning📚 Learn with a Teacher (Labeled Data)
🟩 Unsupervised Learning🔍 Discover Hidden Patterns (Unlabeled Data)
🟪 Reinforcement Learning🏆 Learn by Rewards & Penalties
🟨 Semi-Supervised Learning📖 Learn from a Few Labels + Many Unlabeled Data

⭐ One-Line Revision

Learning TypeOne-Line Summary
🟦 Supervised LearningLabeled Data → Learn → Predict Output
🟩 Unsupervised LearningUnlabeled Data → Find Hidden Patterns
🟪 Reinforcement LearningAction → Reward/Penalty → Learn Best Decision
🟨 Semi-Supervised LearningFew Labels + Many Unlabeled Data → Better Prediction

Semi-Supervised Learning

 

#️⃣ Semi-Supervised Learning 

🏥 Example: Medical Image Classification


🟦 1. 📖 Introduction

💡 Semi-Supervised Learning is a type of Machine Learning that combines a small amount of labeled data with a large amount of unlabeled data.

It is useful when labeling data is expensive, time-consuming, or requires expert knowledge. The algorithm first learns from the labeled data and then uses the unlabeled data to improve its performance.


🌟 Definition

Semi-Supervised Learning is a machine learning technique that uses both labeled and unlabeled data for training. A small amount of labeled data guides the model, while a large amount of unlabeled data helps improve learning and prediction accuracy.


🟩 2. 🏥 Real-Life Example

A hospital wants to build an AI system to detect Pneumonia from chest X-ray images.

The hospital has:

📷 10,000 X-ray Images

However,

✔ Only 1,000 images have been examined and labeled by doctors.

❓ The remaining 9,000 images have no labels.

Instead of ignoring the unlabeled images, the AI learns from both labeled and unlabeled images.


🟨 3. 🔄 Step-by-Step Working


🟢 Step 1 : 📥 Collect Input Data

The hospital collects thousands of chest X-ray images.

Available Data

📷 Chest X-ray Images

👨 Patient Information

📅 Examination Date

🏥 Hospital Records

This is called the Input Dataset.


🟢 Step 2 : 🏷️ Partial Labeling

Doctors examine only a small number of images.

Example

🩻 X-ray Image🏷️ Label
Image 1✅ Pneumonia
Image 2❌ Normal
Image 3✅ Pneumonia
Image 4❓ Unknown
Image 5❓ Unknown

📌 Only some images have labels.

The remaining images are Unlabeled Data.


🟢 Step 3 : 🤖 Train the Machine Learning Model

The model first learns from the labeled X-ray images.

It understands important features such as:

🫁 Lung Infection

🌫 White Spots

🩻 Abnormal Lung Patterns

These features help the model recognize pneumonia.


🟢 Step 4 : 📂 Use Unlabeled Data

The model now studies the unlabeled X-ray images.

It compares them with the labeled examples.

The algorithm gradually predicts labels for images that were previously unlabeled.


🟢 Step 5 : 🧠 Improve Learning

As more unlabeled images are analyzed,

✔ The model becomes smarter.

✔ Prediction accuracy improves.

✔ The AI discovers more disease patterns.

Thus, both labeled and unlabeled data contribute to better learning.


🟢 Step 6 : 🎯 Prediction

A new patient's X-ray is provided.

Example

🩻 New X-ray Image

🤖 Machine Learning Model

Prediction: Pneumonia Detected

or

Prediction: Normal Lungs


🟥 4. 🔄 Semi-Supervised Learning Workflow

📥 Input Dataset
(Labeled + Unlabeled Images)
            │
            ▼
🏷️ Small Amount of Labeled Data
            │
            ▼
📂 Large Amount of Unlabeled Data
            │
            ▼
🤖 Machine Learning Model
            │
            ▼
🧠 Learns from Both Types of Data
            │
            ▼
🎯 Disease Prediction

🟪 5. 📋 Important Components

🧩 Component📖 Description
📥 Input DataMedical X-ray Images
🏷️ Labeled DataImages labeled by doctors
❓ Unlabeled DataImages without labels
🤖 Machine Learning ModelLearns from both datasets
🎯 OutputDisease Prediction

🟦 6. ⚖️ Comparison of Data

📊 Data TypeExample
🏷️ Labeled Data1,000 X-ray images with diagnosis
❓ Unlabeled Data9,000 X-ray images without diagnosis

🟩 7. 🌍 Applications

🏥 Medical Image Diagnosis

📧 Email Classification

😊 Face Recognition

🗣 Speech Recognition

🚗 Autonomous Vehicles

📄 Document Classification

🌾 Crop Disease Detection

🛰 Satellite Image Analysis


🟦 8. ✅ Advantages

✔ Requires fewer labeled examples

✔ Reduces labeling cost

✔ Improves prediction accuracy

✔ Makes use of large unlabeled datasets

✔ Useful when expert labeling is expensive


🟥 9. ❌ Limitations

❌ Incorrect unlabeled data may reduce accuracy

❌ More complex than supervised learning

❌ Requires careful model design

❌ Performance depends on the quality of labeled data


🟨 10. ⭐ Comparison of Learning Types

🟢 Supervised🔵 Unsupervised🟣 Semi-Supervised
Only labeled dataOnly unlabeled dataBoth labeled and unlabeled data
Teacher availableNo teacherSmall amount of labeled guidance
Predicts outputFinds patternsImproves prediction using both datasets

🟥 11. 📝 Examination Definition

💡 Semi-Supervised Learning is a machine learning technique that uses both labeled and unlabeled data for training. The model learns from a small amount of labeled data and improves its performance by utilizing a large amount of unlabeled data.


🌟 🎯 Exam Tip

🔑 Remember This Sequence

📥 Input Data

⬇️

🏷️ Small Labeled Data

Large Unlabeled Data

⬇️

🤖 Machine Learning Model

⬇️

🧠 Learning Process

⬇️

🎯 Prediction


⭐ One-Line Revision

📚 Semi-Supervised Learning = Small Labeled Data + Large Unlabeled Data + Better Prediction