简体   繁体   中英

deploy machine learning model with one hot encoded features

I have trained an xgboost classifier with categorical features that I have previously one hot encoded. For example, I have a categorical feature 'Year' which takes values between 2014 and 2018. When OHEd I get 5 binary features: Year_2014, Year_2015, Year_2016, Year_2017, Year_2018. What happens if I make a prediction on a sample that has Year=2019 since the feature Year_2019 does not exist?

More generally, what is a robust way to transform data in order to make predictions on a new samples?

Binary features are evaluated like this:

if(year != ${year value}){
  // Enter "left" branch
} else {
  // Enter "right" branch
}

An unseen category level gets sent to the "left" branch.

#While traning say year has below values
df = pd.DataFrame([2014,2015,2016,2017,2018], columns = ['year']) 
data=pd.get_dummies(df,columns=['year']) 
data.head()
# while predicting lets say input for year is 2018
known_categories = ['2014','2015','2016','2017','2018']    
year_type = pd.Series(['2018']) 
year_type = pd.Categorical(year_type, categories = known_categories)
pd.get_dummies(year_type)
# column name does not matter only the values matters which will be input to the model

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM