Understanding Linear Regression with Python (Study Notes)

As I continue learning Power BI and healthcare analytics, one topic that has especially interested me is linear regression using Python. Although I had never used Python before, I soon realized that the code is simply another way to tell the computer to perform the same linear regression concepts used in statistics and machine learning.

These notes reflect my personal exploration of linear regression and Python. My goal is to understand the concepts in plain language and connect them with practical healthcare examples.

What Is Linear Regression?

Linear regression is a machine learning and statistical technique used to predict a continuous numeric value based on the relationship between variables.

The basic idea is simple:

  • Input (X) → Independent variable or feature.
  • Output (Y) → Dependent variable or target.
  • The model learns the relationship between X and Y and finds the best-fit line.
  • The line can then be used to estimate future values.

A simple formula for linear regression is:

y = β₀ + β₁x

Where:

  • y = predicted value
  • x = input variable
  • β₀ (intercept) = where the line starts
  • β₁ (coefficient or slope) = how much y changes when x increases by one unit

Healthcare Example

Suppose we want to study the relationship between the number of chronic conditions and a patient’s annual healthcare cost.

Number of Chronic Conditions (X)Annual Cost (Y)
1$2,000
2$3,100
3$4,200
4$5,400
5$6,600

A linear regression model can use historical patient data like this to learn the pattern and estimate future healthcare costs for similar patients.

Other healthcare examples include:

  • Predicting annual healthcare costs.
  • Estimating PMPM (Per Member Per Month) spending.
  • Forecasting hospital resource utilization.
  • Exploring the relationship between HCC burden and healthcare costs.

What Is Python Doing?

The Python code shown in class is not creating the mathematics from scratch. It simply follows a series of logical steps:

Step 1. Import the Required Libraries

Python loads tools that help perform mathematical calculations and machine learning tasks.

Step 2. Load or Create the Data

The data is divided into:

  • X (Independent Variable) – the input or feature.
  • Y (Dependent Variable) – the value we want to predict.

Step 3. Create a Linear Regression Model

A machine learning model is created using Python’s built-in LinearRegression() function.

Step 4. Train the Model

The command:

model.fit(X, y)

tells the computer to study the historical data and find the best-fit line. This is called training the model.

Step 5. Obtain the Model Parameters

After training, the model calculates:

  • Intercept – the starting point of the line.
  • Coefficient (Slope) – how much the predicted value changes when the input increases by one unit.

Step 6. Make Predictions

The command:

model.predict()

uses the learned relationship to estimate values for new data.

For example, if we have data for patients with 1–10 chronic conditions, the model can estimate the expected annual cost for a patient with 12 chronic conditions.

Understanding the Results

Intercept

The intercept is where the regression line crosses the y-axis. It represents the predicted value when the input variable is zero.

Coefficient (Slope)

The coefficient tells us how much the predicted value changes for every one-unit increase in the input variable.

Mean Squared Error (MSE)

MSE measures the average prediction error of the model. Smaller values generally indicate better predictions.

R² (R-Squared)

R² measures how well the model explains the variation in the data.

  • R² = 0 → The model explains almost none of the variation.
  • R² = 1 → The model explains all of the variation (perfect fit).

An R² value close to 1 usually indicates that the model fits the data well.

What I Learned

At first, I thought I needed to understand every line of Python code. However, I realized that the most important part is understanding the business question and the logic behind the model.

For me, linear regression is simply a way of answering this question:

Can we use historical data to estimate a future numeric value?

The Python code is simply a tool that helps the computer perform the calculations. The real goal is to understand the relationship between data and use it to support better decisions.

🌱 Study Note: Before memorizing Python syntax, make sure you understand the purpose of linear regression. The code may change, but the business question remains the same.

A Simple Way I Remember the Python Code

When I first saw Python, I thought I needed to understand every detail. However, I realized that each line simply represents one step in the machine learning process.

PythonWhat It Means
importBring in the tool we need.
X and yInput data (feature) and output data (target).
LinearRegression()Create a linear regression model.
fit(X, y)Train the model using historical data.
predict()Use the model to estimate or predict future values.

🌱 A Simple Way I Think About Python

Many introductory machine learning examples follow the same basic workflow:

Import the libraries → Provide the data → Create and fit the model → Make predictions.

As a beginner, I find it even easier to remember it as a simple mental map:

Bring in the tools → Load the data → Create the model → Train the model → Make predictions.

Once I understood this sequence, the Python code became much less intimidating. Instead of trying to memorize every command, I focus on understanding what the computer is doing at each step.

🌱 Key Takeaways

  • Linear regression predicts a number.
  • Python is simply a tool to implement the model.
  • fit() = train, predict() = estimate.
  • Always start with the business question.
Scroll to Top