I took the following code from GitHub in order to encode categorical variables using target mean encoding with smoothing. It worked perfectly for the training data but faile