Project 1: Introduction to Linear Regression

Predictive Modeling and Data Visualization with Python

Author

Dr. Wesley Francillon

Welcome to the Papillon Engineering K-12 Outreach Hub. This comprehensive module introduces students to data-driven engineering models through Simple Linear Regression, transitioning them from abstract algebraic line equations to active data science applications utilizing Python.

1. Module Overview & Learning Objectives

The purpose of this two-day module is to bridge the gap between standard high school algebra and professional engineering data analysis. Students will transition from solving standard y = mx + b problems on paper to utilizing modern Python libraries (numpy, pandas, plotly, and scikit-learn) to analyze real-world physical correlations.

Core Learning Objectives: * Mathematical Foundation: Define and interpret the slope (m) and y-intercept (b) in the context of real-world physical measurements (Height vs. Weight). * Data Visualization: Construct scatter plots and visualize linear trends using interactive Python graphics. * Algorithmic Calculation: Manually compute slope and intercept parameters using standard least-squares summation formulas. * Machine Learning Application: Implement professional industry frameworks to train a predictive regression model.


2. Materials & Prerequisites

  • Software Environment: Python 3.x installed with numpy, pandas, plotly, scikit-learn, and scipy packages.
  • Platform: Quarto-rendered web pages hosted via Papillon Engineering or local Jupyter Notebooks.
  • Prior Knowledge: Basic middle/high school algebra (equations of a line, PEMDAS) and elementary Python syntax (arrays and variable assignment).

3. Detailed Pacing Guide (120 Minutes Total)

Day 1: Theory, Data Inspection, and Visualization (60 Minutes)

  • Hook & Introduction (15 Mins): Engage students with real-world predictive questions. Do taller people systematically weigh more? How do engineers program autonomous systems to make predictions when sensor data has variability?
  • Mathematical Review (15 Mins): Introduce y = mx + b. Review what slope and intercept represent physically. Explain the concept of a “Line of Best Fit” minimizing error across a scattered dataset.
  • Loading Data & Pandas DataFrames (15 Mins): Guide students through initializing numpy arrays for height and weight. Explain the importance of structured data by formatting arrays into a tabular pandas.DataFrame.
  • Plotly Interactive Visualization (15 Mins): Execute the interactive scatter plot script. Teach students how to hover over data points, identify outliers, and visually confirm the positive correlation trend.

Day 2: Manual Calculations, Predictions, & Machine Learning (60 Minutes)

  • Least-Squares Math Breakdown (20 Mins): Walk through the algorithmic logic for calculating slope (m) and intercept (b). Emphasize that computers are simply executing algebraic summations extremely quickly.
  • Making Predictions (15 Mins): Use the derived linear equation to predict a target height. Instructor Prompt: Have students change the target height variable to their own height to predict their weight.
  • Industry Tools: Scikit-Learn & Scipy (15 Mins): Demonstrate how professional data scientists bypass manual algebraic code using linear_model.LinearRegression(). Discuss the concept of model “training” (fit).
  • Wrap-Up & Assessment (10 Mins): Review key takeaways. Assess student understanding by asking them to compare the manual calculation outputs with the scikit-learn outputs.

4. Assessment Strategies & Answer Key

  • Formative Check: Verify that students correctly reshape arrays (train_x = x.reshape(-1, 1)) when implementing scikit-learn. Explain that ML models expect 2D feature matrices, not 1D arrays.
  • Expected Output Reference:
    • Calculated Slope (m): 7.96
    • Calculated Intercept (b): -367.61
    • Predicted Weight for 69-inch height: 181.8 lbs

Introduction: We Are Learning About Models

Have you ever wondered if we can predict physical characteristics using math? In this module, our goal is to determine if there is a mathematical correlation between a person’s height and their weight. Generally, the taller someone is, the more likely they are to weigh more.

As engineers and data scientists, we can use Simple Linear Regression to build a mathematical model that accurately predicts a person’s weight for a given height based on historical data.


Step 1: Understanding the Data & Mathematical Foundations

In algebra, the equation of a straight line is written as:

y = mx + b

  • y: The dependent variable we want to predict (Weight in lbs).
  • x: The independent variable we already know (Height in inches).
  • m: The slope of the line (the rate of change, or how steep the line is).
  • b: The y-intercept (the starting value of y when x = 0).

Let’s start by loading our sample data into Python using the numpy (Numerical Python) and pandas (Data Analysis) libraries.

Code
import numpy as np
import pandas as pd

# x represents Heights (in inches)
x = np.array([66, 68, 60, 70, 65, 61, 74, 73, 75, 67])

