Regression in Data Analysis

Learn how to analyze relationships between variables, build regression models, make predictions, measure model performance, and solve real-world business problems using Python.

What is Regression?

Regression is a statistical and machine learning technique used to understand the relationship between a dependent variable and one or more independent variables. It is commonly used to predict numerical values.

Independent Variable (X)

The input or feature used to explain or predict another variable.

Dependent Variable (Y)

The numerical value that we want to understand or predict.

Regression Model

A mathematical model that estimates the relationship between X and Y.

Prediction

The estimated value produced by the regression model for new data.

1. Regression Analysis Workflow

A typical regression analysis follows these steps:

Collect Data
→
Clean Data
→
Select Features
→
Build Model
→
Predict
→
Evaluate

For example, an online real-estate company can use regression to estimate the price of a house from its area, number of bedrooms, age, and location.

2. Regression vs Classification

Regression and classification are both supervised learning techniques, but they solve different types of problems.

Regression Classification
Predicts numerical values Predicts categories
House price Spam or Not Spam
Monthly sales Customer will churn or not
Employee salary Approved or Rejected
Simple rule: If the output you want to predict is a continuous numerical value, regression is often an appropriate starting point.
3. Types of Regression
  • Simple Linear Regression: One input variable predicts one numerical output.
  • Multiple Linear Regression: Multiple input variables predict one numerical output.
  • Polynomial Regression: Models curved relationships between variables.
  • Ridge Regression: Linear regression with L2 regularization.
  • Lasso Regression: Linear regression with L1 regularization.
  • Elastic Net: Combines L1 and L2 regularization.
1
Simple Linear Input
N
Multiple Inputs
X²
Polynomial Relationship
L1/L2
Regularization
4. Installing Required Libraries

Pandas and NumPy are useful for data preparation, while Matplotlib and scikit-learn are commonly used for regression analysis.

Terminal
pip install pandas numpy matplotlib scikit-learn
5. Importing Regression Libraries
Python
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_absolute_error, mean_squared_error, r2_score
6. Creating a Real-Time House Price Dataset

Suppose a real-estate company wants to estimate house prices based on the size of the property.

Python
data = {
    "area_sqft": [800, 1000, 1200, 1400, 1600, 1800, 2000, 2200],
    "price_lakh": [42, 50, 58, 67, 76, 85, 94, 103]
}

df = pd.DataFrame(data)

print(df)
area_sqft price_lakh 0 800 42 1 1000 50 2 1200 58 3 1400 67 4 1600 76 5 1800 85 6 2000 94 7 2200 103
7. Understanding X and Y

In this example:

  • X = area_sqft → input variable
  • Y = price_lakh → target variable
House Price = f(Area)
Python
X = df[["area_sqft"]]
y = df["price_lakh"]

print(X)
print(y)
8. Visualizing the Relationship

Before building a regression model, it is useful to understand whether the input and target appear to have a relationship.

Python
plt.scatter(df["area_sqft"], df["price_lakh"])

plt.xlabel("Area (sq ft)")
plt.ylabel("Price (Lakh)")
plt.title("House Area vs House Price")

plt.show()
If the points show an approximately straight-line pattern, linear regression may be a useful model to investigate.
9. Simple Linear Regression Formula

Simple linear regression models the relationship between one input variable and one numerical output.

ŷ = b₀ + b₁x
  • ŷ = predicted value
  • b₀ = intercept
  • b₁ = slope
  • x = input variable

The model attempts to find the line that best represents the observed data.

10. Splitting Training and Testing Data

A model should normally be evaluated on data that was not used to train it.

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

print("Training rows:", len(X_train))
print("Testing rows:", len(X_test))
Training data is used to learn the model. Testing data is used to evaluate how the model performs on unseen observations.
11. Creating a Linear Regression Model
Python
model = LinearRegression()

model.fit(X_train, y_train)

print("Model trained successfully")
Model trained successfully
12. Understanding the Model Coefficients
Python
print("Intercept:", model.intercept_)
print("Coefficient:", model.coef_[0])

The coefficient represents the estimated change in the target value for a one-unit increase in the input variable, under the model.

13. Making Predictions
Python
predictions = model.predict(X_test)

