I have the following table.
id col1 col2 col3 col4 target
1 A B A 101 1
2 B B A 191 1
3 A B A 81 0
4 C B C 67 1
5 B C C 3 0
I want to target encode every column except col4.
Expected Output:
e1 e2 e3 target
0.5 0.75 0.667 1
0.5 0.75 0.667 1
0.5 0.75 0.667 0
1.0 0.75 0.5 1
0.5 0.00 0.5 0
EDIT:
For each column of col1, col2, col3 I want to get the target encodings.
For example, in col3, A appears 3 times and 2/3 times it has a target of 1. thus the encoding will be 0.667 for A. Similarly for C it will be 0.5 in col3.
I've tried something like this one for one column:
encodings = df.groupby('col1')['target'].mean().reset_index()
df = df.merge(encodings, how = 'left', on = 'col1')
df.drop('col1', axis = 1, inplace = TRUE)
target;)