Feature Distribution and Target Analysis
Analyzing feature-target relationships, class imbalance detection, mutual information.
Feature Distribution and Target Analysis is a free Learn AI with Python lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Learn AI with Python learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Why Analyze Features Against the Target?
The target is the variable you want to predict. The point of EDA for modeling is to learn which features actually relate to that target.
This lesson covers feature-vs-target plots, measuring informativeness with mutual information, detecting class imbalance, and fixing skew with transforms.
Feature vs Target — Numeric Target
For a numeric target, a scatter plot of feature vs target shows whether the feature carries signal.
A clear trend means the feature is predictive; a shapeless cloud means it probably is not.
import seaborn as sns
import matplotlib.pyplot as plt
sns.scatterplot(x="rooms", y="price", data=df, alpha=0.4)
plt.show()Feature vs Target — Categorical Target
When the target is a class label, compare the feature distribution within each class. Overlapping KDEs mean the feature separates classes poorly; well-separated curves mean it is useful.
sns.kdeplot(data=df, x="balance", hue="churned", common_norm=False, fill=True)
plt.title("Balance by churn status")
plt.show()Comparing Class Distributions with Box Plots
Grouped box plots are another way to see feature-vs-class relationships: put the target class on x and the feature on y.
Different medians across classes indicate the feature helps distinguish them.
sns.boxplot(x="churned", y="tenure", data=df)
plt.show()Detecting Class Imbalance
Class imbalance happens when one target class vastly outnumbers another (e.g. 95% not-fraud, 5% fraud). It misleads accuracy and biases models toward the majority class.
Check it with a simple value count on the target.
print(df["churned"].value_counts())
# 0 9200
# 1 800value_counts(normalize=True) for Proportions
Raw counts can hide how severe imbalance is. normalize=True turns counts into proportions, so you read it as a percentage directly.
print(df["churned"].value_counts(normalize=True))
# 0 0.92
# 1 0.08 -> only 8% positive classWhy Imbalance Matters
With 92% negatives, a model that always predicts "no" scores 92% accuracy while being useless. That is why you watch imbalance early.
Remedies include resampling (oversample minority / undersample majority), class weights, or metrics like precision, recall, and F1 instead of accuracy.
Mutual Information — Measuring Informativeness
Mutual information measures how much knowing a feature reduces uncertainty about the target. Unlike correlation, it captures non-linear relationships too.
For classification targets, scikit-learn provides mutual_info_classif.
from sklearn.feature_selection import mutual_info_classif
X = df[["balance", "tenure", "rooms"]]
y = df["churned"]
mi = mutual_info_classif(X, y, random_state=42)
print(dict(zip(X.columns, mi)))Ranking Features by Mutual Information
Putting MI scores in a sorted Series gives a quick feature-importance ranking. Higher MI means the feature is more informative about the target.
This is a great early filter before training any model.
import pandas as pd
from sklearn.feature_selection import mutual_info_classif
mi = mutual_info_classif(X, y, random_state=42)
scores = pd.Series(mi, index=X.columns).sort_values(ascending=False)
print(scores)Log Transform for Skewed Features
Right-skewed features (income, counts, prices) often behave better after a log transform, which compresses the long tail toward a more symmetric shape.
Use np.log1p (log of 1 + x) so it safely handles zeros.
import numpy as np
df["income_log"] = np.log1p(df["income"])
print(df["income"].skew(), "->", df["income_log"].skew())Before-and-After: Visualizing the Transform
Always confirm a transform helped by plotting before and after. The log version should look closer to a symmetric bell shape.
import numpy as np
import matplotlib.pyplot as plt
fig, ax = plt.subplots(1, 2)
df["income"].hist(ax=ax[0]); ax[0].set_title("Raw")
np.log1p(df["income"]).hist(ax=ax[1]); ax[1].set_title("log1p")
plt.show()Quick Check: Skewed Feature Fix
A feature is strongly right-skewed and contains some zero values.
Recap: Feature and Target Analysis
You learned to connect features to the prediction target:
- Scatter / KDE / box plots to see feature-vs-target signal
value_counts(normalize=True)to detect and quantify class imbalancemutual_info_classifto rank features by informativeness, including non-linear linksnp.log1pto tame right-skewed features
That completes Exploratory Data Analysis. Next: collecting data from web APIs.
Frequently asked questions
Is the “Feature Distribution and Target Analysis” lesson free?
Yes — the full text of “Feature Distribution and Target Analysis” is free to read here on the web, and the Learn AI with Python course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Learn AI with Python course, upgrade to CoddyKit PRO.
What will I learn in “Feature Distribution and Target Analysis”?
Analyzing feature-target relationships, class imbalance detection, mutual information. You practise Learn AI with Python with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Learn AI with Python?
No prior experience is required. Learn AI with Python on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Feature Distribution and Target Analysis” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Learn AI with Python lesson?
Yes. Every Learn AI with Python lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- EDA Workflow and Data Profiling
- Univariate Analysis
- Bivariate and Multivariate Analysis
- Feature Distribution and Target Analysis