I'm working in a problem in which I'm OneHot encoding a set of feaures from a dataframe, for instance:
from sklearn.preprocessing import OneHotEncoder
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y)
oh = OneHotEncoder(handle_unknown='ignore')
print(X)
a b c
1 one m y
2 two m n
36 three f n
113 one f n
31 two m other
....
oh.fit(X_train)
However, it could be that not all features are present in the test set. For this example, say I only have the two first columns. The encoder, in this case will raise an error:
oh.transform(X_test.loc[:,:'b'])
The number of features in X is different to the number of features of the fitted data. The fitted data had 3 features and the X has 2 features.
Is there some way around this? Ideally I'd like to have the missing columns, either ignored (not present in the output), or set to NaN
.