linear regression assignments

Chapter: Understanding Linear Regression Output


When beginners train a Linear Regression model, they often see many numbers in the output and don't know:

What is good?
What is bad?
What should I focus on?
Why are these metrics needed?

This chapter explains every important output in simple language.


1. Training Records

Example:

Training Records: 80

What is it?

Training records are the data used to teach the model.

Suppose you have:

100 products

You split them:

80 for training
20 for testing

Then:

Training Records = 80

Why do we need training data?

Imagine teaching a child.

You show:

Apple
Apple
Apple

Banana
Banana
Banana

After seeing many examples, the child learns the difference.

Machine Learning works the same way.

The model learns patterns from training data.

Example:

Higher Marketing Spend
→ Higher Sales

More Reviews
→ Higher Sales

The model learns these relationships.


Can we train on all data?

Yes.

But then we cannot check whether the model actually learned.

That's why we keep some data separate.

That separate data is called:

Testing Data

2. Testing Records

Example:

Testing Records: 20

What is it?

Testing data is unseen data.

The model never sees it during training.


Example

Dataset:

100 products

Split:

80 Training

20 Testing

The model learns from 80 products.

Then we ask:

Can you predict sales
for these 20 products
you have never seen?

This is the test.


Why is testing important?

Without testing:

Model may memorize data
instead of learning.

This problem is called:

Overfitting

3. R² Score (R-Squared)

This is the most famous metric in Linear Regression.

Example:

R² = 0.95

What does R² measure?

It measures:

How well the model explains the data.

Or:

How good the model is.

Range of R²

0 → Worst

1 → Perfect

Example

Suppose actual sales are:

10000
15000
20000
25000

Predicted sales:

10100
14900
20100
24800

Predictions are very close.

Then:

R² ≈ 0.99

Very good.


Interpretation Table

R² ScoreMeaning
1.0Perfect
0.95Excellent
0.90Very Good
0.80Good
0.70Acceptable
Below 0.50Weak

Real Meaning

If:

R² = 0.92

It means:

92% of the variation in sales
is explained by the model.

4. MAE (Mean Absolute Error)

Example:

MAE = 500

What is MAE?

MAE tells:

How wrong the model is on average.

Example

Actual Sales:

10000
20000
30000

Predicted Sales:

10500
19500
30200

Errors:

500
500
200

Average:

(500+500+200)/3

= 400

MAE:

400

Meaning

Your predictions are wrong by:

₹400

on average.


Why people like MAE

Because it is easy to understand.

Example:

MAE = 250

means:

Average mistake = ₹250

Simple.


5. MSE (Mean Squared Error)

Example:

MSE = 250000

What is MSE?

MSE also measures error.

But before averaging:

It squares every error.

Example

Errors:

100
200
1000

Square them:

10000
40000
1000000

Average:

350000

Why square?

To punish big mistakes.


Example

Two models:

Model A:

100
100
100

errors

Model B:

50
50
1000

errors

Average errors look similar.

But Model B made one huge mistake.

MSE punishes that huge mistake heavily.


6. RMSE (Root Mean Squared Error)

Example:

RMSE = 600

Problem with MSE

MSE values become huge.

Example:

250000

Hard to understand.


Solution

Take square root.

√250000

= 500

Now easy to understand.


Meaning

Predictions are off by ₹500
on average.

Why companies use RMSE

Because it uses original units.

Sales:

RMSE:

Easy to explain to managers.


7. Actual vs Predicted

Example:

Actual     Predicted

10000      10200
15000      14900
20000      20100

Actual

Actual means:

Real value

Example:

Actual Sales

= ₹10000

Predicted

Predicted means:

Model's guess

Example:

Predicted Sales

= ₹10200

Difference

10200 - 10000

= 200

Error:

₹200

Why compare?

Because this shows:

How close the model
is to reality.

8. Coefficients

Example:

MarketingSpend : 1.8

WebsiteVisits : 2.1

DiscountPercent : 120

CustomerReviews : 95

These numbers are extremely important.


What is a coefficient?

A coefficient tells:

How much a feature affects
the prediction.

Example

MarketingSpend:

1.8

Meaning:

Spend ₹1 more

Sales increase ₹1.8

Website Visits

2.1

Meaning:

One extra website visit

increases sales by ₹2.1

Customer Reviews

95

Meaning:

One additional review

increases sales by ₹95

Why coefficients matter?

They help businesses understand:

What drives sales?

Example:

