Getting feature names after one-hot encoding
<p>I have a dataset that I've recently transformed through one-hot encoding and used it trained a lasso logistic regression on it. I'm trying to get a list of the non-zero coefficients. I can get a list of the coefficients through sklearn but I'm not sure how to map them back to the data after one hot encoding.</p>

<p>A small excerpt of the dataset (pre one hot encoding is below)</p>

<pre><code>  {'acc_now_delinq': {29601: 0.0,
  143234: 0.0,
  157345: 0.0,
  158754: 0.0,
  229042: 0.0},
 'application_type': {29601: 0, 143234: 0, 157345: 0, 158754: 0, 229042: 0},
 'collections_12_mths_ex_med': {29601: 0.0,
  143234: 0.0,
  157345: 0.0,
  158754: 0.0,
  229042: 0.0},
 'credit_age': {29601: 118.0,
  143234: 157.0,
  157345: 213.0,
  158754: 269.0,
  229042: 240.0},
 'delinq_2yrs': {29601: 0.0,
  143234: 0.0,
  157345: 0.0,
  158754: 0.0,
  229042: 0.0},
 'dti': {29601: 2.0600000000000001,
  143234: 23.710000000000001,
  157345: 18.960000000000001,
  158754: 18.690000000000001,
  229042: 22.530000000000001},
 'emp_length_num': {29601: 8.0,
  143234: 2.0,
  157345: 1.0,
  158754: 7.0,
  229042: 1.0},
 'home_ownership': {29601: 4, 143234: 5, 157345: 5, 158754: 1, 229042: 1},
 'inq_last_6mths': {29601: 2.0,
  143234: 0.0,
  157345: 0.0,
  158754: 0.0,
  229042: 0.0},
 'loan_amnt': {29601: 214.0,
  143234: 211.0,
  157345: 571.0,
  158754: 937.0,
  229042: 466.0},
 'loan_status': {29601: 0, 143234: 1, 157345: 0, 158754: 1, 229042: 1},
 'log_annual_inc': {29601: 11.225243392499999,
  143234: 10.8022251252,
  157345: 11.0020998412,
  158754: 11.6952470218,
  229042: 11.225243392499999},
 'open_acc': {29601: 5.0,
  143234: 21.0,
  157345: 11.0,
  158754: 9.0,
  229042: 14.0},
 'pub_rec': {29601: 0.0, 143234: 0.0, 157345: 0.0, 158754: 0.0, 229042: 0.0},
 'purpose': {29601: 4, 143234: 2, 157345: 2, 158754: 2, 229042: 2},
 'revol_bal': {29601: 2266.0,
  143234: 12254.0,
  157345: 20657.0,
  158754: 11367.0,
  229042: 39404.0},
 'revol_inc_ratio': {29601: 0.030213333333299997,
  143234: 0.24941990637100001,
  157345: 0.34428333333300004,
  158754: 0.094725000000000004,
  229042: 0.52538666666699996},
 'revol_util': {29601: 44.0,
  143234: 89.400000000000006,
  157345: 76.900000000000006,
  158754: 81.200000000000003,
  229042: 95.5},
 'tot_coll_amt': {29601: 0.0,
  143234: 0.0,
  157345: 0.0,
  158754: 0.0,
  229042: 0.0},
 'tot_cur_bal': {29601: 2266.0,
  143234: 115947.0,
  157345: 80598.0,
  158754: 347695.0,
  229042: 355741.40000000002},
 'total_acc': {29601: 5.0,
  143234: 41.0,
  157345: 35.0,
  158754: 17.0,
  229042: 30.0},
 'total_rev_hi_lim': {29601: 5100.0,
  143234: 13700.0,
  157345: 26900.0,
  158754: 14000.0,
  229042: 80780.0},
 'verification_status': {29601: 0, 143234: 2, 157345: 1, 158754: 2, 229042: 1}}
</code></pre>

<p>And my one-hot encoding code:</p>

<pre><code>def one_hot(df):
# Categorical columns for use in one-hot encoder
categorical = (df.dtypes.values != np.dtype('float64'))
print categorical

# Get numpy array from data
x = df.values[:, :-1]
y = df.values[:, -1]

# Apply one hot endcoing
encoder = preprocessing.OneHotEncoder(categorical_features=categorical[:-1], sparse=False)  # Last value in mask is y
x = encoder.fit_transform(x)

return x, y
</code></pre>