3. Your First Machine Learning Model
# 3.1 Selecting Data for Modeling
Your dataset had too many variables to wrap your head around, or even to print out nicely. How can you pare down this overwhelming amount of data to something you can understand?
We'll start by picking a few variables using our intuition. Later courses will show you statistical techniques to automatically prioritize variables.
To choose variables/columns, we'll need to see a list of all columns in the dataset. That is done with the columns property of the DataFrame (the bottom line of code below).
import pandas as pd
melbourne_file_path = '../input/melbourne-housing-snapshot/melb_data.csv'
melbourne_data = pd.read_csv(melbourne_file_path)
melbourne_data.columns
2
3
4
5
# The Melbourne data has some missing values (some houses for which some variables weren't recorded.)
# We'll learn to handle missing values in a later tutorial.
# Your Iowa data doesn't have missing values in the columns you use.
# So we will take the simplest option for now, and drop houses from our data.
# Don't worry about this much for now, though the code is:
# dropna drops missing values (think of na as "not available")
melbourne_data = melbourne_data.dropna(axis=0)
2
3
4
5
6
7
8
There are many ways to select a subset of your data. The Pandas Micro-Course (opens new window) covers these in more depth, but we will focus on two approaches for now.
- Dot notation, which we use to select the "prediction target"
- Selecting with a column list, which we use to select the "features"
# Selecting The Prediction Target
You can pull out a variable with dot-notation. This single column is stored in a Series, which is broadly like a DataFrame with only a single column of data.
We'll use the dot notation to select the column we want to predict, which is called the prediction target. By convention, the prediction target is called y. So the code we need to save the house prices in the Melbourne data is
y = melbourne_data.Price
# 3.2 Choosing "Features"
The columns that are inputted into our model (and later used to make predictions) are called "features." In our case, those would be the columns used to determine the home price. Sometimes, you will use all columns except the target as features. Other times you'll be better off with fewer features.
For now, we'll build a model with only a few features. Later on you'll see how to iterate and compare models built with different features.
We select multiple features by providing a list of column names inside brackets. Each item in that list should be a string (with quotes).
Here is an example:
melbourne_features = ['Rooms', 'Bathroom', 'Landsize', 'Lattitude', 'Longtitude']
By convention, this data is called X.
X = melbourne_data[melbourne_features]
Let's quickly review the data we'll be using to predict house prices using the describe
method and the head
method, which shows the top few rows.
X.describe()
X.head()
Visually checking your data with these commands is an important part of a data scientist's job. You'll frequently find surprises in the dataset that deserve further inspection.
# 3.3 Building Your Model
You will use the scikit-learn library to create your models. When coding, this library is written as sklearn, as you will see in the sample code. Scikit-learn is easily the most popular library for modeling the types of data typically stored in DataFrames.
The steps to building and using a model are:
- Define: What type of model will it be? A decision tree? Some other type of model? Some other parameters of the model type are specified too.
- Fit: Capture patterns from provided data. This is the heart of modeling.
- Predict: Just what it sounds like
- Evaluate: Determine how accurate the model's predictions are.
Here is an example of defining a decision tree model with scikit-learn and fitting it with the features and target variable.
from sklearn.tree import DecisionTreeRegressor
# Define model. Specify a number for random_state to ensure same results each run
melbourne_model = DecisionTreeRegressor(random_state=1)
# Fit model
melbourne_model.fit(X, y)
2
3
4
5
6
7
Many machine learning models allow some randomness in model training. Specifying a number for random_state
ensures you get the same results in each run. This is considered a good practice. You use any number, and model quality won't depend meaningfully on exactly what value you choose.
We now have a fitted model that we can use to make predictions.
In practice, you'll want to make predictions for new houses coming on the market rather than the houses we already have prices for. But we'll make predictions for the first few rows of the training data to see how the predict function works.
print("Making predictions for the following 5 houses:")
print(X.head())
print("The predictions are")
print(melbourne_model.predict(X.head()))
2
3
4
# 3.4 Exercise: Your First Machine Learning Model
Try it out yourself in the Model Building Exercise (opens new window)
# Step 1: Specify Prediction Target
Select the target variable, which corresponds to the sales price. Save this to a new variable called y
. You'll need to print a list of the columns to find the name of the column you need.
# print the list of columns in the dataset to find the name of the prediction target
home_data.describe()
2
y = home_data.SalePrice
step_1.check()
2
3
# Step 2: Create X
Now you will create a DataFrame called X
holding the predictive features.
Since you want only some columns from the original data, you'll first create a list with the names of the columns you want in X
.
You'll use just the following columns in the list (you can copy and paste the whole list to save some typing, though you'll still need to add quotes): * LotArea * YearBuilt * 1stFlrSF * 2ndFlrSF * FullBath * BedroomAbvGr * TotRmsAbvGrd
After you've created that list of features, use it to create the DataFrame that you'll use to fit the model.
# Create the list of features below
feature_names = ['LotArea','YearBuilt','1stFlrSF','2ndFlrSF','FullBath','BedroomAbvGr','TotRmsAbvGrd']
# select data corresponding to features in feature_names
X = home_data[feature_names]
step_2.check()
2
3
4
5
6
7
# Review Data
Before building a model, take a quick look at X to verify it looks sensible
# Review data
# print description or statistics from X
print(X.describe())
# print the top few lines
print(X.head())
2
3
4
5
6
7
# Step 3: Specify and Fit Model
Create a DecisionTreeRegressor
and save it iowa_model. Ensure you've done the relevant import from sklearn to run this command.
Then fit the model you just created using the data in X
and y
that you saved above.
from sklearn.tree import DecisionTreeRegressor
#specify the model.
#For model reproducibility, set a numeric value for random_state when specifying the model
iowa_model = DecisionTreeRegressor(random_state=1)
# Fit the model
iowa_model.fit(X,y)
step_3.check()
2
3
4
5
6
7
8
9
# Step 4: Make Predictions
Make predictions with the model's predict
command using X
as the data. Save the results to a variable called predictions
.
predictions = iowa_model.predict(X)
print(predictions)
step_4.check()
2
3
4
# Think About Your Results
Use the head
method to compare the top few predictions to the actual home values (in y
) for those same homes. Anything surprising?
You'll understand why this happened if you keep going.