如何对不包括 'nan' 值的特定特征执行目标引导编码?

How to perform target guided encoding on a particular feature excluding 'nan' values?

from category_encoders import TargetEncoder
encoder=TargetEncoder()

for i in df['gender']:
df['gender']=np.where(df[i]!='nan',encoder.fit_transform(data['gender'],data['target']),'nan')

{KeyError: 'Male'}

经过大量Google搜索,我发现已经有一个内置方法。试试这个:

from category_encoders import TargetEncoder

encoder = TargetEncoder(handle_missing = 'return_nan')
df['gender'] = encoder.fit_transform(df['gender'], df['target'])