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 made this model, just named it model, and now it has a fit method on it. And that fits the data, sort of trains it on that data.

And we get back a trained model. It's gonna 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, as well as the label that goes with those features.

That way it can take a look at it and say, okay, that's a cat, that's a dog. Or, class five minus three is two, seven minus four is three, and hope that they can learn the patterns, learn the lessons. They need to know both the question and the answer in order to understand the concept.

So that's what we're doing here. We're going to give it that value and have it train on it. So that's very easy.

We just say model.fit. It's very simple, I should say. Not easy, not the same thing. 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.

And it will evaluate to a linear regression model, this right here. And now that it's trained and it was that quick, we can start testing the model, see how 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