print(predictions)
The prediction is the estimated house price generated by the trained regression model.
14. Predicting the Price of a New House

Imagine that a customer asks for an estimated price for a 2,500 square-foot house.

Python
new_house = pd.DataFrame({
    "area_sqft": [2500]
})

predicted_price = model.predict(new_house)

print("Predicted price:", predicted_price[0], "lakh")
Predicted price: approximately 115 lakh

The exact prediction depends on the coefficients learned from the training data.

15. Comparing Actual and Predicted Values
Python
comparison = pd.DataFrame({
    "Actual": y_test.values,
    "Predicted": predictions
})

print(comparison)

Comparing actual and predicted values helps analysts understand where the model is accurate and where it makes larger errors.

16. Mean Absolute Error (MAE)

Mean Absolute Error measures the average absolute difference between actual and predicted values.

MAE = Average(|Actual − Predicted|)
Python
mae = mean_absolute_error(y_test, predictions)

print("MAE:", mae)
A smaller MAE means the predictions are, on average, closer to the actual values.
17. Mean Squared Error (MSE)

MSE calculates the average squared prediction error. Squaring makes larger errors contribute more strongly.

MSE = Average((Actual − Predicted)²)
Python
mse = mean_squared_error(y_test, predictions)

print("MSE:", mse)
18. Root Mean Squared Error (RMSE)

RMSE is the square root of MSE and expresses the error in the same units as the target variable.

RMSE = √MSE
Python
rmse = np.sqrt(mean_squared_error(y_test, predictions))

print("RMSE:", rmse)
19. R² Score

R², or the coefficient of determination, describes how much of the variation in the target is explained by the regression model.

R² = 1 − (Residual Sum of Squares / Total Sum of Squares)
Python
r2 = r2_score(y_test, predictions)

print("R² Score:", r2)
R² should be interpreted in context. A higher R² does not automatically mean that a model is appropriate for every business problem.
20. Complete Evaluation Example
Python
mae = mean_absolute_error(y_test, predictions)
mse = mean_squared_error(y_test, predictions)
rmse = np.sqrt(mse)
r2 = r2_score(y_test, predictions)

print("MAE :", mae)
print("MSE :", mse)
print("RMSE:", rmse)
print("R²  :", r2)
MAE
Average absolute error
MSE
Squared error
RMSE
Error in target units
R²
Explained variation
21. Regression Line Visualization
Python
plt.scatter(X, y, label="Actual Data")

plt.plot(
    X,
    model.predict(X),
    label="Regression Line"
)

plt.xlabel("Area (sq ft)")
plt.ylabel("Price (Lakh)")
plt.title("Linear Regression")
plt.legend()

plt.show()

The scatter points represent observed data, while the line represents the values predicted by the regression model.

22. Multiple Linear Regression

In real-world data analysis, one variable is often not enough. House price may depend on area, bedrooms, house age, and parking.

Python
data = {
    "area": [800, 1000, 1200, 1400, 1600, 1800, 2000, 2200],
    "bedrooms": [2, 2, 3, 3, 3, 4, 4, 4],
    "age": [20, 15, 12, 10, 8, 6, 4, 2],
    "price": [42, 50, 58, 67, 76, 85, 94, 103]
}

df = pd.DataFrame(data)

X = df[["area", "bedrooms", "age"]]
y = df["price"]

model = LinearRegression()
model.fit(X, y)

print("Coefficients:", model.coef_)
print("Intercept:", model.intercept_)
23. Predicting with Multiple Features

A new property can be described using several characteristics.

Python
new_house = pd.DataFrame({
    "area": [1800],
    "bedrooms": [3],
    "age": [5]
})

predicted_price = model.predict(new_house)

print("Estimated price:", predicted_price[0], "lakh")
Multiple regression allows the model to use several measurable factors simultaneously.
24. Real-Time Example: Sales Prediction

An e-commerce company wants to estimate monthly sales based on advertising spending.

Python
data = {
    "advertising": [10, 15, 20, 25, 30, 35, 40],
    "sales": [110, 140, 170, 205, 235, 270, 300]
}

df = pd.DataFrame(data)

X = df[["advertising"]]
y = df["sales"]

model = LinearRegression()
model.fit(X, y)

new_campaign = pd.DataFrame({
    "advertising": [45]
})

