Training and Testing Linear Regression Models

Train the linear regression model using X train and Y train data.

Learn how to effectively train a linear regression model by supplying it with labeled data. Understand the simple yet crucial steps involved in preparing machine learning models for testing.

Key Insights

  • Train a linear regression model using the fit method, providing it with both features (X train) and labels (Y train) so it can accurately learn data patterns.
  • Supplying the model with labeled data enables it to identify patterns and relationships—for example, distinguishing between categories like "cat" vs. "dog" or solving arithmetic problems such as "five minus three."
  • After training the model with labeled data, the next step involves evaluating its performance by testing it against new data samples.

Note: These materials offer prospective students a preview of how our classes are structured. Students enrolled in this course will receive access to the full set of materials, including video lectures, project-based assignments, and instructor feedback.

Let's train the model. We created this model and named it 'model', and now it has a fit method on it. That method fits the data and trains the model on it.

We get back a trained model. It will take our training data as its inputs, and that's the X train and the Y train. Remember, it needs to know the answer as well as the inputs, the features and the label that corresponds to those features.

This allows the model to classify data such as identifying a cat or a dog. Or, for example, the model might learn that 5 minus 3 equals 2 and 7 minus 4 equals 3, hoping it will detect patterns and relationships. The model needs both the question (input) and the answer (label) to understand the concept.

We provide it with the data and allow it to train on it. It's that simple.

We simply use model.fit. It's straightforward, not necessarily easy. Give it the X train data and the Y train data.

Data Analytics Certificate: Live & Hands-on, In NYC or Online, 0% Financing, 1-on-1 Mentoring, Free Retake, Job Prep. Named a Top Bootcamp by Forbes, Fortune, & Time Out. Noble Desktop. Learn More.

It will evaluate as a linear regression model, as shown here. Now that the model is trained, and it was done quickly, we can begin testing the model to see how well it works.

Colin Jaffe

Colin Jaffe is a programmer, writer, and teacher with a passion for creative code, customizable computing environments, and simple puns. He loves teaching code, from the fundamentals of algorithmic thinking to the business logic and user flow of application building—he particularly enjoys teaching JavaScript, Python, API design, and front-end frameworks.

Colin has taught code to a diverse group of students since learning to code himself, including young men of color at All-Star Code, elementary school kids at The Coding Space, and marginalized groups at Pursuit. He also works as an instructor for Noble Desktop, where he teaches classes in the Full-Stack Web Development Certificate and the Data Science & AI Certificate.

Colin lives in Brooklyn with his wife, two kids, and many intricate board games.

More articles by Colin Jaffe

How to Learn Machine Learning

Master Machine Learning with Hands-on Training. Use Python to Make, Modify, and Test Your Own Machine Learning Models.

Yelp Facebook LinkedIn YouTube Twitter Instagram