Challenge - 5 Problems
Custom Transformer Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a simple custom transformer
What is the output of the following code when transforming the input data?
ML Python
from sklearn.base import BaseEstimator, TransformerMixin import numpy as np class MultiplyByTwo(BaseEstimator, TransformerMixin): def fit(self, X, y=None): return self def transform(self, X): return X * 2 transformer = MultiplyByTwo() input_data = np.array([[1, 2], [3, 4]]) output = transformer.transform(input_data) print(output)
Attempts:
2 left
💡 Hint
Think about what multiplying by two does to each element in the array.
✗ Incorrect
The transform method multiplies each element by 2, so the output doubles each input value.
❓ Model Choice
intermediate1:30remaining
Choosing the right method to implement in a custom transformer
Which method must be implemented in a custom transformer to apply a transformation to the data?
Attempts:
2 left
💡 Hint
This method changes the data without learning from it.
✗ Incorrect
The transform method applies the transformation to the input data. fit is for learning parameters, transform applies changes.
❓ Hyperparameter
advanced2:00remaining
Effect of a hyperparameter in a custom transformer
Consider a custom transformer that adds a constant value to all features. What effect does changing the constant hyperparameter have?
Attempts:
2 left
💡 Hint
Think about what adding a constant means for each feature value.
✗ Incorrect
The constant hyperparameter controls how much is added to each feature value during transform.
🔧 Debug
advanced2:00remaining
Debugging a custom transformer that fails to transform
Why does this custom transformer raise an error when calling transform?
class AddOne(BaseEstimator, TransformerMixin):
def fit(self, X, y=None):
return self
def transform(self, X):
return X + 1
transformer = AddOne()
transformer.transform([1, 2, 3])
Attempts:
2 left
💡 Hint
Check the input type and what adding 1 means for a list.
✗ Incorrect
Adding 1 to a list causes a TypeError because list + int is invalid. Converting input to numpy array fixes this.
🧠 Conceptual
expert2:30remaining
Why implement a custom transformer in a machine learning pipeline?
What is the main advantage of creating a custom transformer for a machine learning pipeline?
Attempts:
2 left
💡 Hint
Think about how pipelines help organize repeated steps.
✗ Incorrect
Custom transformers allow you to package data processing logic so it can be reused and combined with other steps in a pipeline.