predicted_sales = model.predict(new_campaign)

print("Predicted sales:", predicted_sales[0])
Predicted sales: approximately 330 units

This type of model can help analysts study the relationship between advertising expenditure and sales.

25. Real-Time Example: Employee Salary Analysis

A company can analyze whether years of experience are associated with employee salary.

Python
data = {
    "experience": [1, 2, 3, 4, 5, 7, 9, 11],
    "salary": [3.2, 3.8, 4.5, 5.2, 6.1, 7.8, 9.5, 11.2]
}

df = pd.DataFrame(data)

X = df[["experience"]]
y = df["salary"]

model = LinearRegression()
model.fit(X, y)

new_employee = pd.DataFrame({
    "experience": [6]
})

salary = model.predict(new_employee)

print("Estimated salary:", salary[0], "LPA")
Regression describes relationships in the available data; a predicted salary should not be treated as a guaranteed compensation decision.
26. Real-Time Example: Website Traffic

A digital marketing team wants to estimate website visits based on advertising expenditure.

Python
data = {
    "ad_spend": [5, 10, 15, 20, 25, 30],
    "visits": [1200, 2100, 3000, 3900, 4850, 5700]
}

df = pd.DataFrame(data)

X = df[["ad_spend"]]
y = df["visits"]

model = LinearRegression()
model.fit(X, y)

next_month = pd.DataFrame({
    "ad_spend": [35]
})

predicted_visits = model.predict(next_month)

print("Predicted visits:", predicted_visits[0])
27. Real-Time Example: Electricity Consumption

A utility company may analyze electricity consumption based on temperature, household size, or historical usage.

Python
data = {
    "temperature": [20, 22, 24, 26, 28, 30, 32],
    "usage_kwh": [180, 190, 205, 225, 250, 280, 315]
}

df = pd.DataFrame(data)

X = df[["temperature"]]
y = df["usage_kwh"]

model = LinearRegression()
model.fit(X, y)

tomorrow = pd.DataFrame({
    "temperature": [31]
})

usage = model.predict(tomorrow)

print("Estimated electricity usage:", usage[0], "kWh")
28. Residuals

A residual is the difference between an observed value and its predicted value.

Residual = Actual − Predicted
Python
residuals = y_test - predictions

print(residuals)

Analysts can inspect residuals to identify patterns that may indicate that a simple regression model does not adequately capture the data.

29. Residual Plot
Python
plt.scatter(predictions, residuals)

plt.axhline(y=0, linestyle="--")

plt.xlabel("Predicted Values")
plt.ylabel("Residuals")
plt.title("Residual Plot")

plt.show()
Residual plots are useful diagnostic tools. Patterns in the residuals can indicate issues such as non-linearity or changing variance.
30. Polynomial Regression

Not every relationship is a straight line. Polynomial regression can represent certain curved relationships.

Python
from sklearn.preprocessing import PolynomialFeatures
from sklearn.pipeline import make_pipeline

model = make_pipeline(
    PolynomialFeatures(degree=2),
    LinearRegression()
)

model.fit(X, y)

predictions = model.predict(X)

print(predictions)
Increasing polynomial degree can make a model more flexible, but excessive flexibility can lead to overfitting.
31. Multiple Regression with a CSV File

In a real project, data will usually come from files or databases rather than being manually entered.

Python
df = pd.read_csv("house_prices.csv")

X = df[["area", "bedrooms", "age"]]
y = df["price"]

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

model = LinearRegression()
model.fit(X_train, y_train)

predictions = model.predict(X_test)

print("R²:", r2_score(y_test, predictions))
32. Correlation Before Regression

Correlation can help analysts explore whether numerical variables move together, although correlation alone does not establish causation.

Python
print(df.corr(numeric_only=True))
Correlation is an exploratory statistic. Regression adds a predictive model and provides coefficients and prediction errors.
33. Regression and Feature Selection

Real datasets may contain many columns. Analysts should select variables that are relevant to the question being studied.

House Feature Potential Use
Area Useful numerical predictor
Bedrooms Useful property characteristic
Age Potential predictor
House ID Usually an identifier rather than a useful predictor
Customer Name Not directly usable as a numerical regression feature
34. Handling Categorical Variables