Reviews have huge impact

Marketing has medium impact

Discount has low impact

Now the company knows where to invest money.


9. Cross Validation Score

Example:

Scores:

0.91
0.93
0.92
0.94
0.90

Average:

0.92

What is Cross Validation?

Instead of testing once:

Train-Test Split

we test many times.


Example

100 records.

Split into:

5 groups

Train and test:

5 times

Each time:

Different test data

Why?

Because one test split may be lucky.

Cross Validation checks:

Can the model perform well
on many different datasets?

Meaning

If:

Cross Validation = 0.92

Then:

Model is reliable.

Final Summary

MetricQuestion It Answers
Training RecordsWhat data was used to learn?
Testing RecordsWhat data was used to evaluate?
How good is the model?
MAEOn average, how much am I wrong?
MSEHow severely should big mistakes be punished?
RMSEWhat is the practical prediction error?
Actual vs PredictedHow close are predictions to reality?
CoefficientsWhich features influence the prediction most?
Cross ValidationWill the model work well on unseen data in the future?

If you understand these 9 outputs deeply, you can read and explain the results of most beginner and intermediate Linear Regression projects with confidence.


MetricMeaning
Training Records            Data used to learn
Testing RecordsData used to evaluate
Model quality
MAEAverage error
MSESquared error
RMSEReal-world error
Cross ValidationReliability of model

Most important thing to remember

For Linear Regression projects, the outputs you should always analyze are:

  1. R² Score → How good is the model?

  2. MAE → Average error.

  3. RMSE → Practical prediction error.

  4. Coefficients → Which features affect the target most?

  5. Actual vs Predicted → How close predictions are to reality.

  6. Cross Validation Score → Can the model generalize to new data?




ProductID,MarketingSpend,WebsiteVisits,DiscountPercent,CustomerReviews,MonthlySales
1,5000,1200,5,45,15000
2,5500,1250,5,48,15800
3,6000,1300,6,50,16500
4,6500,1400,7,52,17500
5,7000,1500,8,55,18500
6,7500,1550,8,58,19300
7,8000,1600,9,60,20100
8,8500,1700,10,62,21200
9,9000,1800,10,65,22300
10,9500,1850,11,67,23000
11,5200,1220,5,46,15200
12,5700,1280,6,49,16000
13,6200,1350,6,51,16900
14,6700,1420,7,53,17800
15,7200,1480,8,56,18600
16,7700,1540,8,59,19500
17,8200,1620,9,61,20400
18,8700,1710,10,63,21400
19,9200,1790,10,66,22500
20,9700,1870,11,68,23300
21,5400,1240,5,47,15500
22,5900,1320,6,50,16300
23,6400,1380,7,52,17200
24,6900,1450,7,54,18100
25,7400,1520,8,57,19000
26,7900,1580,9,60,19900
27,8400,1660,9,62,20800
28,8900,1740,10,64,21900
29,9400,1820,11,66,22800
30,9900,1900,11,69,23700
31,5100,1210,5,45,15100
32,5600,1270,6,48,15900
33,6100,1330,6,50,16700
34,6600,1410,7,53,17600
35,7100,1490,8,55,18400
36,7600,1560,8,58,19400
37,8100,1630,9,60,20300
38,8600,1720,10,63,21300
39,9100,1810,10,65,22400
40,9600,1880,11,67,23200
41,5300,1230,5,46,15300
42,5800,1290,6,49,16100
43,6300,1360,6,51,17000
44,6800,1430,7,54,17900
45,7300,1510,8,56,18800
46,7800,1570,8,59,19700
47,8300,1650,9,61,20600
48,8800,1730,10,64,21700
49,9300,1810,10,66,22600
50,9800,1890,11,68,23500
51,10000,1920,12,70,24000
52,10200,1950,12,71,24400
53,10400,1980,13,72,24800
54,10600,2010,13,73,25200
55,10800,2040,14,74,25600
56,11000,2070,14,75,26000
57,11200,2100,15,76,26400
58,11400,2130,15,77,26800
59,11600,2160,16,78,27200
60,11800,2190,16,79,27600
61,12000,2220,17,80,28000
62,12200,2250,17,81,28400
63,12400,2280,18,82,28800
64,12600,2310,18,83,29200
65,12800,2340,19,84,29600
66,13000,2370,19,85,30000
67,13200,2400,20,86,30400
68,13400,2430,20,87,30800
69,13600,2460,21,88,31200
70,13800,2490,21,89,31600
71,14000,2520,22,90,32000
72,14200,2550,22,91,32400
73,14400,2580,23,92,32800
74,14600,2610,23,93,33200
75,14800,2640,24,94,33600
76,15000,2670,24,95,34000
77,15200,2700,25,96,34400
78,15400,2730,25,97,34800
79,15600,2760,26,98,35200
80,15800,2790,26,99,35600
81,16000,2820,27,100,36000
82,16200,2850,27,101,36400
83,16400,2880,28,102,36800
84,16600,2910,28,103,37200
85,16800,2940,29,104,37600
86,17000,2970,29,105,38000
87,17200,3000,30,106,38400
88,17400,3030,30,107,38800
89,17600,3060,31,108,39200
90,17800,3090,31,109,39600
91,18000,3120,32,110,40000
92,18200,3150,32,111,40400
93,18400,3180,33,112,40800
94,18600,3210,33,113,41200
95,18800,3240,34,114,41600
96,19000,3270,34,115,42000
97,19200,3300,35,116,42400
98,19400,3330,35,117,42800
99,19600,3360,36,118,43200
100,19800,3390,36,119,43600



