Training a Machine Learning Model
Introduction & Context
Welcome back! In the previous lessons, you learned how to explore the California housing dataset, engineer new features, handle outliers, and split your data into training and testing sets. These are essential steps that set the foundation for building reliable machine learning models.
In this lesson, we will take the next big step: training your first machine learning model using the preprocessed data. By the end of this lesson, you will know how to build, evaluate, and save a simple regression model using Scikit-Learn. This is a key milestone, as it marks your transition from preparing data to actually making predictions. The skills you learn here will be useful in many real-world projects and will prepare you for more advanced modeling techniques in the future.
Reviewing the Preprocessed Data
Before we start building a model, let's quickly review the data you have prepared so far. In the last lesson, you created new features such as RoomsPerHousehold, capped extreme values to handle outliers, and split your data into training and testing sets. You then saved these processed datasets as CSV files, which makes it easy to load them for modeling.
The training data contains 16,512 samples, and the test data contains 4,128 samples. Each sample includes the following features:
MedIncHouseAgeAveRoomsAveBedrmsPopulationAveOccupLatitudeLongitudeRoomsPerHousehold
The target variable we want to predict is MedHouseVal, which represents the median house value in each district. By keeping your features and target variable organized, you are now ready to train a model that can learn from this data.
Loading the Training Data
The first step in training our model is to load the preprocessed training data. On CodeSignal, all necessary libraries are pre-installed, so you can focus on the code itself. Here is how we can load the data and prepare it for training:
In this code, we use pandas to load the training data from a CSV file. We then separate the features (X_train) from the target variable (y_train). The features are all the columns except MedHouseVal, which is our target variable that we want to predict.
When we run this code, we will see output confirming the number of training samples and the features being used:
This confirms that our data is loaded correctly and ready for training.
