MACHINE LEARNING • LESSON 5

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.

THE SIMPLE IDEA

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.

01

Start With Your Dataset

Imagine that we want to predict house prices.

Our dataset contains:

Size Bedrooms Location Price
1500 2 City A 300000
2000 3 City A 400000
2500 4 City B 550000

We already know from the previous topics that Size, Bedrooms, and Location are features.

Price is the label we want the model to predict.

02

Put the Features Into X

In Python, we commonly use X to represent the input features.

X Input Features

Size

Bedrooms

Location

Think of X as:

"What information am I giving to the model?" → That information goes into X.
03

Put the Label Into y

We commonly use y to represent the target or label.

y Target / Label

Price

Think of y as:

"What answer am I asking the model to predict?" → That answer goes into y.
04

X and y Together

Now we can connect the two ideas.

X Features

Size, Bedrooms, Location

MODEL Learns Patterns
y Target

Price

During training, the model receives X together with the known values of y and learns the relationship between them.

05

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.

06

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.

07

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.

X House Information

1500 sq ft, 2 bedrooms

y Correct Answer

$300,000

The model learns from many of these feature-and-answer pairs.

08

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.

09

The Important Pattern

RAW DATA Dataset
SELECT Useful Features
X Model Input
y Target
REMEMBER THIS

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.

QUICK CHECK

What Goes Into X and y?

We want to predict whether a customer will purchase a product.

Age Feature → X
Previous Purchases Feature → X
Purchased: Yes / No Target → y
Answer

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.

LESSON 5 COMPLETE

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.

NEXT LESSON

Lesson 6 — Data Preparation

Real-world data is rarely ready for a machine learning model. Next, we will learn how to handle missing values, duplicate data, incorrect data, outliers, categorical values, feature scaling, and other preparation steps.