Step 3 — Feature Engineering

** Introduction to Machine Learning & AI with Python
Lesson Content
0% Complete

Step 3 — Feature Engineering

Phase: Data Engineering

“Now we prepare the data for the model. Think of it as translating your product catalog into the language a computer understands.”

Machine learning algorithms work with numbers. Ordinal categories like ‘Fair → Ideal’ or ‘J → D’ need to be converted into meaningful numeric values.

 

3a. Encode Ordinal Categories

cut_order     = [‘Fair’, ‘Good’, ‘Very Good’, ‘Premium’, ‘Ideal’]

color_order   = [‘J’, ‘I’, ‘H’, ‘G’, ‘F’, ‘E’, ‘D’]

clarity_order = [‘I1’, ‘SI2’, ‘SI1’, ‘VS2’, ‘VS1’, ‘VVS2’, ‘VVS1’, ‘IF’]

diamonds[‘cut_enc’]     = diamonds[‘cut’].map({v: i for i, v in enumerate(cut_order)})

diamonds[‘color_enc’]   = diamonds[‘color’].map({v: i for i, v in enumerate(color_order)})

diamonds[‘clarity_enc’] = diamonds[‘clarity’].map({v: i for i, v in enumerate(clarity_order)})

 

3b. Remove Redundant Features

The columns x, y, z (physical dimensions) are near-perfect proxies for carat. Keeping all of them introduces multicollinearity — the model gets confused by features that say the same thing twice.

diamonds = diamonds.drop(columns=[‘x’, ‘y’, ‘z’, ‘cut’, ‘color’, ‘clarity’])

 

3c. Log-Transform the Target Variable (Optional but Recommended)

import numpy as np

diamonds[‘log_price’] = np.log(diamonds[‘price’])

3d. Final Feature Set

features = [‘carat’, ‘cut_enc’, ‘color_enc’, ‘clarity_enc’, ‘depth’, ‘table’]

target   = ‘price’   # or ‘log_price’

 

X = diamonds[features]

y = diamonds[target]

 

💡 Marketing check: We now have 6 clean, numeric features. Every diamond in your catalog can be described by just these 6 numbers — and the model will use those 6 numbers to predict its price.

Course Outline