Step 2 — Exploratory Data Analysis (EDA)

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

Step 2 — Exploratory Data Analysis (EDA)

Phase: Data Analysis

“Before we let a machine learn anything, we need to learn something ourselves. EDA is how you get to know your data.”

This is arguably the most important step. EDA tells you what the data is really saying — not just what you assumed going in.

 

2a. Check for Missing Values

print(diamonds.isnull().sum())

Good news: the seaborn diamonds dataset has zero missing values. In real-world marketing data, you’d rarely be this lucky.

 

2b. Understand the Distribution of Price

import matplotlib.pyplot as plt

diamonds[‘price’].hist(bins=50, figsize=(10, 4))

plt.title(‘Distribution of Diamond Prices’)

plt.xlabel(‘Price (USD)’)

plt.show()

 

What you’ll notice: Price is heavily right-skewed — most diamonds are in the $500–$3,000 range, but a small number fetch $15,000+. This tells us we may want to use a log transformation on price later.

💡 Marketing insight: Your inventory has a long tail. A small percentage of premium diamonds represent a disproportionate share of revenue potential. Knowing this helps you decide where to invest marketing spend.

 

2c. What Drives Price? (The Most Important Question)

corr = diamonds[[‘carat’,’depth’,’table’,’price’,’x’,’y’,’z’]].corr()

sns.heatmap(corr, annot=True, cmap=’coolwarm’)

 

Key finding: carat has a correlation of ~0.92 with price. That’s a near-perfect linear relationship. The physical dimensions (x, y, z) are similarly correlated because they’re directly related to carat weight.

💡 What this tells you as a marketer: Weight is king. But notice the spread — two diamonds with the same carat can differ by thousands of dollars. That’s where cut, color, and clarity explain the premium.

 

2d. Categorical Features

fig, axes = plt.subplots(1, 3, figsize=(15, 4))

for ax, col in zip(axes, [‘cut’, ‘color’, ‘clarity’]):

    diamonds.groupby(col)[‘price’].median().sort_values().plot(kind=’bar’, ax=ax)

    ax.set_title(f’Median price by {col}’)

plt.tight_layout()

plt.show()

 

Surprising insight: ‘Ideal’ cut diamonds have a lower median price than ‘Premium.’ This seems counterintuitive — until you realize Ideal cuts tend to be smaller stones. The cut grade doesn’t exist in isolation; it interacts with carat.

💡 This is exactly why machine learning is valuable here. Human intuition might misprice diamonds because it can’t simultaneously process all feature interactions. A model can.

Course Outline