Lesson 09
Feature Engineering
Nine beginner steps on five students: same study hours, different marks, and a pandas column for hours times attendance.
What You Will Learn
Nine steps. Five students. One new column, built with pandas. Click a step to jump.
- 1. What is feature engineering?
- 2. Types of feature engineering
- 3. Where is it used?
- 4. Where it sits before the model
- 5. What problem does the raw table have?
- 6. How the new column fixes the marks
- 7. Key Takeaways
- 8. Mind Map
- 9. Quiz
1. What is feature engineering?
You already collected the columns. Feature engineering is the step that builds a clearer column from those, before any model tries to predict.
Look at student B. The collected numbers are 5 hours and attended = 1. Marks are 80. The useful extra number is not a new survey question. It is 5 × 1 = 5: hours that were actually spent in class.
The whole table:
| Student | Hours | Attended | Marks |
|---|---|---|---|
| A | 5 | 0, missed | 30 |
| B | 5 | 1, came | 80 |
| C | 2 | 1, came | 50 |
| D | 8 | 0, missed | 30 |
| E | 6 | 1, came | 90 |
A and B studied the same 5 hours. A missed class and scored 30. B came and scored 80. D studied the most, 8 hours, and still scored 30, because D also missed class.
The rule that fits every row:
marks = 30 + 10 × hours × attended
Check B: 30 + 10 × 5 × 1 = 80. Check D: 30 + 10 × 8 × 0 = 30.
2. Types of feature engineering
On this page you collected 2 columns and calculated 1.
| Kind | What you do | On this table |
|---|---|---|
| Calculated column | A rule you already believe, written as a new column | hours × attended |
| Word to number | A model cannot multiply the word "yes" | If the cell said yes or no, store 1 or 0 first |
| Fill a blank | One empty cell should not erase the row | A missing attended cell needs a deliberate fill, not a silent guess |
| Bin | Group a number into a few labels | Hours under 4 as short, 4 or more as long |
The calculated column is the one we will build. The original hours and attended columns stay. You added a column. You did not throw one away. Throwing columns away is a different job, closer to dimensionality reduction.
3. Where is it used?
Any table where a combination says more than the raw cells.
| Job | A column you might calculate |
|---|---|
| This lesson | Hours in class = hours × attended |
| Prices | Price per square unit, from price and size |
| Shops | Visits this week, from a list of visit days |
| Health | A ratio of two measurements already on the row |
| Text | A count of a chosen word, from the raw text |
The pattern is the same: the new column is a fact you can explain in one sentence.
4. Where it sits before the model
The order is fixed.
- Collect hours, attended, and marks.
- Build hours × attended.
- Only then let a model, such as the trees in gradient boosting, see the rows.
A model that receives only hours still sees A and B as the same kind of student. The next section shows that scatter. The section after it draws hours × attended, which separates them.
5. What problem does the raw table have?
Hours do not name the marks. A and B are both at 5 hours, with marks 30 and 80. D is at 8 hours and still at 30.
A word cannot be multiplied. If attended stored "yes" and "no", the product hours × attended would not be a number. Map yes to 1 and no to 0 first.
Do not build the column from the answer. Marks are what you are trying to predict. A column such as marks × 2 repeats the answer and will look brilliant on these five rows and fail on a new student. Hours and attended were known before the exam. Marks were not.
Extra columns can memorize. One honest product is useful. A pile of products you cannot explain can hug these five students and miss the next one. That is the same worry as a heavy fine in regularization.
6. How the new column fixes the marks
This page needs one module: pandas. It is the library that holds tables. The playground fetches it when the first line runs. NumPy is not required. scikit-learn is not required.
| Piece | What it means here |
|---|---|
import |
Load a module into this notebook |
pandas |
The table module |
as pd |
The short name. Later lines say pd, not pandas |
pd.DataFrame |
Build the table from named columns |
students["hours"] |
The hours column |
* |
Multiply two columns, one student at a time |
import pandas as pd
students = pd.DataFrame({
"hours": [5, 5, 2, 8, 6],
"attended": [0, 1, 1, 0, 1],
"marks": [30, 80, 50, 30, 90],
})
students["hours_in_class"] = students["hours"] * students["attended"]
print(students)import pandas as pd is the whole requirements line. DataFrame is called on pd because that is where the table builder lives. The new column name is hours_in_class. The right-hand side multiplies the two collected columns. Adding a column this way is the same idea as the pandas column lesson.
The printed table:
| hours | attended | marks | hours_in_class | |
|---|---|---|---|---|
| A, row 0 | 5 | 0 | 30 | 0 |
| B, row 1 | 5 | 1 | 80 | 5 |
| C, row 2 | 2 | 1 | 50 | 2 |
| D, row 3 | 8 | 0 | 30 | 0 |
| E, row 4 | 6 | 1 | 90 | 6 |
If the attended column had held words, you would number them before the multiply:
students["attended"] = students["attended"].replace({"yes": 1, "no": 0})Our table is already 0 and 1, so that line is not needed. The playground below runs the first cell only.
Read one row against the formula. Student E: hours 6, attended 1, product 6, marks 30 + 10 × 6 = 90.
7. Key Takeaways
- Feature engineering builds a column you can explain from columns you already have.
- A and B both studied 5 hours. Attendance makes the marks 30 and 80.
- The column is hours × attended.
- marks = 30 + 10 × that column on these five rows.
- The only module required here is pandas, loaded with
import pandas as pd. - Do not build the new column from the marks. Those are the answer.
- The same cell is in the playground. Run it and check row B: product 5, marks 80.
8. Mind Map
Cover B and say the three numbers: hours 5, product 5, marks 80. A matches the hours and not the other two.
9. Quiz
The questions use this table. A is 5 hours, missed, marks 30. B is 5 hours, attended, marks 80. The import line is import pandas as pd.
Loading playground…
Check your understanding
Feature Engineering Quiz
Students A and B both studied 5 hours. A missed class and scored 30. B attended and scored 80. What does the new column store?
1 / 6