C2W3 assignment list(select_k_best) error

Here is the beginning: I do not believe erros occur here:

START CODE HERE

Create SelectKBest object using f_classif (ANOVA statistics) for 8 classes

select_k_best = SelectKBest(f_classif, k=8)

Fit and transform the input data using select_k_best

X_new = select_k_best.fit_transform(X,y)

Extract the features which are selected using get_support API

features_mask = select_k_best.get_support(indices=False)

END CODE HERE

Print the results

reqd_cols = pd.DataFrame({‘Columns’: df_num.columns, ‘Retain’: features_mask})
print(reqd_cols)

Get the feature names from SelectKBest

feature_names = list(select_k_best)

list(reqd_cols.Columns)

TypeError Traceback (most recent call last)
in
1 # Get the feature names from SelectKBest
----> 2 feature_names = list(select_k_best)
3 # list(reqd_cols.Columns)
4
5 # Append the categorical and label columns

TypeError: ‘SelectKBest’ object is not iterable

I am not sure why this error is coming up. I suppose that the object “select_k_best” created above is not iterable. Need some help with this one please.

Daniel

select_k_best is an instance of sklearn.feature_selection.SelectKBest. Think about how you can use df_num.columns and features_mask to get the subset of columns.

One way to think about this is that you can use boolean array to specify the subset of columns you want. For instance, if you have some_dataframe.columns as ['c1', 'c2', 'c3'] and you specified some_dataframe.columns[[True, False, True]], the result would be ['c1', 'c3']

Thank you for letting me see this. It helped

Daniel