C2W3 Programming Assignment - Stuck in Exercise 1 - Feature Selection

Hello, I am stuck with the code on Exercise 1. I have checked the exercises of week 1 and 2 as well as public available documentation, however still receiving error messages. Your help is appreciated.

Here is the code:


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

Extract the features which are selected using get_support API

features_mask = X_new.get_support(indices=False)


Print the results

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

Thanx in advance,

Hi Jörn

For X_new you should have the following:
X_new = select_k_best.fit_transform(X,y)

Then, features_mask should be:

features_mask = select_k_best.get_support(indices=False)

The reason for this is that X_new is not an instance of select_k_best. Rather, it gets assigned the return result from running fit_transform on select_k_best. This result is simply an array. I can’t see offhand why this is stored. It may just be a standard practice to make the array available in case it is needed. Anyway, whether the output gets stored to X_new or not, by calling fit_transform it gets run on our instance of select_k_best nonetheless (try running the code without assigning the output to X_new). In other words, just have select_k_best.fit_transform(X,y) on that line. You should get the same result.

Let me know if you run into any further problems.