Linear Regression Assignments

  1. Train a Linear Regression model to predict Monthly Sales.
import pandas as pd

from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression

# Load CSV
df = pd.read_csv("product_sales.csv")

# Features
X = df[
    [
        "MarketingSpend",
        "WebsiteVisits",
        "DiscountPercent",
        "CustomerReviews"
    ]
]

# Target
y = df["MonthlySales"]

# 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 = LinearRegression()

# Train Model
model.fit(X_train, y_train)

print("Model Trained Successfully")


  1. Calculate the R² (R-Squared) score.


from sklearn.metrics import r2_score

y_pred = model.predict(X_test)

r2 = r2_score(y_test, y_pred)

print("R² Score:", r2)



Interpretation:

R² = 1.0
Perfect prediction

R² = 0.90
90% variance explained

R² = 0
No predictive power





  1. Calculate MAE (Mean Absolute Error).
from sklearn.metrics import mean_absolute_error

mae = mean_absolute_error(y_test, y_pred)

print("MAE:", mae)



Meaning:

Average prediction error

Example:

MAE = 500

means predictions are off by ₹500 on average.


  1. Calculate MSE (Mean Squared Error).
from sklearn.metrics import mean_squared_error

mse = mean_squared_error(y_test, y_pred)

print("MSE:", mse)

MSE penalizes large errors more heavily.

  1. Calculate RMSE (Root Mean Squared Error).

from sklearn.metrics import mean_squared_error

rmse = mean_squared_error(
    y_test,
    y_pred,
    squared=False
)

print("RMSE:", rmse)


RMSE is easier to understand because it is in the same unit as sales.



  1. Print all Linear Regression coefficients.

for feature, coef in zip(
        X.columns,
        model.coef_
):
    print(feature, ":", coef)

print("Intercept:", model.intercept_)




  1. Interpret the coefficient of Website Visits.

Interpret Website Visits Coefficient

Suppose:

WebsiteVisits = 2.5

Meaning:

One additional website visit
increases expected sales by ₹2.5



  1. Interpret the coefficient of Discount Percentage.

Suppose:

DiscountPercent = 120

Meaning:

Increasing discount by 1%
increases expected sales by ₹120


  1. Interpret the coefficient of Customer Reviews.

Suppose:

CustomerReviews = 95

Meaning:

One additional positive review
increases expected sales by ₹95



  1. Predict sales for a new product.

new_product = pd.DataFrame(
    [[15000, 2800, 20, 100]],
    columns=[
        "MarketingSpend",
        "WebsiteVisits",
        "DiscountPercent",
        "CustomerReviews"
    ]
)

prediction = model.predict(new_product)

print(
    "Predicted Sales:",
    prediction[0]
)
  1. Compare actual sales and predicted sales.

comparison = pd.DataFrame({
    "Actual": y_test,
    "Predicted": y_pred
})

print(comparison.head())
  1. Find the most influential feature.
importance = pd.DataFrame({
    "Feature": X.columns,
    "Coefficient": model.coef_
})

