Predict Housing Prices with Regression
Train Linear and Logistic Regression models on real medical data.
Introduction
30 Second Summary
Think about how a doctor looks at a patient's weight, age, and blood pressure to estimate their risk of getting sicker. They are spotting patterns in data to make a prediction. Machine learning does the same thing, but at scale and backed by math.
In this project, you will build a Python script that trains two different ML models on real medical data. One model predicts a number. The other classifies patients into groups. By the end, you can run a single script and see both models make predictions side-by-side.
What You'll Build
Run one script and watch two algorithms analyze the same patient dataset, then print metrics that prove each model answers a fundamentally different type of question.
By the end of this project, you'll have:
- A scatter plot with a red regression line showing your Linear Regression model predicting disease progression as a continuous number.
- A comparison bar chart revealing why Logistic Regression classifies patients into "high" and "low" categories with more confidence than predicting exact values.
- Printed evaluation metrics (R-squared and accuracy) that answer the question: which algorithm should I use for my problem?
- Secret Mission: Add a Decision Tree model and compare all three algorithms' performance head-to-head on the same dataset.
Are there any prerequisites?
No prior machine learning or Python experience is needed. This project includes full setup instructions for every tool you will use.
Before We Start
Before We Start...
Before we dive in, let's lock in what you're about to build and why it matters.
Set Up Your Environment
Before you can train any machine learning models, you need the right tools on your machine. Python is the programming language, and scikit-learn is the library that handles the heavy lifting for training models.
In this step, you will install Python, create an isolated environment for your project's packages, and verify everything works by importing scikit-learn.
In this step, get ready to:
- Install Python 3.14 on your Windows machine.
- Create a virtual environment and install ML libraries.
- Set up VS Code and verify your environment works.
Install Python 3.14
Python is the language your ML code runs on. You need version 3.14 or higher because scikit-learn 1.9.0 requires it.
- Go to https://www.python.org/downloads/ in your browser.
- Click the download button for the Windows installer (64-bit) for Python 3.14.
- Run the installer once it downloads.
- Check the box that says Add python.exe to PATH before clicking anything else.
- Click Install Now and wait for installation to complete.
- Close the installer when finished.
Why is Add to PATH important?
PATH is a list of folders your terminal searches when you type a command. Without checking this box, typing python in your terminal would fail because your system would not know where to find it.
Now verify that Python installed correctly.
- Press the Windows key to open your search bar.
- Type PowerShell and press Enter to open it.
- Check your Python version by running this command:
python --version
✔️ I see Python 3.14
You should see something like Python 3.14.6. Python is ready to go.
ⓧ I see an older version
You have an older Python installed. Download and install Python 3.14 from https://www.python.org/downloads/ using the steps above.
- Make sure Add python.exe to PATH is checked during installation.
- Close your PowerShell window completely and reopen it.
- Run python --version again.
If it still shows the old version, the older Python's PATH entry may be taking priority. Try running this instead:
py -3.14 --version
If that works, use py -3.14 instead of python for the rest of this project.
ⓧ Command not found
Python is not installed yet, or the PATH was not set. Follow these steps:
- Go to https://www.python.org/downloads/ and download the Windows installer (64-bit) for Python 3.14.
- Run the installer and check Add python.exe to PATH.
- Click Install Now.
- Close and reopen PowerShell after installation finishes.
- Run python --version again to confirm.
Create a virtual environment and install libraries
A virtual environment is an isolated folder where your project's packages live. It keeps this project's libraries separate from anything else on your system.
- Move to your Desktop by running this command:
cd ~/Desktop
- Create your virtual environment and activate it by running these commands:
python -m venv ml-project
ml-project\Scripts\activate
Your terminal prompt now shows (ml-project) at the beginning. This confirms you are inside the virtual environment.
Not seeing (ml-project) in your prompt?
- If you see an error about running scripts being disabled, run this command to allow it:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
Type Y when prompted, then try ml-project\Scripts\activate again.
If you see 'python is not recognized', go back to the Python install tab and make sure PATH was configured. Help me activate my virtual environment on Windows.
- Install the machine learning packages by running this command:
pip install scikit-learn matplotlib numpy pandas
You will see packages downloading and a final line that says Successfully installed followed by a list of package names.
What are these packages?
- scikit-learn provides the machine learning algorithms (Linear Regression, Logistic Regression, and more).
- matplotlib draws charts and visualizations so you can see what your models are doing.
- numpy handles fast number crunching behind the scenes.
- pandas is useful for exploring datasets in follow-up work.
Seeing pip errors?
- Make sure your virtual environment is active (you should see (ml-project) in your prompt). If not, run ml-project\Scripts\activate again.
- If you see a permissions error, make sure you are running PowerShell as a regular user, not as Administrator.
Help me fix pip install errors in my virtual environment.
Set up VS Code and verify your environment
VS Code is the code editor where you will write your Python script. You need it open with your project folder so it can find the virtual environment.
- If you do not have VS Code installed, download it from https://code.visualstudio.com and run the installer.
- Press the Windows key to open your search bar.
- Type VS Code and press Enter to open it.
- Click File in the top menu bar.
- Click Open Folder.
- Navigate to your Desktop and select the ml-project folder, then click Select Folder.
Now create the Python file where all your ML code will live.
- Click File in the top menu bar, then click New File.
- Type ml_models.py as the file name and press Enter.
- Add this code to your new file to test that scikit-learn is installed:
import sklearn; print(sklearn.__version__)
What does this code do?
This line imports the scikit-learn library and prints its version number. If the import works and a version prints, your entire environment (Python, virtual environment, and packages) is set up correctly.
Before you run this, what version number do you expect to see printed?
- Save the file by pressing Ctrl+S.
- Open the terminal inside VS Code by pressing Ctrl+` (the backtick key, above Tab).
- Make sure your virtual environment is active in this terminal. If you do not see (ml-project) in the prompt, run:
ml-project\Scripts\activate
- Run your script by entering this command:
python ml_models.py
You should see 1.9.0 printed in the terminal. This confirms Python, your virtual environment, and scikit-learn are all working together.
✔️ Awesome, I've got everything!
Great. Double check you have saved ml_models.py and that 1.9.0 printed in your terminal.
ⓧ I'd like to double check the full code
Your ml_models.py file should contain exactly this:
import sklearn; print(sklearn.__version__)
Not seeing 1.9.0?
- If you see ModuleNotFoundError: No module named 'sklearn', your virtual environment is not active in the VS Code terminal. Run ml-project\Scripts\activate and try again.
- If the terminal opened in a different folder, make sure you opened the ml-project folder through File > Open Folder in VS Code.
- If you see a different version number, that is fine as long as it is 1.9 or higher.
Help me get scikit-learn working in VS Code.
Your environment is ready. Next up, you will load a real medical dataset and explore it to understand what your ML models will learn from.
Load and Explore Real Data
Your environment is ready and scikit-learn confirmed it imports correctly. Now it is time to meet the data your models will learn from.
A model is only as good as the data you feed it. Jumping straight to training without understanding your dataset leads to garbage predictions. In this step you will load a real medical dataset, inspect its shape and range, then visualize one relationship to see the pattern your model will try to capture.
In this step, get ready to:
- Load the scikit-learn diabetes dataset and print its key statistics.
- Create a scatter plot showing the relationship between BMI and disease progression.
Load the diabetes dataset and print its overview
The diabetes dataset is built into scikit-learn. It contains measurements from 442 patients. Your goal is to see how many samples, features, and what range of target values you are working with before training any model.
- Switch back to ml_models.py in VS Code.
- Select all the existing code and delete it.
- Replace it with the following code that imports your libraries, loads the dataset, and prints an overview:
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import load_diabetes
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression, LogisticRegression
from sklearn.metrics import mean_squared_error, r2_score, accuracy_score
# --- Step 2: Load and Explore Real Data ---
diabetes = load_diabetes()
X = diabetes.data
y = diabetes.target
feature_names = diabetes.feature_names
print("=== Dataset Overview ===")
print(f"Samples: {X.shape[0]}")
print(f"Features: {X.shape[1]}")
print(f"Feature names: {feature_names}")
print(f"Target range: {y.min():.0f} to {y.max():.0f}")
print(f"Target mean: {y.mean():.0f}")
print()
What does this code do?
- The first six lines import every library the project will use. Some imports are for later steps, but loading them now keeps all imports in one place at the top.
- load_diabetes() pulls a built-in dataset of 442 diabetes patients with 10 health measurements each.
- X holds the feature data (the inputs) and y holds the target (the number the model will predict).
- The print statements show you the dataset's shape and value range so you know exactly what you are working with.
- Save ml_models.py.
- Run the script in your terminal by running:
python ml_models.py
You should see output showing 442 samples, 10 features, a list of feature names, and a target range of 25 to 346.
Seeing a ModuleNotFoundError?
- Make sure your virtual environment is active. You should see (ml-project) at the start of your terminal prompt. If not, run ml-project\Scripts\activate first.
- Confirm you installed the packages inside the virtual environment by running pip list and checking that scikit-learn appears.
Why isn't my script finding scikit-learn even though I installed it?
Create a scatter plot of BMI vs disease progression
Numbers alone do not tell the whole story. A scatter plot lets you see whether a visible relationship exists between a single feature and the target. If you can spot a trend by eye, that is a good sign that Linear Regression will be able to capture it mathematically.
- Add the following code at the bottom of ml_models.py, below the print() line:
# BMI is the 3rd feature (index 2)
bmi_index = 2
plt.figure(figsize=(8, 5))
plt.scatter(X[:, bmi_index], y, alpha=0.5, color="steelblue")
plt.xlabel("BMI (standardized)")
plt.ylabel("Disease Progression")
plt.title("BMI vs Disease Progression")
plt.tight_layout()
plt.savefig("01_data_exploration.png")
plt.show()
What does this code do?
- bmi_index = 2 selects the BMI column (the third feature in the dataset, at index 2).
- plt.scatter() plots each patient as a dot. The x-axis is their BMI value and the y-axis is their disease progression score.
- plt.savefig() saves the chart as 01_data_exploration.png so you have a permanent copy. plt.show() opens the plot in a window.
- Save ml_models.py.
Before you run the script, what do you think the scatter plot will look like? A clear straight line, random noise, or something in between?
- Run the script again:
python ml_models.py
A matplotlib window opens with a scatter plot. You should see a cloud of blue dots with a visible upward trend. Higher BMI values tend to have higher disease progression scores, but there is a lot of spread. Close the plot window to let the script finish.
Why is the trend not a perfect line?
BMI is only one of 10 features that affect disease progression. A single feature cannot capture the full picture. In the next step, you will see how Linear Regression tries to draw a "best fit line" through this noisy cloud.
Plot window not appearing?
- If you are running the script through VS Code's integrated terminal, the plot window may open behind VS Code. Check your taskbar for a Python window.
- If no window appears at all, confirm matplotlib installed correctly by running pip show matplotlib in your terminal.
Why is my matplotlib plot not showing up on Windows?
✔️ Awesome, I've got everything!
Great. Double check you have saved ml_models.py and that 01_data_exploration.png now exists in your project folder.
ⓧ I'd like to double check the full code
Your ml_models.py should contain two sections in this order:
- The imports block (6 import lines) followed by a blank line and the # --- Step 2: Load and Explore Real Data --- comment.
- The dataset loading and print block (diabetes = load_diabetes() through print()).
- The scatter plot block (bmi_index = 2 through plt.show()).
Scroll up and confirm each chunk matches what you pasted. If your output shows 442 samples and the scatter plot opened, you are on track.
You now know your dataset: 442 patients, 10 features, and a target that ranges from 25 to 346. You have also seen with your own eyes that BMI and disease progression are correlated but noisy. Next up, you will train a Linear Regression model to draw the best line through that scatter and measure how well it predicts.
Train Linear Regression and Hit Its Limits
In the previous step, you saw a clear upward trend between BMI and disease progression in your scatter plot. Now it is time to ask: can a model actually learn that trend and use it to predict new patients' outcomes?
Linear Regression draws a straight line through your data to predict a continuous number. The problem is that a single line cannot perfectly capture complex medical relationships. In this step, you will train the model and watch it fall short, which motivates evaluation metrics.
In this step, get ready to:
- Train a Linear Regression model using only the BMI feature and see its low R-squared score.
- Visualize the regression line overlaid on the actual data points.
- Retrain with all 10 features and measure the improvement.
Split the data and train on BMI only
Before a model can learn patterns, you need to hold back some data it has never seen. This is called a train/test split. The model learns from 80% of the data (training set) and gets tested on the remaining 20% (test set).
- In ml_models.py, scroll to the bottom of your file (below the plt.show() line from Step 2).
- Add the following code to split the BMI data and train your first model:
# --- Step 3: Train Linear Regression ---
# Using only BMI feature first
X_bmi = X[:, bmi_index].reshape(-1, 1)
X_train, X_test, y_train, y_test = train_test_split(
X_bmi, y, test_size=0.2, random_state=42
)
reg_bmi = LinearRegression()
reg_bmi.fit(X_train, y_train)
y_pred_bmi = reg_bmi.predict(X_test)
r2_bmi = r2_score(y_test, y_pred_bmi)
print("=== Linear Regression (BMI only) ===")
print(f"R-squared score: {r2_bmi:.4f}")
print()
What does this code do?
reshape(-1, 1) converts the BMI column from a flat array into a 2D array that scikit-learn expects. train_test_split randomly divides your data so the model cannot cheat by memorising answers.
The three model steps follow a universal pattern: create the model, .fit() it on training data, then .predict() on test data. R-squared measures how well predictions match reality. A score of 1.0 means perfect predictions. A score of 0 means the model is no better than guessing the average every time.
Before you run this, think: with only one feature (BMI) out of ten available, do you expect the model to predict disease progression accurately?
- Save ml_models.py and run it in your terminal:
python ml_models.py
You will see the dataset overview from Step 2, the scatter plot window, and then a new printout showing an R-squared score around 0.33. That means the model explains only about 33% of the variation in disease progression using BMI alone.
Not seeing the R-squared output?
Close the first scatter plot window (from Step 2) when it appears. The script pauses at each plt.show() call until you close the plot window. Once you close it, the remaining code continues and prints the R-squared score.
If you see an error about LinearRegression not being defined, check that your import line at the top still reads from sklearn.linear_model import LinearRegression, LogisticRegression.
help me debug my Linear Regression code
Visualize the regression line
A number like 0.33 is abstract. A visualization makes the limitation concrete. You will plot the model's prediction line against the actual data points to see exactly where predictions go wrong.
- Add this code below your print() statement to plot the regression line overlaid on the test data:
# Plot regression line
plt.figure(figsize=(8, 5))
plt.scatter(X_test, y_test, alpha=0.5, color="steelblue", label="Actual")
sort_idx = X_test.flatten().argsort()
plt.plot(
X_test[sort_idx],
y_pred_bmi[sort_idx],
color="red",
linewidth=2,
label="Prediction Line",
)
plt.xlabel("BMI (standardized)")
plt.ylabel("Disease Progression")
plt.title(f"Linear Regression (BMI only) - R-squared: {r2_bmi:.4f}")
plt.legend()
plt.tight_layout()
plt.savefig("02_linear_regression_bmi.png")
plt.show()
What does this visualization show?
The blue dots are real patient outcomes. The red line is the model's "best guess" for any given BMI value. Notice how many dots sit far from the red line. The model captures the general upward trend but misses individual patients badly.
sort_idx sorts the test data by BMI value so the red line draws left-to-right instead of zigzagging between random test points.
- Save ml_models.py and run it again:
python ml_models.py
Close the first plot window, then a second plot appears showing blue dots scattered widely around a red line. The file 02_linear_regression_bmi.png is saved to your project folder.
Plot window not appearing?
Make sure you close each plot window that appears before the next one can display. If you are running in VS Code's integrated terminal and no window appears at all, try running from a standalone Command Prompt or PowerShell window instead.
help me get matplotlib plots to display
Retrain with all 10 features
The R-squared of 0.33 is disappointing, but the reason is clear: you gave the model only one piece of information (BMI) when the dataset has ten features. What happens if you give the model everything?
- Add this code below your plt.show() line to retrain on all features:
# Now use ALL features
X_train_all, X_test_all, y_train_all, y_test_all = train_test_split(
X, y, test_size=0.2, random_state=42
)
reg_all = LinearRegression()
reg_all.fit(X_train_all, y_train_all)
y_pred_all = reg_all.predict(X_test_all)
r2_all = r2_score(y_test_all, y_pred_all)
print("=== Linear Regression (All 10 features) ===")
print(f"R-squared score: {r2_all:.4f}")
print(f"Improvement over BMI only: +{r2_all - r2_bmi:.4f}")
print()
Why does this help?
Instead of passing X_bmi (one column), you now pass X (all 10 columns: age, sex, BMI, blood pressure, and six blood serum measurements). More information gives the model more clues to work with.
The concept here is feature selection. Choosing which features to include directly impacts model performance. More features generally help, but even with all 10, the R-squared will still be well below 1.0 because predicting exact numbers from noisy medical data is inherently difficult.
Before you run this, predict: will the R-squared double, or improve by a smaller margin?
- Save ml_models.py and run it one more time:
python ml_models.py
After closing both plot windows, you will see the all-features R-squared is around 0.45 to 0.52. It improved, but predicting exact disease progression numbers remains hard.
What does this result tell you?
Even with all the data available, Linear Regression can only explain about half the variation in disease outcomes. This is the fundamental limitation: predicting a precise continuous number from noisy real-world data is difficult.
In the next step, you will ask a simpler question ("high or low?") and see how a different algorithm answers it with much higher confidence.
R-squared score looks very different from expected?
Make sure random_state=42 is set in both train_test_split calls. Without it, the split is random each run and scores will vary.
Check that the all-features split uses X (the full 10-column array), not X_bmi (the single BMI column).
help me debug my R-squared scores
✔️ Awesome, I've got everything!
Great. Double check you have saved ml_models.py and that running it prints both R-squared scores after the plot windows close.
ⓧ I'd like to double check the full code
Your ml_models.py should contain the following sections in order. Scroll through your file and confirm each is present:
- Imports at the top (numpy, matplotlib, sklearn modules).
- Step 2 block: load diabetes dataset, print overview, create and save 01_data_exploration.png.
- Step 3 block: reshape BMI, split, train reg_bmi, print R-squared (~0.33).
- Plot block: scatter test data, draw red prediction line, save 02_linear_regression_bmi.png.
- All-features block: split full X, train reg_all, print improved R-squared (~0.45-0.52) and improvement delta.
You have now seen Linear Regression's ceiling on this dataset. Even with all features, predicting an exact number leaves half the story unexplained. Next, you will transform the same data into a simpler question and watch a different algorithm answer it with much higher confidence.
Classify with Logistic Regression
Your Linear Regression model from the last step maxed out around 0.45-0.52 R-squared, even with all 10 features.
Predicting an exact disease progression number is genuinely hard. But what if you reframe the question? Instead of "what exact number?" you ask "high or low?" Logistic Regression answers yes/no questions like this with much higher confidence.
In this step, get ready to:
- Transform the continuous target into a binary classification problem.
- Train a Logistic Regression model and print its accuracy.
- Create a comparison bar chart showing both models' scores side-by-side.
Create a binary target from the dataset
Classification needs labels, not continuous numbers. You will split patients into two groups based on the median disease progression score: anyone above the median gets label 1 ("high"), anyone below gets label 0 ("low").
- In ml_models.py, scroll to the bottom of your file (below the last print() statement) and add this code:
# --- Step 4: Logistic Regression Classification ---
# Create binary target: above median = 1 (high), below = 0 (low)
median_target = np.median(y)
y_binary = (y > median_target).astype(int)
print(f"Median disease progression: {median_target:.0f}")
print(f"High progression patients: {y_binary.sum()}")
print(f"Low progression patients: {len(y_binary) - y_binary.sum()}")
print()
What does this code do?
You are turning a regression problem into a classification problem using the same data.
- np.median(y) finds the middle value of all disease progression scores.
- (y > median_target).astype(int) creates a new array where every score above the median becomes 1, and every score below becomes 0.
- The print statements confirm the split is roughly even between high and low groups.
- Save ml_models.py and run it with:
python ml_models.py
- After the previous Linear Regression output, you should see new lines showing the median value and how many patients fall into each group (roughly half and half).
Not seeing the new output?
Make sure you added the code at the very bottom of your file, after all the existing Linear Regression code. If you see an error about y not being defined, check that your Step 2 code (which defines y = diabetes.target) is still intact above.
Help me debug my binary target code
Train a Logistic Regression model
Now that you have binary labels, you can train a Logistic Regression classifier. This algorithm draws a boundary between the two groups instead of fitting a line through scattered points.
- Add this code below the binary target code you just wrote:
X_train_cls, X_test_cls, y_train_cls, y_test_cls = train_test_split(
X, y_binary, test_size=0.2, random_state=42
)
clf = LogisticRegression(max_iter=1000, random_state=42)
clf.fit(X_train_cls, y_train_cls)
y_pred_cls = clf.predict(X_test_cls)
accuracy = accuracy_score(y_test_cls, y_pred_cls)
print("=== Logistic Regression (Classification) ===")
print(f"Accuracy: {accuracy:.4f} ({accuracy * 100:.1f}%)")
print()
What does this code do?
- train_test_split splits the data the same way as before (80% train, 20% test), but now using the binary labels y_binary instead of the continuous target.
- max_iter=1000 gives the algorithm enough iterations to converge on this dataset. The default of 100 may not be enough.
- accuracy_score measures the percentage of patients the model correctly classified as high or low.
Why is classification easier than regression here?
Predicting "is this patient above or below the median?" is a simpler question than "what is this patient's exact progression score?" The model only needs to get the direction right, not the magnitude. This is why you can expect accuracy above 70% even though the R-squared was only around 0.50.
- Save ml_models.py and run the script again:
python ml_models.py
- Look for the new Logistic Regression section in the output. You should see an accuracy score above 70%.
Seeing a convergence warning?
If you see a warning about the model not converging, double-check that you set max_iter=1000 in the LogisticRegression() call. The default of 100 iterations is too few for this dataset.
Help me fix my Logistic Regression convergence issue
Create a comparison visualization
You now have two models solving two different types of problems. A side-by-side bar chart makes the difference unmistakable.
- Add this code at the bottom of ml_models.py:
# Comparison bar chart
plt.figure(figsize=(8, 5))
models = ["Linear Regression\n(R-squared)", "Logistic Regression\n(Accuracy)"]
scores = [r2_all, accuracy]
colors = ["steelblue", "coral"]
plt.bar(models, scores, color=colors, width=0.5)
plt.ylim(0, 1)
plt.ylabel("Score")
plt.title("Regression vs Classification: Different Problems, Different Metrics")
for i, score in enumerate(scores):
plt.text(i, score + 0.02, f"{score:.2f}", ha="center", fontsize=12)
plt.tight_layout()
plt.savefig("03_model_comparison.png")
plt.show()
print("=== Summary ===")
print(f"Linear Regression R-squared (all features): {r2_all:.4f}")
print(f"Logistic Regression Accuracy: {accuracy:.4f}")
print()
print("Key Insight: Logistic Regression scores higher because")
print("classifying 'high vs low' is easier than predicting exact numbers.")
print("Choose your algorithm based on your QUESTION, not your data.")
What does this code do?
- The bar chart plots both models' scores on the same 0-to-1 scale so you can visually compare them.
- plt.text() adds the exact numeric score above each bar for clarity.
- plt.savefig("03_model_comparison.png") saves the chart as an image file in your project folder.
- The summary print statements highlight the key takeaway: choose your algorithm based on your question, not your data.
- Save ml_models.py.
Before you run the script, what do you think the bar chart will show? Will the Logistic Regression bar be taller or shorter than the Linear Regression bar?
- Run the script to find out:
python ml_models.py
You should see a bar chart pop up with two bars. The Logistic Regression (coral) bar is taller because classification accuracy is higher than the regression R-squared score. The summary text confirms the insight.
Bar chart not appearing?
Make sure you close the previous plot windows (the scatter plot and regression line) before the script proceeds to the bar chart. Each plt.show() call pauses execution until you close that window.
Help me figure out why my matplotlib chart is not showing
✔️ Awesome, I've got everything!
Great. Double check you have saved ml_models.py and that you see 03_model_comparison.png in your project folder.
ⓧ I'd like to double check the full code
Your ml_models.py file is now longer than 30 lines. Scroll through and confirm these sections are present, in order:
- Imports at the top (numpy, matplotlib, sklearn).
- Step 2: Load and Explore Real Data (loads diabetes dataset, prints overview, saves 01_data_exploration.png).
- Step 3: Train Linear Regression (BMI-only model, plot with regression line saved as 02_linear_regression_bmi.png, then all-features model).
- Step 4: Logistic Regression Classification (binary target creation, train/test split, model training, accuracy print).
- Comparison bar chart saved as 03_model_comparison.png, followed by summary print statements.
You have now built two fundamentally different models from the same dataset. Linear Regression predicts a continuous number. Logistic Regression predicts a category. Next up, you will extend this into a Secret Mission by adding a third algorithm and comparing all three head-to-head.
Secret mission
Add a Decision Tree and Compare All Three Models
You have two algorithms that approach the same data differently. But what about a third? Decision Trees learn by splitting data into branches, asking yes/no questions about features instead of drawing lines. Add a Decision Tree for both tasks and crown the winner.
Clean Up Your Resources
Clean Up Your Resources
This project runs entirely on your local machine, so there are no ongoing cloud costs or subscription charges. Decide whether to keep your resources running, pause them to come back later, or delete them entirely.
Resources you used:
- ml-project virtual environment (contains scikit-learn, matplotlib, numpy, and pandas).
- ml_models.py script file.
- Generated chart images (01_data_exploration.png, 02_linear_regression_bmi.png, 03_model_comparison.png, 04_full_comparison.png).
- Python 3.14 (system-wide install).
- VS Code (system-wide install).
Keep everything running
No action needed. Choose this if you are still actively building or want to experiment further with the models. Your virtual environment and script will be ready whenever you return.
Pause - I'll come back to this later
Shut down any running processes and close your editor to free up system memory. Your files stay on disk so you can pick up where you left off.
- If your virtual environment is still active in the terminal, deactivate it by running:
- Close VS Code.
deactivate
When you come back, reactivate the environment by opening a terminal in your project folder and running ml-project\Scripts\activate.
Delete - I don't want to use this again
Remove all project files, the virtual environment, and optionally uninstall the tools you installed for this project.
- If your virtual environment is still active, deactivate it first by running:
deactivate
- Delete the virtual environment by running:
rmdir /s /q ml-project
- Delete the script and generated images by running:
del ml_models.py
del *.png
- To uninstall Python, press the Windows key and open Settings.
- Select Apps, then Installed apps.
- Search for Python 3.14, click the three-dot menu, and select Uninstall.
- To uninstall VS Code, search for Visual Studio Code in the same Installed apps list, click the three-dot menu, and select Uninstall.
Nice Work!
Nice Work!
You just built a complete machine learning script that trains two fundamental algorithms on real medical data and visualizes how each one makes predictions.
You've learned how to:
- Train a Linear Regression model that predicts continuous values from real patient data, and evaluate it using an R-squared score.
- Train a Logistic Regression model that classifies patients into categories, and evaluate it using accuracy.
- Visualize how each algorithm sees data differently and choose between regression (predicting a number) and classification (predicting a category) based on your question.
- Secret Mission: Added a Decision Tree for both regression and classification tasks and compared all three algorithms head-to-head.
Ready to quiz yourself?