程序笔记   发布时间:2022-07-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了[特征工程] encoding大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

:An Overview of Encoding Techniques | Kaggle

 

@H_652_5@method 1: Label encoding 

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

@H_489_10@
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]
        

 

@H_652_5@method 2 : One hot encoding 

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

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

 

@H_489_10@
from sklearn.preprocessing import OneHotEncoder
one=OneHotEncoder(
one.fit(X)
Train=one.transform(X)

 

@H_652_5@method 3 : Feature Hashing/Hashing Trick

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

 

@H_489_10@
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)

 

@H_652_5@method 4 :Encoding categories with dataset statistics

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

@H_489_10@
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
    

 

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

 

@H_489_10@
X_Train_cyclic=X.copy()
columns=['day','@H_395_30@month']
for col in columns:
    X_Train_cyclic[col+'_sin']=np.sin((2*np.pi*X_Train_cyclic[col])/@H_463_16@max(X_Train_cyclic[col]))
    X_Train_cyclic[col+'_cos']=np.cos((2*np.pi*X_Train_cyclic[col])/@H_463_16@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)

 

@H_652_5@method 5 : Target encoding 

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

                     

[特征工程] encoding

@H_489_10@
X_target=df_Train.copy()
X_target['day']=X_target['day'].astype('object')
X_target['@H_395_30@month']=X_target['@H_395_30@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份数据之中的数据。

 

@H_489_10@
X['target']=y
cols=X.drop(['target','id'],axis=1).columns
%%time
X_fold=X.copy()
X_fold[['ord_0','day','@H_395_30@month']]=X_fold[['ord_0','day','@H_395_30@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

 

 

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

 

大佬总结

以上是大佬教程为你收集整理的[特征工程] encoding全部内容,希望文章能够帮你解决[特征工程] encoding所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:aggcolumndatasetloadphpshuffle程序员
猜你在找的程序笔记相关文章
其他相关热搜词更多
phpJavaPython程序员load如何string使用参数jquery开发安装listlinuxiosandroid工具javascriptcap