4

我有一个数据集,我最近通过 one-hot 编码对其进行了转换,并使用它训练了一个套索逻辑回归。我正在尝试获取非零系数的列表。我可以通过 sklearn 获得系数列表,但我不确定如何在一次热编码后将它们映射回数据。

数据集的一小部分摘录(下面是一个热编码)

  {'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}}

还有我的 one-hot 编码代码:

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
4

1 回答 1

4

假设您将数据集的一小部分摘录存储在一个名为的变量中temp

temp = pd.DataFrame(temp)
categorical = (temp.dtypes.values != np.dtype('float64'))
categorical = temp.columns[categorical]

def one_hot(temp, categorical):
    # temp is the data frame from which the "categorical" columns need to be One hot encoded
    from sklearn.preprocessing import OneHotEncoder
    enc_model = OneHotEncoder(sparse=False)
    X = enc_model.fit_transform(temp[categorical])
    uniq_vals = temp[categorical].apply(lambda x: x.value_counts()).unstack()
    uniq_vals = uniq_vals[~uniq_vals.isnull()]
    enc_cols = list(uniq_vals.index.map('{0[0]}_{0[1]}'.format)) # https://stackoverflow.com/questions/41987743/merge-two-multiindex-levels-into-one-in-pandas
    enc_df = pd.DataFrame(X, columns=enc_cols, index=temp.index, dtype='bool')

    return(enc_df)
于 2017-11-10T20:45:14.303 回答