Real datasets often contain categories such as city, property type, or product category. These values need appropriate encoding before many regression algorithms can use them.

Python
df = pd.get_dummies(
    df,
    columns=["city"],
    drop_first=True
)

print(df.head())

One-hot encoding converts categorical values into numerical indicator columns.

35. Real-Time Example: Product Demand Prediction

A retail company can build a regression model to estimate product demand using historical sales, discount percentage, advertising expenditure, and product price.

Python
data = {
    "price": [100, 95, 90, 85, 80, 75],
    "discount": [0, 5, 10, 15, 20, 25],
    "ad_spend": [5, 8, 10, 12, 15, 18],
    "demand": [80, 95, 110, 130, 155, 180]
}

df = pd.DataFrame(data)

X = df[["price", "discount", "ad_spend"]]
y = df["demand"]

model = LinearRegression()
model.fit(X, y)

new_product = pd.DataFrame({
    "price": [78],
    "discount": [20],
    "ad_spend": [16]
})

prediction = model.predict(new_product)

print("Estimated demand:", prediction[0])
36. Ridge Regression

Ridge regression adds L2 regularization to the linear regression objective. It can be useful when predictors are highly correlated or when controlling coefficient magnitude is desirable.

Python
from sklearn.linear_model import Ridge

model = Ridge(alpha=1.0)

model.fit(X_train, y_train)

predictions = model.predict(X_test)

print("R²:", r2_score(y_test, predictions))
37. Lasso Regression

Lasso regression uses L1 regularization. Under suitable conditions, it can shrink some coefficients to zero, which can be useful for feature selection.

Python
from sklearn.linear_model import Lasso

model = Lasso(alpha=0.1)

model.fit(X_train, y_train)

predictions = model.predict(X_test)

print("R²:", r2_score(y_test, predictions))
38. Train-Test Evaluation

A regression model should be evaluated on data that represents the situation in which the model will actually be used.

Full Dataset
→
Train Set
+
Test Set
→
Evaluation
Evaluating a model only on the same observations used for training can give an overly optimistic picture of its performance.
39. Cross-Validation

Cross-validation repeatedly divides available training data into training and validation portions to provide a more robust estimate of model performance.

Python
from sklearn.model_selection import cross_val_score

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

print("R² scores:", scores)
print("Average R²:", scores.mean())
40. Real-Time Example: Customer Spending Prediction

An online store wants to estimate annual customer spending from the number of purchases, average order value, and website visits.

Python
data = {
    "purchases": [5, 8, 10, 12, 15, 18, 20],
    "avg_order": [800, 750, 900, 850, 950, 1000, 1050],
    "visits": [20, 30, 35, 42, 50, 58, 65],
    "annual_spending": [4000, 6000, 9000, 10500, 14500, 18500, 22000]
}

df = pd.DataFrame(data)

X = df[["purchases", "avg_order", "visits"]]
y = df["annual_spending"]

model = LinearRegression()
model.fit(X, y)

customer = pd.DataFrame({
    "purchases": [16],
    "avg_order": [980],
    "visits": [55]
})

spending = model.predict(customer)

print("Estimated annual spending:", spending[0])
41. Regression for Business Analysis
Business Area Target Possible Predictors
Real Estate House Price Area, bedrooms, age, location
Retail Sales Price, discount, advertising
Finance Revenue Customers, transactions, product mix
Energy Consumption Temperature, occupancy, historical usage
Marketing Leads Ad spend, impressions, clicks
Human Resources Salary Experience, role, education
42. Complete Real-Time Sales Regression Project

The following example combines data preparation, model training, prediction, and evaluation into one workflow.

Complete Python Example
import pandas as pd
import numpy as np

from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_absolute_error, mean_squared_error, r2_score

# 1. Create business data
data = {
    "ad_spend": [10, 15, 20, 25, 30, 35, 40, 45, 50, 55],
    "discount": [2, 3, 5, 5, 7, 8, 10, 10, 12, 15],
    "website_visits": [1000, 1200, 1450, 1600, 1850,
                       2100, 2350, 2600, 2850, 3100],
    "sales": [100, 125, 150, 170, 195, 220, 250, 275, 305, 330]
}

df = pd.DataFrame(data)

