[特征工程] encoding

参考:An Overview of Encoding Techniques | Kaggle

 

Method 1: Label encoding 

给每个类别以一个数字label,作为分类。将类别映射到自然数数值空间上

from sklearn.preprocessing import LabelEncoder
train=pd.DataFrame()
label=LabelEncoder()
for c in  X.columns:
    if(X[c].dtype=='object'):
        train[c]=label.fit_transform(X[c])
    else:
        train[c]=X[c]
        

 

Method 2 : One hot encoding 

即独热码,每一个category对应特征向量中的一位,对应位置是否为1判定是否为该类。

可以使用pd.get_dummies()或sklearn.preprocessing中OneHotEncoder

 

from sklearn.preprocessing import OneHotEncoder
one=OneHotEncoder(
one.fit(X)
train=one.transform(X)

 

Method 3 : Feature Hashing/Hashing Trick

一个“one hot encoding style” 的编码方式,将数据编入特定维数的散度矩阵中,降维中使用了hash方法。

 

from sklearn.feature_extraction import FeatureHasher
X_train_hash=X.copy()
for c in X.columns:
    X_train_hash[c]=X[c].astype('str')      
hashing=FeatureHasher(input_type='string')
train=hashing.transform(X_train_hash.values)

 

Method 4 :Encoding categories with dataset statistics

尝试为模型提供较低维的每个类别的表示,且其中类似的类别的表示相近。 最简单的方法是将每个类别替换为我们在数据集中看到它的次数,即用出现频率作为他们的embedding。

X_train_stat=X.copy()
for c in X_train_stat.columns:
    if(X_train_stat[c].dtype=='object'):
        X_train_stat[c]=X_train_stat[c].astype('category')
        counts=X_train_stat[c].value_counts()
        counts=counts.sort_index()
        counts=counts.fillna(0)
        counts += np.random.rand(len(counts))/1000
        X_train_stat[c].cat.categories=counts
    

 

对于循环出现的特征,例如日期,星期等,常用sin\cos将其转为二维空间中的数据。这是基于“循环”的性质,类似于对圆进行分割。

 

X_train_cyclic=X.copy()
columns=['day','month']
for col in columns:
    X_train_cyclic[col+'_sin']=np.sin((2*np.pi*X_train_cyclic[col])/max(X_train_cyclic[col]))
    X_train_cyclic[col+'_cos']=np.cos((2*np.pi*X_train_cyclic[col])/max(X_train_cyclic[col]))
X_train_cyclic=X_train_cyclic.drop(columns,axis=1)
one=OneHotEncoder()
one.fit(X_train_cyclic)
train=one.transform(X_train_cyclic)

 

Method 5 : Target encoding 

Target encoding 通过目标数据对类别变量进行编码,使用目标对应概率或平均概率替换该类别,即出现频次相近的被视为同一类(大城市,热门项等)。这个方法比较依赖训练集与测试集合的分布,要求他们数据分布一致。另外,这种方法可能会导致过拟合。

                     

X_target=df_train.copy()
X_target['day']=X_target['day'].astype('object')
X_target['month']=X_target['month'].astype('object')
for col in X_target.columns:
    if (X_target[col].dtype=='object'):
        target= dict ( X_target.groupby(col)['target'].agg('sum')/X_target.groupby(col)['target'].agg('count'))
        X_target[col]=X_target[col].replace(target).values

 

为了减轻过拟合可能带来的影响,可以使用K-Fold Validation ,每次对一份样本进行目标编码时,使用的都是其他K-1份数据之中的数据。

 

X['target']=y
cols=X.drop(['target','id'],axis=1).columns
%%time
X_fold=X.copy()
X_fold[['ord_0','day','month']]=X_fold[['ord_0','day','month']].astype('object')
X_fold[['bin_3','bin_4']]=X_fold[['bin_3','bin_4']].replace({'Y':1,'N':0,'T':1,"F":0})
kf = KFold(n_splits = 5, shuffle = False, random_state=2019)
for train_ind,val_ind in kf.split(X):
    for col in cols:
        if(X_fold[col].dtype=='object'):
            replaced=dict(X.iloc[train_ind][[col,'target']].groupby(col)['target'].mean())
            X_fold.loc[val_ind,col]=X_fold.iloc[val_ind][col].replace(replaced).values

 

 

此外,在对特征进行编码前也需要进行特征种类的区分。常分为:

  • 0-1数值:只有两种取值,可映射到0,1
  • 类别数值:多个类别,这也是最常见的数据。
  • 时序数据:时间戳等,隐含了顺序信息,可以反应趋势。