print(
    importance.sort_values(
        by="Coefficient",
        ascending=False
    )


  1. Create a complete Product Sales Prediction System using Linear Regression.


import pandas as pd

from sklearn.model_selection import (
    train_test_split,
    cross_val_score
)

from sklearn.linear_model import LinearRegression

from sklearn.metrics import (
    r2_score,
    mean_absolute_error,
    mean_squared_error
)

# ==================================================
# DATASET
# ==================================================
df = pd.read_csv("product_sales.csv")


print("=" * 50)
print("DATASET")
print("=" * 50)

print(df.head())

# ==================================================
# FEATURES AND TARGET
# ==================================================

X = df[
    [
        "MarketingSpend",
        "WebsiteVisits",
        "DiscountPercent",
        "CustomerReviews"
    ]
]

y = df["MonthlySales"]

# ==================================================
# TRAIN TEST SPLIT
# ==================================================

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

# ==================================================
# LINEAR REGRESSION MODEL
# ==================================================

model = LinearRegression()

model.fit(X_train, y_train)

print("\nModel Trained Successfully!")

# ==================================================
# PREDICTIONS
# ==================================================

y_pred = model.predict(X_test)

# ==================================================
# EVALUATION METRICS
# ==================================================

r2 = r2_score(y_test, y_pred)

mae = mean_absolute_error(y_test, y_pred)

mse = mean_squared_error(y_test, y_pred)

rmse = mse ** 0.5

print("\n" + "=" * 50)
print("MODEL EVALUATION")
print("=" * 50)

print("R² Score :", round(r2, 4))
print("MAE      :", round(mae, 2))
print("MSE      :", round(mse, 2))
print("RMSE     :", round(rmse, 2))

# ==================================================
# ACTUAL VS PREDICTED
# ==================================================

comparison = pd.DataFrame({
    "Actual Sales": y_test.values,
    "Predicted Sales": y_pred
})

print("\n" + "=" * 50)
print("ACTUAL VS PREDICTED")
print("=" * 50)

print(comparison)

# ==================================================
# MODEL COEFFICIENTS
# ==================================================

print("\n" + "=" * 50)
print("FEATURE COEFFICIENTS")
print("=" * 50)

for feature, coefficient in zip(
        X.columns,
        model.coef_):

    print(
        feature,
        ":",
        round(coefficient, 4)
    )

print("\nIntercept:",
      round(model.intercept_, 4))

# ==================================================
# NEW PRODUCT PREDICTION
# ==================================================

new_product = pd.DataFrame(
    [[15000, 2800, 20, 100]],
    columns=[
        "MarketingSpend",
        "WebsiteVisits",
        "DiscountPercent",
        "CustomerReviews"
    ]
)

predicted_sales = model.predict(new_product)

print("\n" + "=" * 50)
print("NEW PRODUCT PREDICTION")
print("=" * 50)

print(
    "Predicted Monthly Sales:",
    round(predicted_sales[0], 2)
)

# ==================================================
# CROSS VALIDATION
# ==================================================

scores = cross_val_score(
    model,
    X,
    y,
    cv=5,
    scoring="r2"
)

print("\n" + "=" * 50)
print("5-FOLD CROSS VALIDATION")
print("=" * 50)

print("Individual Scores:")

for score in scores:
    print(round(score, 4))

print(
    "\nAverage R² Score:",
    round(scores.mean(), 4)
)

# ==================================================
# FINAL REPORT
# ==================================================

print("\n" + "=" * 50)
print("FINAL REPORT")
print("=" * 50)

print("Algorithm Used : Linear Regression")
print("Training Records:", len(X_train))
print("Testing Records :", len(X_test))
print("R² Score        :", round(r2, 4))
print("MAE             :", round(mae, 2))
print("MSE             :", round(mse, 2))
print("RMSE            :", round(rmse, 2))
print("Cross Val Mean  :", round(scores.mean(), 4))





INTERPRETATION

the output section by section :


1. DATASET

Output:

==================================================
DATASET
==================================================

   MarketingSpend  WebsiteVisits  DiscountPercent  CustomerReviews  MonthlySales
0            5000           1200                5               45         15000
1            5500           1250                5               48         15800
2            6000           1300                6               50         16500
...

Meaning

This is your training data.

Each row represents one product/business scenario.

Example:

MarketingSpend = 5000
WebsiteVisits = 1200
DiscountPercent = 5
CustomerReviews = 45

MonthlySales = 15000

Interpretation:

Company spent ₹5000 on marketing,
got 1200 website visits,
offered 5% discount,
received 45 reviews,

and generated ₹15000 sales.

2. Model Trained Successfully!

Output:

Model Trained Successfully!

Meaning

This line executes:

model.fit(X_train, y_train)

The model learns relationships like:

Higher marketing spend
→ Higher sales

More reviews
→ Higher sales

More visits
→ Higher sales

The model creates a mathematical equation.


3. MODEL EVALUATION

Example:

R² Score : 0.98
MAE      : 450.25
MSE      : 350000
RMSE     : 591.60

4. R² Score

Example:

R² Score : 0.98

R² measures:

How well the model explains sales.

Range:

0 → Bad

1 → Perfect

Interpretation:

0.98 means

98% of sales variation
is explained by the model.

Very good model.


5. MAE (Mean Absolute Error)

Example:

MAE : 450

Formula:

Average of all prediction errors

Example:

Actual sales:

15000
20000
25000

Predicted:

14500
20500
25200

Errors:

500
500
200

MAE:

(500+500+200)/3

= 400

Meaning:

Model is wrong by ₹400 on average.

6. MSE (Mean Squared Error)

Example:

MSE : 350000

Formula:

Square every error
then average.

Why?

Because big mistakes should be punished more.

Example:

Errors:

100
100
1000

MSE becomes large because:

1000² = 1,000,000

Large errors affect MSE heavily.


7. RMSE

Example:

RMSE : 591.60

Formula:

RMSE = √MSE

Meaning:

Average prediction error
in sales units.

Interpretation:

Predictions are off by
approximately ₹592.

RMSE is usually easier to understand than MSE.


8. ACTUAL VS PREDICTED

Output:

Actual Sales    Predicted Sales

15000           15250
18500           18100
25000           24850

Meaning

Actual:

Real sales value

Predicted:

Model's guess

Difference:

Prediction Error

Example:

Actual = 15000

Predicted = 15250

Error = 250

9. FEATURE COEFFICIENTS

Example:

MarketingSpend : 1.85
WebsiteVisits : 2.10
DiscountPercent : 115
CustomerReviews : 92

These are the most important outputs.


MarketingSpend Coefficient

1.85

Meaning:

For every ₹1 increase
in marketing spend,

sales increase by ₹1.85

assuming other variables remain unchanged.


WebsiteVisits Coefficient

2.10

Meaning:

One extra website visit

increases sales by ₹2.10

DiscountPercent Coefficient

115

Meaning:

Increasing discount
by 1%

increases sales
by ₹115

CustomerReviews Coefficient

92

Meaning:

One additional review

increases sales
by ₹92

10. Intercept

Example:

Intercept: 3500

The model equation becomes:

Sales

=
3500
+
(1.85 × MarketingSpend)

+
(2.10 × WebsiteVisits)

+
(115 × DiscountPercent)

+
(92 × CustomerReviews)

The intercept is the starting value when all inputs are zero.


11. NEW PRODUCT PREDICTION

Input:

new_product = [
    15000,
    2800,
    20,
    100
]

Meaning:

Marketing Spend = ₹15000

Website Visits = 2800

Discount = 20%

Reviews = 100

Output:

Predicted Monthly Sales:
34000

Meaning:

The model expects

₹34,000 sales

for this product.


12. CROSS VALIDATION

Example:

Individual Scores

0.97
0.98
0.96
0.99
0.98

Meaning

Dataset is divided into:

5 parts

Model trains:

5 times

Every time:

Different test data

is used.

This checks:

Can the model perform
well on unseen data?

13. Average Cross Validation Score

Example:

Average R² Score: 0.976

Meaning:

Average performance
across all 5 experiments.

Interpretation:

97.6%

Excellent model

14. FINAL REPORT

Example:

Algorithm Used : Linear Regression

Training Records : 16

Testing Records : 4

R² Score : 0.98

MAE : 450

MSE : 350000

RMSE : 591

Cross Val Mean : 0.976


Meaning

MetricMeaning
Training Records            Data used to learn
Testing RecordsData used to evaluate
Model quality
MAEAverage error
MSESquared error
RMSEReal-world error
Cross ValidationReliability of model

Most important thing to remember

For Linear Regression projects, the outputs you should always analyze are:

  1. R² Score → How good is the model?

  2. MAE → Average error.

  3. RMSE → Practical prediction error.

  4. Coefficients → Which features affect the target most?

  5. Actual vs Predicted → How close predictions are to reality.

  6. Cross Validation Score → Can the model generalize to new data?










For Logistic Regression


these are the main evaluation tools you used:


1. Accuracy

What it checks?

Accuracy checks:

Out of all predictions, how many were correct?

Formula

[
Accuracy = \frac{Correct Predictions}{Total Predictions}
]

Example

Suppose:

Total Customers = 100

Correct Predictions = 95

Then:

Accuracy = 95/100 = 0.95 = 95%

Simple Meaning

The model gives correct answers 95 times out of 100.


2. Precision

What it checks?

Precision checks:

When the model says "Customer will purchase", how often is it correct?

Formula

[
Precision = \frac{TP}{TP + FP}
]

Example

Model predicted:

20 customers will purchase

Actually:

18 purchased
2 did not purchase

Then:

Precision = 18/20 = 90%

Simple Meaning

If the model predicts a purchase, how much can we trust that prediction?


3. Recall

What it checks?

Recall checks:

Out of all actual buyers, how many did the model find?

Formula


Recall =             TP

                    _________

                     TP + FN

Example

Actual buyers:

30 customers

Model identified:

27 buyers

Then:

Recall = 27/30 = 90%

Simple Meaning

How good is the model at finding all customers who will purchase?


4. Confusion Matrix

What it checks?

It shows every prediction in a table.

Example:

             Predicted
            No     Yes

Actual No   8      1

Actual Yes  2      9

True Positive (TP)

Model predicted:

Purchase

Customer actually:

Purchased

Correct positive prediction.

Example:

Predicted = 1
Actual = 1

True Negative (TN)

Model predicted:

No Purchase

Customer actually:

Did Not Purchase

Correct negative prediction.

Example:

Predicted = 0
Actual = 0

False Positive (FP)

Model predicted:

Purchase

Customer actually:

Did Not Purchase

Wrong prediction.

Example:

Predicted = 1
Actual = 0

Also called:

False Alarm

False Negative (FN)

Model predicted:

No Purchase

Customer actually:

Purchased

Wrong prediction.

Example:

Predicted = 0
Actual = 1

Visual Understanding

ActualPredictedType
11TP
00TN
01FP
10FN

5. Coefficients

What do they check?

Coefficients tell:

Which feature affects the prediction and by how much.

Example:

Age = 0.10

Income = 0.0002

CartItems = 2.50

Interpretation

Positive coefficient:

Higher value
→ More chance of purchase

Negative coefficient:

Higher value
→ Less chance of purchase

6. Most Influential Feature

What does it check?

Find the feature with the largest coefficient.

Example:

Age = 0.10

Income = 0.0002

WebsiteVisits = 0.80

CartItems = 2.50

Largest coefficient:

CartItems = 2.50

Meaning

CartItems has the strongest impact on purchase prediction.


7. Prediction on New Customer

What does it check?

Tests the trained model on unseen data.

Example:

new_customer = [[30,45000,7,18,4]]

Output:

[1]

Meaning:

Customer is likely to purchase.

Easy Viva Summary

ToolPurpose
AccuracyOverall correctness
PrecisionHow reliable positive predictions are
RecallHow many actual buyers are found
Confusion MatrixShows TP, TN, FP, FN
TPCorrectly predicted buyer
TNCorrectly predicted non-buyer
FPPredicted buyer but didn't buy
FNPredicted non-buyer but actually bought
CoefficientsImpact of each feature
Most Influential FeatureStrongest feature affecting prediction
New PredictionPredicts outcome for new customer

One-line memory trick

Confusion Matrix → "What mistakes did the model make?"
Accuracy → "How many predictions were correct?"
Precision → "Can I trust a positive prediction?"
Recall → "Did I find all the actual positives?"
Coefficients → "Which feature matters most?"






customer data

CustomerID,Age,MonthlyIncome,WebsiteVisits,TimeSpentMinutes,CartItems,Purchased

1,22,25000,2,5,0,0

2,24,28000,3,7,1,0

3,26,30000,5,12,2,1

4,28,35000,6,15,3,1

5,30,40000,2,4,0,0

6,32,45000,7,18,4,1

7,35,50000,8,20,5,1

8,21,22000,1,3,0,0

9,27,32000,4,10,1,0

10,29,38000,6,14,3,1

11,31,42000,7,17,4,1

12,23,26000,2,6,0,0

13,25,29000,3,8,1,0

14,34,48000,8,21,5,1

15,36,52000,9,24,6,1

16,20,21000,1,2,0,0

17,22,24000,2,5,0,0

18,24,27000,3,7,1,0

19,26,31000,5,11,2,1

20,28,36000,6,15,3,1

21,30,41000,7,18,4,1

22,32,46000,8,20,5,1

23,34,50000,9,22,5,1

24,21,23000,1,4,0,0

25,23,25000,2,5,0,0

26,25,28000,3,8,1,0

27,27,33000,4,11,2,1

28,29,37000,5,13,2,1

29,31,43000,7,17,4,1

30,33,47000,8,20,5,1

31,35,51000,9,23,6,1

32,20,20000,1,2,0,0

33,22,24000,2,4,0,0

34,24,27000,3,7,1,0

35,26,30000,4,10,2,1

36,28,35000,5,12,2,1

37,30,40000,6,15,3,1

38,32,45000,7,18,4,1

39,34,50000,8,21,5,1

40,36,55000,10,25,7,1

41,21,22000,1,3,0,0

42,23,26000,2,6,0,0

43,25,29000,3,8,1,0

44,27,34000,4,10,2,1

45,29,39000,5,13,3,1

46,31,43000,6,16,4,1

47,33,47000,8,20,5,1

48,35,52000,9,24,6,1

49,20,21000,1,2,0,0

50,22,25000,2,5,0,0

51,24,28000,3,7,1,0

52,26,32000,4,10,2,1

53,28,36000,5,12,2,1

54,30,41000,6,15,3,1

55,32,46000,7,18,4,1

56,34,50000,8,21,5,1

57,36,56000,10,26,7,1

58,21,23000,1,4,0,0

59,23,26000,2,5,0,0

60,25,30000,3,8,1,0

61,27,34000,4,11,2,1

62,29,39000,5,13,3,1

63,31,44000,7,17,4,1

64,33,48000,8,20,5,1

65,35,53000,9,24,6,1

66,20,20000,1,2,0,0

67,22,24000,2,4,0,0

68,24,27000,3,7,1,0

69,26,31000,4,10,2,1

70,28,35000,5,12,2,1

71,30,40000,6,15,3,1

72,32,45000,7,18,4,1

73,34,50000,8,21,5,1

74,36,55000,10,25,7,1

75,21,22000,1,3,0,0

76,23,25000,2,5,0,0

77,25,29000,3,8,1,0

78,27,33000,4,10,2,1

79,29,38000,5,13,3,1

80,31,42000,6,16,4,1

81,33,47000,8,20,5,1

82,35,52000,9,24,6,1

83,20,21000,1,2,0,0

84,22,24000,2,4,0,0

85,24,28000,3,7,1,0

86,26,32000,4,10,2,1

87,28,36000,5,12,2,1

88,30,41000,6,15,3,1

89,32,46000,7,18,4,1

90,34,50000,8,21,5,1

91,36,56000,10,26,7,1

92,21,23000,1,4,0,0

93,23,26000,2,5,0,0

94,25,30000,3,8,1,0

95,27,34000,4,11,2,1

96,29,39000,5,13,3,1

97,31,44000,7,17,4,1

98,33,48000,8,20,5,1

99,35,53000,9,24,6,1

100,37,58000,10,28,8,1





Dataset:

Customer Purchase Prediction Dataset

Target Column:

Purchased

0 = Customer did not purchase
1 = Customer purchased

Assignment 1

Load the Customer Purchase Prediction dataset and train a Logistic Regression model using all available input features to predict whether a customer will purchase a product.

import pandas as pd

from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression

# Load Dataset
df = pd.read_csv("customer_data.csv")

# Features
X = df[
    [
        "Age",
        "MonthlyIncome",
        "WebsiteVisits",
        "TimeSpentMinutes",
        "CartItems"
    ]
]

# Target
y = df["Purchased"]

# 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 = LogisticRegression(max_iter=1000)

# Train Model
model.fit(X_train, y_train)

print("Model Trained Successfully")

Interpretation

The Logistic Regression model learns patterns from customer information to predict whether a customer will purchase a product.





Assignment 2

Use the trained Logistic Regression model to predict purchase decisions for all records in the test dataset.


y_pred = model.predict(X_test)

print(y_pred)




Assignment 3

Calculate the accuracy of the Logistic Regression model and explain what the accuracy value indicates about the model's performance.

from sklearn.metrics import accuracy_score
accuracy = accuracy_score(
    y_test,
    y_pred
)

print("Accuracy:", accuracy)

Formula


Accuracy =                 TP + TN    

                            ________________

                            TP + TN + FP + FN

Expected Result

Accuracy ≈ 1.0

Interpretation

The model correctly predicts nearly all customer purchase decisions.

If accuracy is 1.0:

The model correctly classified 100% of the customers in the test dataset.




Assignment 4

Calculate the precision score of the Logistic Regression model and explain its importance in customer purchase prediction.

from sklearn.metrics import precision_score

precision = precision_score(
    y_test,
    y_pred
)

print("Precision:", precision)

Precision =                 TP

                            __________

                               TP + FP


Interpretation

When the model predicts that a customer will purchase, it is almost always correct.




Assignment 5

Calculate the recall score of the Logistic Regression model and explain its significance.

from sklearn.metrics import recall_score
recall = recall_score(
    y_test,
    y_pred
)

print("Recall:", recall)

Formula

                                TP   

                        ___________

                            TP + FN

Expected Result

Recall ≈ 1.0

Interpretation

The model successfully identifies almost all customers who actually purchase.



Assignment 7

Generate the confusion matrix for the Logistic Regression model and identify the values of True Positives, True Negatives, False Positives, and False Negatives.

from sklearn.metrics import confusion_matrix

cm = confusion_matrix(
    y_test,
    y_pred
)

print(cm)

Expected Output

[[8 0]
 [0 12]]


Interpretation

TN = 8
FP = 0
FN = 0
TP = 12
  • True Negatives = 8

  • False Positives = 0

  • False Negatives = 0

  • True Positives = 12

The model makes no prediction errors.

Assignment 8

Display all coefficients learned by the Logistic Regression model and explain how each feature influences the purchasing decision.


for feature, coef in zip(
        X.columns,
        model.coef_[0]
):
    print(feature, ":", coef)

print("Intercept:", model.intercept_[0])


Example Output

Age : 0.12
MonthlyIncome : 0.0003
WebsiteVisits : 0.85
TimeSpentMinutes : 0.90
CartItems : 2.45

Intercept : -12.34


Interpretation
FeatureMeaning
Age        Higher age slightly increases purchase chance
MonthlyIncome        Higher income increases purchase chance
WebsiteVisits        More visits increase purchase chance
TimeSpentMinutes        More time spent increases purchase chance
CartItems        Strongly increases purchase chance

Positive coefficient:

Feature increases
→ Purchase probability increases

Negative coefficient:

Feature increases
→ Purchase probability decreases

Assignment 9

Identify the most influential feature in the Logistic Regression model and justify your answer using the coefficient values.

Example:

Age : 0.12

MonthlyIncome : 0.0003

WebsiteVisits : 0.85

TimeSpentMinutes : 0.90

CartItems : 2.45

Largest coefficient:

CartItems : 2.45

Interpretation

CartItems is the most influential feature because it has the highest coefficient value.

Customers with more items in their cart are much more likely to purchase.




Assignment 10

Create a new customer record and use the Logistic Regression model to predict whether the customer is likely to purchase the product.


new_customer = [[
    30,      # Age
    45000,   # MonthlyIncome
    7,       # WebsiteVisits
    18,      # TimeSpentMinutes
    4        # CartItems
]]

prediction = model.predict(
    new_customer
)

print(prediction)

Expected Output

[1]

Interpretation

1 = Customer likely to purchase

The model predicts that this customer will buy the product because they have:

  • Good income

  • Multiple website visits

  • High engagement time

  • Several items in the cart





 



Decision Tree

Assignment 11

Train a Decision Tree Classifier using the Customer Purchase Prediction dataset and evaluate its performance.


Assignment 12

Calculate the accuracy of the Decision Tree model and compare it with the accuracy of the Logistic Regression model.


Assignment 13

Generate a confusion matrix for the Decision Tree model and analyze the prediction errors.


Assignment 14

Train a Decision Tree model with a maximum depth of 2 and observe how limiting tree depth affects prediction accuracy.


Assignment 15

Train another Decision Tree model with a maximum depth of 5 and compare its performance with the previous model.


Assignment 16

Compare the performance of Decision Tree models trained with maximum depths of 2, 5, and unlimited depth.


Assignment 17

Determine the optimal tree depth that produces the best prediction performance for this dataset.


Assignment 18

Visualize the complete Decision Tree and explain the meaning of each decision node.


Assignment 19

Extract and display feature importance values from the Decision Tree model and identify the most important feature.


Assignment 20

Use the trained Decision Tree model to predict the purchasing behavior of a new customer.

0 Comments