Photo by Cosmic Timetraveler
A Linear Regression model is a basic technique in statistics used to understand the relationship between two variables by fitting data to a straight line. It assumes a linear relationship between the input and output variables. For example you can use it to predict the price of a house based on the size of the house.
Let’s proceed with a simple step-by-step guide on how to use Linear Regression to make predictions:
- Collect Data: Gather data containing pairs of input variables (X) and their corresponding output variable (Y). For example, to predict house prices based on their size, you’d collect data on house sizes (X) and their respective prices (Y).
- Prepare Data: Organize your data into a format that the Linear Regression model can understand. Typically, this involves splitting your data into a training set, to train the model and a test set to evaluate its performance.
- Choose the Model: Select Linear Regression as your predictive model. In its simplest form, Linear Regression assumes a linear relationship between the input and output variables, represented by the equation: Y = mx + b, where Y is the predicted value (house price) and x is the input variable. I’ll skip detailed explanation of this formula for now but just know that m and b are known as the slope and intercept.
- Train the Model: Feed your training data into the Linear Regression model. The model will adjust its parameters (m and b which are the slope and intercept) to minimize the difference between its predictions and the actual output values in the training data. This process is called “training” the model.
- Make Predictions: Once the model is trained, you can use it to make predictions on new, unseen data. Simply input the size of the house into the trained model, and it will predict the corresponding price of the house.
To see this simplified example in action get the following python code, then open a notebook and paste the code in the cell. Finally, click the run/play button to see the predicted result. After training the model with training data, you can use it to predict the price of a house based on new house size data.
Congratulations on taking your first real step into the world of Machine Learning with Linear Regression! Your next step is to broaden your toolkit for solving different types of problems by learning How to Use a Logistic Regression Model.


Leave a comment