0
0
ML Pythonml~20 mins

Custom transformers in ML Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Custom Transformer Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
A
[[1 2]
 [3 4]]
BTypeError
C
[[0.5 1]
 [1.5 2]]
D
[[2 4]
 [6 8]]
Attempts:
2 left
💡 Hint
Think about what multiplying by two does to each element in the array.
Model Choice
intermediate
1: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?
Atransform
Bscore
Cpredict
Dfit_transform
Attempts:
2 left
💡 Hint
This method changes the data without learning from it.
Hyperparameter
advanced
2: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?
AIt changes the amount added to each feature during transformation.
BIt changes the number of features in the output.
CIt changes the data type of the output features.
DIt changes the order of features in the output.
Attempts:
2 left
💡 Hint
Think about what adding a constant means for each feature value.
🔧 Debug
advanced
2: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])
AValueError because fit was not called before transform
BAttributeError because transform method is missing
CTypeError because input is a list, not a numpy array
DNo error, output is [2, 3, 4]
Attempts:
2 left
💡 Hint
Check the input type and what adding 1 means for a list.
🧠 Conceptual
expert
2: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?
ATo avoid using any external libraries for data processing
BTo reuse specific data processing steps consistently and integrate them seamlessly in pipelines
CTo replace the model training step with a simpler approach
DTo automatically tune hyperparameters during training
Attempts:
2 left
💡 Hint
Think about how pipelines help organize repeated steps.