Preparing Features for Python
Before we train a machine learning model, our features need to be organized in a form that Python and machine learning libraries can work with.
Put your features into X and your target into y.
In Python machine learning, it is common to store the input features in a variable called X and the target or label in a variable called y.
Start With Your Dataset
Imagine that we want to predict house prices.
Our dataset contains:
We already know from the previous topics that Size, Bedrooms, and Location are features.
Price is the label we want the model to predict.
Put the Features Into X
In Python, we commonly use X to represent the input features.
Size
Bedrooms
Location
Think of X as:
Put the Label Into y
We commonly use y to represent the target or label.
Price
Think of y as:
X and y Together
Now we can connect the two ideas.
Size, Bedrooms, Location
Price
During training, the model receives X together with the known values of y and learns the relationship between them.
A Simple Python Example
We can represent a simple house dataset in Python like this:
import pandas as pd
data = {
"size": [1500, 2000, 2500],
"bedrooms": [2, 3, 4],
"price": [300000, 400000, 550000]
}
df = pd.DataFrame(data)
X = df[["size", "bedrooms"]]
y = df["price"]
Here, X contains the features we selected: size and bedrooms.
y contains the value we want to predict: price.
What Does X Actually Look Like?
When we select multiple features, X contains multiple columns.
X
size bedrooms
1500 2
2000 3
2500 4
Each row represents one example, such as one house.
Each column represents one selected feature.
What Does y Look Like?
The target contains the answer for each training example.
y
300000
400000
550000
These values correspond to the houses in X.
1500 sq ft, 2 bedrooms
$300,000
The model learns from many of these feature-and-answer pairs.
What About Categorical Features?
Earlier we learned that features can also be categorical, such as city or membership type.
For example:
X
size bedrooms city
1500 2 City A
2000 3 City A
2500 4 City B
This is useful for understanding the dataset, but there is an important issue: many machine learning algorithms cannot directly work with text such as "City A" and "City B".
We will learn how to convert categorical values into a usable numerical representation later in Lesson 6 — Data Preparation.
The Important Pattern
X = Inputs, y = Target
X contains the selected features that we give to the model. y contains the target or label that the model learns to predict.
What Goes Into X and y?
We want to predict whether a customer will purchase a product.
Age and previous purchases are input features, so they belong in X. Purchased is the target we want to predict, so it belongs in y.
You Now Understand Features and Labels
You can now identify features and labels, distinguish numerical and categorical features, select useful features, and understand how those features are organized as X and y in Python.