# y represents Weights (in pounds)
y = np.array([160, 170, 110, 178, 155, 120, 223, 215, 235, 164])

# Display the data as a clean engineering table
data_table = pd.DataFrame({'Height (x)': x, 'Weight (y)': y})
data_table.head(10)
Height (x) Weight (y)
0 66 160
1 68 170
2 60 110
3 70 178
4 65 155
5 61 120
6 74 223
7 73 215
8 75 235
9 67 164

Step 2: Visualizing the Correlation

Before doing any complex math, engineers always plot their data to see if a relationship actually exists. We will use an interactive graphics library called plotly to visualize our height and weight data points.

Code
import plotly.express as px

# Create an interactive scatter plot
fig = px.scatter(
    x=x, y=y, 
    title="Height vs. Weight Correlation Analysis",
    labels={'x': 'Height (inches)', 'y': 'Weight (lbs)'},
    color_discrete_sequence=['#1c528b']
)

fig.update_layout(
    plot_bgcolor='white', 
    xaxis=dict(showgrid=True, gridcolor='lightgrey'), 
    yaxis=dict(showgrid=True, gridcolor='lightgrey')
)
fig.show()
NoteObservation Check

Notice how the dots trend upward from left to right. This upward trajectory indicates a strong positive correlation between a person’s height and their weight.


Step 3: Calculating the Slope and Intercept Manually

To draw the perfect “Line of Best Fit” through these scattered data points, we need to calculate the exact slope (m) and intercept (b). We can program Python to do this using the Least-Squares summation formulas.

Code
# 1. Find the total number of data points
n = len(x)

# 2. Calculate the sums of x and y
sumx = np.sum(x)
sumy = np.sum(y)

# 3. Calculate product columns (x*y) and squared predictors (x^2)
multiplycolumxy = x * y
sum_multiplycolumxy = np.sum(multiplycolumxy)
x_squared = np.square(x)

# 4. Solve for the slope (m)
m = (n * sum_multiplycolumxy - sumx * sumy) / (n * np.sum(x_squared) - np.square(sumx))

# 5. Solve for the y-intercept (b)
b = (sumy * np.sum(x_squared) - sumx * sum_multiplycolumxy) / (n * np.sum(x_squared) - np.square(sumx))

print(f"Calculated Slope (m): {m:.2f}")
print(f"Calculated Intercept (b): {b:.2f}")
Calculated Slope (m): 7.96
Calculated Intercept (b): -367.61

Step 4: Making Real-World Predictions

Now that we have successfully solved for our line equation (y = 7.96x - 367.61), we can use it to make predictive estimations! Let’s predict the weight of someone who is 69 inches tall.

Code
# Predict weight for a target height of 69 inches
target_height = 69
predicted_weight = (m * target_height) + b

print(f"A person who is {target_height} inches tall is predicted to weigh {predicted_weight:.1f} lbs.")
A person who is 69 inches tall is predicted to weigh 181.8 lbs.

(Try changing the target_height variable in the code above to your own height in inches to see what the model predicts for you!)


Step 5: The Professional Way (Machine Learning Libraries)

In professional engineering and data science workflows, practitioners do not write out regression summation formulas line-by-line. Instead, we rely on robust Machine Learning libraries like scikit-learn or scientific modules like scipy to instantly train predictive models.

Here is how you accomplish the exact same prediction task using industry-standard tools:

Using Scikit-Learn

Code
from sklearn import linear_model

# Initialize the Machine Learning model
regr = linear_model.LinearRegression()

# Reshape the data (ML models require 2D arrays for features)
train_x = x.reshape(-1, 1)
train_y = y

# Train (fit) the model on our data
regr.fit(train_x, train_y)

print(f"Sklearn Slope: {regr.coef_[0]:.2f}")
print(f"Sklearn Intercept: {regr.intercept_:.2f}")
Sklearn Slope: 7.96
Sklearn Intercept: -367.61

Using Scipy (Numpy Polyfit)

Code
import scipy

# Calculate slope and intercept using polyfit (degree 1 for a straight line)
(mm, bb) = np.polyfit(x, y, 1)

print(f"Polyfit Slope: {mm:.2f}")
print(f"Polyfit Intercept: {bb:.2f}")
Polyfit Slope: 7.96
Polyfit Intercept: -367.61
TipProject Complete!

Notice that both professional ML tools returned the exact same slope (7.96) and intercept (-367.61) that we calculated manually from scratch. You have successfully built, verified, and implemented your first predictive machine learning model!