0
0
ML Pythonml~10 mins

Custom transformers in ML Python - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the base class for creating a custom transformer in scikit-learn.

ML Python
from sklearn.base import [1]
Drag options to blanks, or click blank then click option'
ABaseEstimator
BTransformerMixin
CCustomTransformer
DPipeline
Attempts:
3 left
💡 Hint
Common Mistakes
Using TransformerMixin instead of BaseEstimator for inheritance.
Trying to import a non-existent class named CustomTransformer.
2fill in blank
medium

Complete the code to define the fit method for a custom transformer class.

ML Python
class MyTransformer(BaseEstimator, TransformerMixin):
    def fit(self, X, y=None):
        # No fitting needed, just return self
        return [1]
Drag options to blanks, or click blank then click option'
Aself
By
CX
DNone
Attempts:
3 left
💡 Hint
Common Mistakes
Returning the input data X instead of self.
Returning None which breaks chaining.
3fill in blank
hard

Fix the error in the transform method to correctly transform input data by doubling all values.

ML Python
def transform(self, X):
    return X [1] 2
Drag options to blanks, or click blank then click option'
A+
B*
C-
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using addition instead of multiplication.
Using division or subtraction which changes values incorrectly.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps words to their lengths only if length is greater than 3.

ML Python
{word: [1] for word in words if len(word) [2] 3}
Drag options to blanks, or click blank then click option'
Alen(word)
B>
C<
Dword
Attempts:
3 left
💡 Hint
Common Mistakes
Using word instead of len(word) as the dictionary value.
Using less than operator < which filters wrong words.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps uppercase words to their counts only if count is greater than zero.

ML Python
result = {{ [1]: [2] for word, count in word_counts.items() if count [3] 0 }}
Drag options to blanks, or click blank then click option'
Aword.upper()
Bcount
C>
Dword.lower()
Attempts:
3 left
💡 Hint
Common Mistakes
Using word.lower() instead of uppercase.
Using less than operator < which filters wrong counts.