ML Fundamentals: lesson 3 of 7
ML Fundamentals
Features, Targets, and Samples
Translate a familiar data table into the inputs and outcomes used by supervised machine learning.
Concept
Supervised learning represents a dataset as samples, features, and a target. A sample is one observation, usually one row. Features are input columns used for prediction. The target, also called a label, is the outcome the model should learn to predict.
Real-World Example
For house-price prediction, each row is one sold house. Size, bedrooms, and age are candidate features; price is the target. The feature matrix is conventionally named capital X, while the target vector is lowercase y.
import pandas as pd
df = pd.DataFrame({
"size_sqft": [900, 1200, 1600, 2100],
"bedrooms": [2, 3, 3, 4],
"age_years": [22, 12, 8, 3],
"price": [180000, 245000, 310000, 415000],
})
X = df[["size_sqft", "bedrooms", "age_years"]]
y = df["price"]
X is a table with one row per house and one column per feature. y is a single series aligned with those rows. Numerical features are directly numeric; categorical features need later preprocessing before many models can use them.
Reading a Table as a Model Would
The same table has several names for the same basic unit: one row, observation, or sample represents one house. A feature is an input variable such as size_sqft; a target or label is the answer to learn, here price. Capital X conventionally holds the feature matrix because it may contain many columns. Lowercase y holds one target value for each matching sample.
For a churn dataset, tenure_months, monthly_charges, and contract_type could be candidate features, while churn is the target. The target changes with the decision: a spending forecast might use monthly spend as y instead. Defining y first keeps the project focused.
What Belongs in X?
A column is not automatically a good feature. customer_id often identifies a row without describing a useful pattern. A cancellation date recorded after churn must not be used to predict churn because it reveals future information. At prediction time, ask: “Would this value genuinely be available for this new case?” That simple question prevents many leakage mistakes.
| Column | Example judgment | Why |
|---|---|---|
tenure_months | Good candidate | Available before a churn prediction and may describe behavior. |
customer_id | Questionable | Usually identifies a row rather than a stable customer pattern. |
account_manager_notes | Requires review | May be inconsistent or written after risk became known. |
cancellation_date | Leaky | It is only known after the event being predicted. |
An identifier can occasionally be useful for joining or grouping, but that does not make it a sensible numeric predictor. Likewise, a column can exist in a DataFrame and still be invalid, unavailable, or misleading at prediction time.
Interpretation
The same table can have different targets for different decisions. For a churn model, churn might be y; for a spend forecast, monthly spend might be y. The target defines the question, so include it deliberately and never accidentally place it inside X.
Failure Signals
Common Mistakes
- Including the target inside the feature matrix.
- Using IDs as meaningful numeric measurements.
- Including columns created after the predicted event.
- Forgetting that X and y rows must stay aligned.
Best Practices
Document row grain, target timing, feature meaning, and known exclusions. Inspect X.shape and y.shape before fitting a model, and choose features based on what would be available in real use.
Interview Perspective
Question: Why are X and y common names? Answer: they conventionally distinguish the matrix of inputs from the vector of outcomes. What the interviewer is testing: whether you understand data representation before discussing algorithms. Follow-up: why might an ID be a poor feature?
Practice Questions
- Identify X and y for a customer churn table.
- Why is a post-cancellation refund field unsafe for churn prediction?
- Write Pandas code to select
ageandtenureas features. - A table contains
customer_id,contract_type,support_calls,churn, andcancellation_reason. Which columns would you exclude from X for a pre-churn prediction, and why? - Why must the rows of X and y stay aligned after filtering a DataFrame?
Quick Quiz
- What does one sample usually represent? Answer: one observation or row.
- What is the target? Answer: the outcome to predict.
- Does a table column always belong in X? Answer: no.
Key Takeaway
Key Takeaways
Samples are rows, X contains appropriate prediction inputs, and y contains the outcome. Good ML starts by defining which information is valid at prediction time.
Next Lesson
Next, use X and y to train a small scikit-learn model end to end.
Finish this lesson on your terms
Mark it complete when you have worked through the material and are ready to move on.