2 years ago

#26218

test-img

Muhammad Ali

Selected Features Column Names in Scikit Learn Feature Selection

Figuring out which features were selected from the main dataframe is a very common problem data scientists face while doing feature selection using scikit-learn feature_selection module.

# importing modules
from sklearn.feature_selection import SelectKBest
from sklearn.feature_selection import f_regression

# creating X - train and Y - test variables
X = main_df.iloc[:,0:-1] 
Y = main_df.iloc[:,-1]

# feature extraction
test = SelectKBest(score_func=f_regression, k=5)
features = test.fit_transform(X,Y)

# finding selected column names
feature_idx = test.get_support(indices=True)
feature_names = main_df.columns[feature_idx]

# creating selected features dataframe with corresponding column names
features = pd.DataFrame(features, columns=feature_names)

features.head()

I hope my code helps the community and if you like the effort, do upvote, it is a form of showing appreciation. Any and every feedback is appreciated.

python

pandas

scikit-learn

feature-extraction

feature-selection

0 Answers

Your Answer

Accepted video resources