# 2. Select features and target
X = df[["ad_spend", "discount", "website_visits"]]
y = df["sales"]

# 3. Split the data
X_train, X_test, y_train, y_test = train_test_split(
    X,
    y,
    test_size=0.2,
    random_state=42
)

# 4. Create the model
model = LinearRegression()

# 5. Train the model
model.fit(X_train, y_train)

# 6. Make predictions
predictions = model.predict(X_test)

# 7. Evaluate
mae = mean_absolute_error(y_test, predictions)
mse = mean_squared_error(y_test, predictions)
rmse = np.sqrt(mse)
r2 = r2_score(y_test, predictions)

print("MAE :", mae)
print("MSE :", mse)
print("RMSE:", rmse)
print("R²  :", r2)

# 8. Predict future sales
future_data = pd.DataFrame({
    "ad_spend": [60],
    "discount": [15],
    "website_visits": [3400]
})

future_sales = model.predict(future_data)

print("Predicted future sales:", future_sales[0])
This workflow can be adapted to real business datasets after appropriate data cleaning, feature engineering, validation, and domain review.
43. Regression Analysis Checklist
  1. Understand the business question.
  2. Identify the numerical target variable.
  3. Collect relevant historical data.
  4. Inspect and clean missing or invalid values.
  5. Explore relationships between variables.
  6. Select appropriate features.
  7. Split data into training and testing sets.
  8. Train the regression model.
  9. Generate predictions.
  10. Measure MAE, MSE, RMSE, or R² as appropriate.
  11. Inspect residuals and model assumptions.
  12. Validate the model on suitable unseen data.
  13. Interpret results in the context of the business problem.
44. Common Regression Problems
  • Overfitting: The model captures noise instead of general patterns.
  • Underfitting: The model is too simple to capture important patterns.
  • Outliers: Extreme observations can strongly affect some regression models.
  • Multicollinearity: Predictors may contain highly overlapping information.
  • Data leakage: Information unavailable at prediction time may accidentally enter the model.
  • Non-linearity: A straight-line model may not represent the actual relationship.
  • Changing variance: Prediction errors may behave differently across the range of predictions.
A good regression workflow evaluates both numerical performance and whether the model makes sense for the real-world problem.
45. Important Regression Metrics
Metric Meaning Typical Interpretation
MAE Average absolute prediction error Lower is generally better
MSE Average squared prediction error Lower is generally better
RMSE Square root of MSE Lower is generally better
R² Variation explained by the model Interpret relative to the dataset and task
46. Regression Best Practices
  • Understand the target variable before selecting a model.
  • Clean missing, duplicate, and invalid records.
  • Inspect distributions and relationships visually.
  • Keep training and evaluation data separate.
  • Check for data leakage.
  • Use cross-validation when appropriate.
  • Compare suitable evaluation metrics.
  • Inspect residuals and model diagnostics.
  • Consider regularization when appropriate.
  • Do not interpret correlation as proof of causation.
  • Use domain knowledge when interpreting coefficients and predictions.
  • Monitor model performance when deployed on changing data.
47. Complete Regression Project Structure
Raw Data
→
Data Cleaning
→
Feature Engineering
→
Regression Model
→
Predictions
→
Evaluation

Example Project

Project: Predict monthly product sales for an e-commerce company.

Features: advertising spend, product price, discount, website traffic, number of promotions, and previous sales.

Target: Monthly sales quantity or revenue.

Business Use: Support inventory planning, campaign analysis, and sales forecasting.

48. Important Regression Rules
  • Regression is primarily used for predicting or explaining numerical outcomes.
  • Always identify the target variable clearly.
  • Separate training data from evaluation data.
  • Use multiple evaluation metrics when appropriate.
  • Inspect residuals and unusual observations.
  • Do not assume that a high R² proves a model is useful.
  • Correlation does not by itself prove causation.
  • Choose features based on the prediction task and domain context.
  • Watch for data leakage and overfitting.
  • Validate the model using data representative of future use.
  • Interpret predictions together with business and domain knowledge.
Key idea: Regression turns historical numerical data into a model that can help analyze relationships and estimate future numerical outcomes.

Python Data Analysis - Regression

Learn regression concepts through practical Python and real-world examples.