Data Science with Python Module 4: Exploratory Data Analysis and Visualization
Introduction
Exploratory data analysis, or EDA, is how you get to know a dataset before modeling it. You check the shape of each variable, look for outliers, and see which variables move together. Charts do much of the work. This module uses a dataset bundled with scikit-learn, so no download is needed.
Prerequisites
- Module 3 completed
Step-by-Step Guide
Step 1: Load a built-in dataset
scikit-learn includes small real datasets. The diabetes dataset has ten numeric features for 442 patients and a numeric target measuring disease progression. as_frame returns pandas objects.
from sklearn.datasets import load_diabetes
data = load_diabetes(as_frame=True)
df = data.frame
print(df.shape)
df.head()Step 2: Summarize each variable
describe gives count, mean, spread, and quartiles. Compare the mean with the median (50 percent) to spot skew, and look at the minimum and maximum for outliers. For text columns, value_counts shows how often each category appears.
df.describe().T
df["target"].describe()
# for a categorical column:
# df["city"].value_counts(normalize=True)Step 3: Plot distributions
A histogram shows how values are spread, and a box plot shows the median, quartiles, and outliers. Always label axes and give the chart a title so it can be understood on its own.
import matplotlib.pyplot as plt
fig, axes = plt.subplots(1, 2, figsize=(10, 4))
df["target"].plot.hist(bins=25, ax=axes[0], title="Disease progression")
df[["bmi", "bp", "s1"]].plot.box(ax=axes[1], title="Selected features")
plt.tight_layout()
plt.show()Step 4: Look at relationships
A scatter plot shows how two variables relate, and a correlation matrix summarizes all pairs at once. Correlation measures straight-line association from minus one to one. It never proves one thing causes another.
corr = df.corr(numeric_only=True)["target"].sort_values(ascending=False)
print(corr)
df.plot.scatter(x="bmi", y="target", alpha=0.5, title="BMI vs progression")
plt.show()Step 5: Compare groups and write down findings
Group a variable into bands and compare the target across them. Finish EDA by listing what you found in plain sentences: which variables matter, what looks wrong, and what to try next. Those notes guide modeling and become part of your final report.
df["bmi_band"] = pd.qcut(df["bmi"], q=4, labels=["low", "mid-low", "mid-high", "high"])
print(df.groupby("bmi_band", observed=True)["target"].agg(["mean", "median", "count"]))FAQs
- Why are the diabetes features scaled around zero?
- The dataset's features were mean-centered and scaled by the dataset's authors, so values such as BMI are not in their original units. That is fine for learning, but it is a reminder to always check how a dataset was prepared.
Summary
EDA means summarizing every variable, plotting distributions, checking relationships with scatter plots and correlations, comparing groups, and writing your findings down before you model.
Related tutorials
- Databricks Module 8: SQL Warehouses, Performance, Cost, and a First Look at MLflow
- Databricks Module 7: Jobs, Orchestration, and Deploying with Git and Bundles
- Databricks Module 6: Declarative Pipelines and the Medallion Architecture
- Databricks Module 5: Ingesting Files with Auto Loader and COPY INTO
- Databricks Module 4: Unity Catalog, Volumes, and Governance
Comments
Log in or create a free account to join the discussion.