Feature engineering is the process of creating new input variables or modifying existing ones to improve a machine learning model's performance.
Click to reveal answer
beginner
Why is feature engineering important?
It helps models learn better by providing more meaningful or clearer information, often leading to improved accuracy and insights.
Click to reveal answer
beginner
Name three common feature engineering techniques.
1. Creating new features by combining existing ones (e.g., ratios) 2. Encoding categorical variables (e.g., one-hot encoding) 3. Scaling or normalizing numerical features
Click to reveal answer
beginner
How can you create a new feature in pandas?
You can create a new column by performing operations on existing columns, for example: df['new_feature'] = df['A'] + df['B'].
Click to reveal answer
beginner
What is one-hot encoding and when do you use it?
One-hot encoding converts categorical variables into multiple binary columns (0 or 1) to help models understand categories as numbers without implying order.
Click to reveal answer
Which of the following is NOT a feature engineering technique?
ACreating new features from existing data
BRemoving missing values
CEncoding categorical variables
DScaling numerical features
✗ Incorrect
Removing missing values is data cleaning, not feature engineering.
In pandas, how do you create a new feature 'total' by adding columns 'A' and 'B'?
Adf['total'] = df['A'] + df['B']
Bdf.total = df.A + df.B
Cdf.add('total', df['A'], df['B'])
Ddf['total'] = df['A'] * df['B']
✗ Incorrect
Option A correctly creates a new column by adding two columns.
What does one-hot encoding do?
AConverts numerical data into categories
BScales features between 0 and 1
CTurns categorical variables into binary columns
DRemoves duplicate rows
✗ Incorrect
One-hot encoding creates binary columns for each category.
Why might you create a new feature like 'age_group' from 'age'?
ATo group continuous data into meaningful categories
BTo remove outliers
CTo reduce the number of missing values
DTo encode text data
✗ Incorrect
Grouping continuous data into categories can help models capture patterns better.
Which pandas function helps to scale numerical features?
Adf.drop_duplicates()
Bdf.fillna()
Cpd.get_dummies()
Dsklearn.preprocessing.StandardScaler()
✗ Incorrect
StandardScaler from sklearn is used to scale numerical features.
Explain what feature engineering is and why it matters in simple terms.
Think about how changing or adding data columns can help a model learn better.
You got /3 concepts.
Describe how you would create a new feature in a pandas DataFrame using existing columns.
Remember the syntax for adding a new column in pandas.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of feature engineering in pandas?
easy
A. To create new useful columns from existing data
B. To delete unnecessary rows from the dataset
C. To change the data type of columns
D. To visualize data using charts
Solution
Step 1: Understand feature engineering definition
Feature engineering means creating new columns that help models learn better.
Step 2: Compare options with definition
Only To create new useful columns from existing data matches the purpose of creating new useful columns from existing data.
Final Answer:
To create new useful columns from existing data -> Option A
Quick Check:
Feature engineering = create new columns [OK]
Hint: Feature engineering = making new columns from old data [OK]
Common Mistakes:
Confusing feature engineering with data cleaning
Thinking it only changes data types
Mixing it up with data visualization
2. Which of the following is the correct way to create a new column total as sum of columns 2a and 2b in a pandas DataFrame df?
easy
A. df['total'] <- df['2a'] + df['2b']
B. df.total = df.2a + df.2b
C. df['total'] == df['2a'] + df['2b']
D. df['total'] = df['2a'] + df['2b']
Solution
Step 1: Recall pandas syntax for new column
Use df['new_col'] = expression to create new columns.
4. What is wrong with this code to create a new column year from a datetime column date in DataFrame df?
df['year'] = df['date'].year
medium
A. There is no error; the code is correct
B. You must use dt.year to access year from datetime column
C. You should use df.year = df.date.year instead
D. The column date must be converted to string first
Solution
Step 1: Recall how to extract year from datetime column
Use df['date'].dt.year to get year from pandas datetime column.
Step 2: Identify error in code
Code uses df['date'].year which is invalid; .year is not attribute of Series.
Final Answer:
You must use dt.year to access year from datetime column -> Option B
Quick Check:
Use df['date'].dt.year for datetime year [OK]
Hint: Use .dt accessor for datetime column parts [OK]
Common Mistakes:
Using .year directly on Series instead of .dt.year
Converting datetime to string unnecessarily
Assigning to df.year instead of df['year']
5. You have a DataFrame df with columns height_cm and weight_kg. You want to create a new feature bmi using the formula bmi = weight_kg / (height_m)^2, where height in meters is height_cm divided by 100. Which code correctly creates the bmi column?
hard
A. df['bmi'] = df['weight_kg'] / (df['height_cm'] / 100) ** 2
B. df['bmi'] = df['weight_kg'] / df['height_cm'] ** 2 / 10000
C. df['bmi'] = df['weight_kg'] * (df['height_cm'] / 100) ** 2
D. df['bmi'] = df['weight_kg'] / (df['height_cm'] * 100) ** 2