0
0
ML Pythonml~10 mins

Feature selection methods 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 SelectKBest feature selector from scikit-learn.

ML Python
from sklearn.feature_selection import [1]
Drag options to blanks, or click blank then click option'
AStandardScaler
BPCA
Ctrain_test_split
DSelectKBest
Attempts:
3 left
💡 Hint
Common Mistakes
Choosing PCA which is for dimensionality reduction, not feature selection.
Using train_test_split which is for splitting data, not feature selection.
2fill in blank
medium

Complete the code to select the top 5 features using SelectKBest with the chi2 scoring function.

ML Python
selector = SelectKBest(score_func=[1], k=5)
Drag options to blanks, or click blank then click option'
Amutual_info_classif
Bf_classif
Cchi2
Dr2_score
Attempts:
3 left
💡 Hint
Common Mistakes
Using f_classif which is for ANOVA F-value, not chi-squared.
Using r2_score which is a regression metric, not a scoring function for feature selection.
3fill in blank
hard

Fix the error in the code to correctly fit the feature selector to the data X and labels y.

ML Python
selector = SelectKBest(score_func=chi2, k=3)
selector.[1](X, y)
Drag options to blanks, or click blank then click option'
Afit
Btransform
Cpredict
Dfit_transform
Attempts:
3 left
💡 Hint
Common Mistakes
Using transform before fitting causes an error.
Using predict is not valid for feature selectors.
4fill in blank
hard

Fill both blanks to create a dictionary of feature scores and select features with scores greater than 10.

ML Python
scores = {feature: score for feature, score in zip(feature_names, selector.scores_)}
selected_features = [feature for feature, score in scores.items() if score [1] [2]]
Drag options to blanks, or click blank then click option'
A>
B10
C<
D5
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' selects low scoring features.
Using 5 instead of 10 changes the threshold.
5fill in blank
hard

Fill all three blanks to create a new dataset X_new with selected features and print its shape.

ML Python
X_new = selector.[1](X)
print('Selected features shape:', X_new.[2])
num_features = X_new.[3]
Drag options to blanks, or click blank then click option'
Atransform
Bshape
Cshape[1]
Dfit
Attempts:
3 left
💡 Hint
Common Mistakes
Using fit instead of transform to get selected features.
Using shape[0] instead of shape[1] to get feature count.