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:

  • MedInc
  • HouseAge
  • AveRooms
  • AveBedrms
  • Population
  • AveOccup
  • Latitude
  • Longitude
  • RoomsPerHousehold

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:

import pandas as pd

# Load training data
train_data = pd.read_csv('data/california_housing_train.csv')
X_train = train_data.drop('MedHouseVal', axis=1)
y_train = train_data['MedHouseVal']

print(f"Training with {len(X_train)} samples")
print(f"Features: {list(X_train.columns)}")

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:

Training with 16512 samples
Features: ['MedInc', 'HouseAge', 'AveRooms', 'AveBedrms', 'Population', 'AveOccup', 'Latitude', 'Longitude', 'RoomsPerHousehold']

This confirms that our data is loaded correctly and ready for training.

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal