Engineering Python · NumPy & pandas Basics
Tables for ML
In Engineering Python because AI/ML/DL labs assume you can build a feature table — rows as examples, columns as features — before fitting anything.
Supervised labs expect a feature matrix X and a target y. In pandas terms: each row is one example; feature columns are inputs; one column (or Series) is the label. This bridge topic builds a tiny table you could hand to an estimator later — without importing sklearn here.
- Engineering Python
- Medium level
- 4 concepts
1Rows are examples
Each row is one observation: one student, one sensor tick, one loan. Mixing two examples into one row breaks the learning setup.
If you later shuffle or split the table, you move whole rows together — features and label for the same example stay aligned.
Figure. Each row is one observation — one student, one sensor tick, or one loan. Features and the label for that example stay on the same row.
Takeaway
- AlignmentRow i of X and index i of y must describe the same example.
In a feature table for ML, what is a row?
- One example / observation
- One model weight
- One Python file
Rows = examples.
2Columns are features (and target)
Feature columns are inputs the model may use. The target column is what you predict — keep it separate from X when you fit later.
A common bug is leaving the label inside X: the model 'predicts' by reading the answer column. Drop the target before any fit.
No diagram — the idea is carried by the prose, code block or coding lab.
| hours | prep | pass |
|---|---|---|
| 2 | 1 | 0 |
| 5 | 1 | 1 |
| 1 | 0 | 0 |
| 6 | 1 | 1 |
Which column must stay out of X when predicting pass?
- pass (the target)
- hours only
- Every numeric column
Targets are labels, not features at train time.
3Splitting X and y
Convention: `X = df[['hours','prep']]` and `y = df['pass']`. Fitting belongs in Machine Learning; here you only shape the table correctly.
Check `X.shape` (examples × features) and that `len(y)` matches the row count before you trust a later lab.
Figure. X is the hours and prep columns: four rows, two features. y is the pass column [0, 1, 0, 1]. Keep the target out of X.
X and y
import pandas as pd
df = pd.DataFrame({
'hours': [2, 5, 1, 6],
'prep': [1, 1, 0, 1],
'pass': [0, 1, 0, 1],
})
X = df[['hours', 'prep']]
y = df['pass']
print(X.shape, list(y))For four examples and two feature columns, what is X.shape?
- (4, 2)
- (2, 4)
- (4,)
- (6, 1)
Rows first, then feature columns.
4Lab: build a feature table
Construct X/y from a tiny frame and print the shape the check expects. No model fitting — table shape only.
Figure. X is hours and prep, so X.shape is (4, 2). y is pass. The lab prints that shape; it does not fit a model.
Coding lab. Build X and y runs in the app, with checks on your output.
Why does this course stop before calling fit?
- Engineering Python owns table shape; model fitting is Machine Learning
- fit is illegal in Python
- pandas cannot hold labels
Keep the boundary: tables here, estimators later.
Notes
- In Engineering Python because AI/ML/DL labs assume you can build a feature table — rows as examples, columns as features — before fitting anything.
- Each row is one observation: one student, one sensor tick, one loan. Mixing two examples into one row breaks the learning setup.
- Feature columns are inputs the model may use. The target column is what you predict — keep it separate from X when you fit later.
Exam traps & shortcuts
- Run one cell at a time and read stdout before changing more lines.
- Names are labels for values; rebinding a name does not rewrite old prints.
Recap
Rows=examples, columns=features; split X from y; fitting waits for ML.
- Rows are examples
- Each row is one observation: one student, one sensor tick, one loan. Mixing two examples into one row breaks the learning setup.
- Columns are features (and target)
- Feature columns are inputs the model may use. The target column is what you predict — keep it separate from X when you fit later.
- Splitting X and y
- Convention: `X = df[['hours','prep']]` and `y = df['pass']`. Fitting belongs in Machine Learning; here you only shape the table correctly.
- Lab: build a feature table
- Construct X/y from a tiny frame and print the shape the check expects. No model fitting — table shape only.
Practise Tables for ML
Reading is free and needs no account. Practice, mocks and progress live in the app.
- A 3-question practice set that ends the chapter
- 4 quick checks with worked explanations
- Timed mocks scored with the real marking scheme
- Readiness tracked per topic, kept on your device