AI Use Cases & Applications

Machine Learning for Teens: Build a Logistic Regression Model

At the 2024 AI Connect K-12 Data Science Conference, Corey Wade of Berkeley Coding Academy coded a heart disease prediction model live in Google Colab. He explained logistic regression and the four core steps of building a machine learning model, then noted that a real project would hold out a test set.

By The AI Enterprise Magazine Team4 min read

Key takeaways

  • Four steps. Import a model, initialize it, fit it to data and score it.
  • Probabilities from a curve. Logistic regression uses a sigmoid curve to output class probabilities.
  • Training adjusts weights. Gradient descent updates random starting weights to reduce error over many iterations.
  • Use a test set. Scoring on the training data is a simplification; real projects check unseen data.
  • Free setup. A Colab notebook and a private Gmail account were enough.
Machine Learning for Teens. Watch on YouTube

At the 2024 AI Connect K-12 Data Science Conference, a virtual event, Corey Wade, founder of Berkeley Coding Academy, led teens, parents and educators through building a real machine learning model in about 15 minutes of live coding. Using a heart disease data set in a Google Colab notebook, he showed that the core workflow takes four steps.

Wade said Berkeley Coding Academy teaches data science to ages 12 to 18, with a curriculum that covers Python, data analytics, machine learning and AI. He said it runs a summer program and year-round one-on-one classes.

How do you set up a machine learning project in Python?

Wade shared a starter notebook in Google Colab and asked attendees to save their own copy to Google Drive. He recommended a private Gmail account because, he said, Google has blocked Colab on many school accounts, and a personal account stores the code for later.

The starter code uses pandas, which he called Python's data analytics library, to read a raw data file from GitHub into a data frame. Calling head shows the first five rows.

The data set describes people and includes a "target" column, where 1 means heart disease and 0 means none. The question for the exercise, Wade said, was whether the other columns could predict that target.

He then split the data into X, the input to the model, and y, the output. "X is your input variable, Y is your output variable," he said, comparing it to traditional math.

How does logistic regression work?

Wade chose logistic regression from a list of algorithms that also includes linear regression, decision trees, random forests and gradient boosting. He explained it through the sigmoid curve, which squeezes any input into a value between 0 and 1.

Those values can be read as probabilities, he said. A point at 0.99 means a 99% probability of class 1. A point at 0.2 means a 20% probability of class 1, or 80% for class 0, so it is mapped to class 0. A point just right of the middle is above 50% and goes to class 1.

In training, he said, the model begins with randomly chosen weights that multiply each column. It adds the results, passes the sum through the sigmoid, and checks the percentage of correct predictions. Gradient descent, a technique that uses calculus and is standard across machine learning models, then adjusts the weights to reduce the error. The loop repeats until the error is minimized.

What are the four steps to build a machine learning model?

Wade coded the model live in four steps:

  1. Choose and import a model. From scikit-learn's linear_model module, import LogisticRegression. Wade noted that if you forget where a model lives, you can look it up.
  2. Initialize it. Create a variable called model and call the class with parentheses. At this point it has no weights.
  3. Fit it to the data. Call fit with capital X and lowercase y. Wade called this the "golden step" where the weights are randomized and optimized, and it ran almost instantly.
  4. Score it. Run model.score on X and y. The result was around 85%.

Wade said capital X denotes many rows and columns while lowercase y denotes a single column.

What can go wrong, and what comes next?

When he combined the steps in one cell, scikit-learn warned that the number of iterations had reached its limit without converging. Wade said that raising the maximum number of iterations to something like 2,000 clears the warning, and the score stayed at 85%.

He also flagged a simplification. In practice, he said, you would hold out a test set separate from the training data, because the goal is not curve fitting but how well the model scores on data it has not seen. He added that practitioners typically compare several models rather than picking one.

Wade said the session was a compressed version of material from the second day of a 52-hour program at his academy. At the academy, he said, it would be roughly a 40-minute lesson, followed by small-group coding on students' own data. Some attendees could not get the notebook to run in the session. Wade suggested trying a different browser or a private Gmail account, and said he would provide the final code afterward.

Where to learn more

AI Connect continues to run free live sessions on data and AI. The 2027 schedule includes 24 free live webinars across four series: the AI x Enterprise Panel Series, AI Demo Days, Agentic AI Showcases and the Plug & Play Series. Registration for 2027 events is coming soon. See the webinar schedule.

Frequently asked questions

How do you build a machine learning model in Python?

Wade showed four steps: import a model from scikit-learn, initialize it, fit it to X and y, and score it. His heart disease model scored about 85%.

What is logistic regression?

It maps inputs through a sigmoid curve to values between 0 and 1 that are read as probabilities of belonging to a class, such as having heart disease.

Why use a test set?

Wade said the goal is how well a model performs on data it has not seen, not curve fitting, so practitioners separate a test set.

What tools do students need?

A Google Colab notebook, ideally under a private Gmail account because school accounts may be blocked, plus pandas and scikit-learn.

This article is based on remarks by Corey Wade, Founder, Berkeley Coding Academy at AI Connect K-12 Data Science Conference 2024 on July 25, 2024. More coverage: More AI Connect Events.

Filed under: AI Use Cases & Applications, People, Culture & Leadership

More from AI Enterprise Magazine

Read the magazine