Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is there away to output selected columns names from SelectFromModel method?

i performed feature selection using ExtraTreesClassifier and SelectFromModel in data set that loaded as DataFrame, however i want to save these selected feature as DataFrame to csv file while maintaining columns name as well. note that output is numpy array return important features whole columns not columns header

import pandas as pd
from sklearn.ensemble import ExtraTreesClassifier
from sklearn.feature_selection import SelectFromModel
import numpy as np


df = pd.read_csv('los_10_one_encoder.csv')
y = df['LOS'] # target 
X= df.drop('LOS',axis=1) # drop LOS column 
clf = ExtraTreesClassifier()
clf = clf.fit(X, y)
print clf.feature_importances_

model = SelectFromModel(clf, prefit=True)
X_new = model.transform(X)
like image 784
kero Avatar asked Dec 11 '16 16:12

kero


1 Answers

model = SelectFromModel(clf, prefit=True)
feature_idx = model.get_support()
feature_name = df.columns[feature_idx]
like image 193
Liang Avatar answered Oct 31 '22 14:10

Liang