0
0
ML Pythonml~20 mins

Feature union in ML Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Feature Union Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
What does FeatureUnion do in a machine learning pipeline?

Imagine you want to combine different sets of features extracted from the same data before training a model. What is the main purpose of using FeatureUnion in this context?

AIt combines multiple feature extraction processes by concatenating their outputs into a single feature set.
BIt selects the best single feature extractor among many to use for the model.
CIt reduces the dimensionality of features by applying PCA on the combined features.
DIt splits the dataset into multiple parts to train separate models independently.
Attempts:
2 left
💡 Hint

Think about how you can use different ways to get features and then join them together before training.

Predict Output
intermediate
2:00remaining
Output shape after FeatureUnion transformation

Given the following code, what is the shape of X_transformed?

ML Python
from sklearn.pipeline import FeatureUnion
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA
import numpy as np

X = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

fu = FeatureUnion([
    ('scale', StandardScaler()),
    ('pca', PCA(n_components=1))
])

X_transformed = fu.fit_transform(X)
A(3, 4)
B(3, 2)
C(3, 3)
D(3, 1)
Attempts:
2 left
💡 Hint

Consider the output dimensions of each transformer and how FeatureUnion combines them.

Model Choice
advanced
2:00remaining
Choosing transformers for FeatureUnion

You want to build a FeatureUnion that combines text features and numeric features for a classification task. Which combination of transformers is most appropriate?

AUse <code>PCA</code> for text features and <code>OneHotEncoder</code> for numeric features.
BUse <code>CountVectorizer</code> for numeric features and <code>MinMaxScaler</code> for text features.
CUse <code>TfidfVectorizer</code> for text and <code>StandardScaler</code> for numeric features.
DUse <code>LabelEncoder</code> for text and <code>Normalizer</code> for numeric features.
Attempts:
2 left
💡 Hint

Think about which transformers are designed for text and which for numeric data.

Hyperparameter
advanced
2:00remaining
Effect of n_jobs parameter in FeatureUnion

What is the effect of setting n_jobs=-1 in a FeatureUnion?

AIt disables parallelism and runs transformers sequentially.
BIt runs all transformers in parallel using all available CPU cores.
CIt limits the number of features to one per transformer.
DIt automatically tunes hyperparameters of each transformer.
Attempts:
2 left
💡 Hint

Consider what n_jobs=-1 usually means in scikit-learn.

🔧 Debug
expert
3:00remaining
Why does this FeatureUnion pipeline raise a ValueError?

Consider this code snippet:

from sklearn.pipeline import FeatureUnion
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA
import numpy as np

X = np.array([[1, 2], [3, 4], [5, 6]])

fu = FeatureUnion([
    ('scale', StandardScaler()),
    ('pca', PCA(n_components=3))
])

X_transformed = fu.fit_transform(X)

Why does this code raise a ValueError?

AThe input array X must be 1-dimensional for FeatureUnion.
BStandardScaler cannot process arrays with less than 3 rows.
CFeatureUnion requires all transformers to output the same number of features.
DPCA is asked to extract more components (3) than the number of features (2) in X.
Attempts:
2 left
💡 Hint

Check the shape of X and the n_components parameter of PCA.