The normal method to construct a machine studying mannequin is to first prepare the mannequin on a “coaching dataset” (normally a dataset of historic values) after which generate predictions on a brand new dataset, the “inference dataset”. That is it. Machine studying algorithms usually fail when the columns within the coaching and inference datasets do not match. That is primarily as a consequence of lacking or new issue ranges within the inference dataset.
First drawback: lacking parts
The next instance assumes that you’ve skilled a machine studying mannequin utilizing the dataset above. We one-hot encoded the dataset into dummy variables. The totally reworked coaching information seems to be like this:
Now let’s introduce the inference dataset. That is what we use to make predictions. Suppose you’re given the next:
# Creating the inference_data DataFrame in Python
inference_data = pd.DataFrame({
'numerical_1': [11, 12, 13, 14, 15, 16, 17, 18],
'color_1_': ['black', 'blue', 'black', 'green',
'green', 'black', 'black', 'blue'],
'color_2_': ['orange', 'orange', 'black', 'orange',
'black', 'orange', 'orange', 'orange']
})
Use a easy one-hot encoding technique just like the one used above (pd.get_dummies)
# Changing categorical columns in inference_data to
# Dummy variables with integers
inference_data_dummies = pd.get_dummies(inference_data,
columns=['color_1_', 'color_2_']).astype(int)
This transforms the inference dataset in the identical method, ensuing within the following dataset:
Are you noticing the issue? The primary drawback is that your inference dataset is lacking columns.
missing_colmns =['color_1__red', 'color_2__pink',
'color_2__blue', 'color_2__purple']
If you happen to run this on a mannequin skilled on a “coaching dataset” it would normally crash.
Second drawback: new components
One other drawback that may come up with one-hot encoding is when the inference dataset incorporates new, unknown parts. Let’s take into account once more the identical dataset as above. Upon nearer inspection, you will see {that a} new column has been added to the inference dataset. color_2__orange.
That is the alternative drawback from earlier than, the inference dataset incorporates new columns that weren’t current within the coaching dataset. That is really frequent and may happen if one of many issue variables is modified. For instance, if the colours above signify automobile colours and automobile producers out of the blue begin making orange vehicles, this information will not be accessible within the coaching information, however it would nonetheless seem within the inference information. There’s a chance. On this case, you want a dependable method to cope with the issue.
So, one would possibly argue, why not listing all of the columns within the reworked coaching dataset as required columns within the inference dataset? The issue right here is that we frequently have no idea upfront which issue ranges are included within the coaching information.
For instance, if new ranges are launched recurrently, it could possibly change into tough to take care of. Along with that, there’s a strategy of matching the inference dataset to the coaching information, so you want to examine all of the precise reworked column names that went into the coaching algorithm and match them to the reworked inference dataset. If any columns are lacking, you should insert new columns with values of 0. Additionally, you probably have further columns, color_2__orange The above columns ought to be eliminated. This can be a moderately cumbersome method to resolve the issue, however fortunately there are higher choices accessible.
The answer to this drawback is pretty easy, however many packages and libraries that try to streamline the method of making predictive fashions fail to implement it correctly. The hot button is to first match a perform or class to the coaching information after which use the identical occasion of that perform or class to rework each the coaching and inference datasets. Under we’ll see how that is performed utilizing each Python and R.
In Python
Python might be probably the greatest programming languages to make use of for machine studying. That is primarily as a consequence of its in depth developer community and mature package deal library, in addition to ease of use that facilitates speedy growth.
For points associated to one-hot encoding mentioned above, the extensively accessible and examined scikit-learn library, particularly sklearn.preprocessing.OneHotEncoder class. Now let’s examine how we will use this on our coaching and inference datasets to create a sturdy one-hot encoding.
from sklearn.preprocessing import OneHotEncoder# Initialize the encoder
enc = OneHotEncoder(handle_unknown='ignore')
# Outline columns to rework
trans_columns = ['color_1_', 'color_2_']
# Match and rework the information
enc_data = enc.fit_transform(training_data[trans_columns])
# Get characteristic names
feature_names = enc.get_feature_names_out(trans_columns)
# Convert to DataFrame
enc_df = pd.DataFrame(enc_data.toarray(),
columns=feature_names)
# Concatenate with the numerical information
final_df = pd.concat([training_data[['numerical_1']],
enc_df], axis=1)
This provides you with the ultimate outcome DataFrameThe transformed worth seems to be like this:
If we break down the above code, we will see that step one is to initialize an occasion of the encoder class.use choice handle_unknown='ignore' This avoids the issue of unknown values in columns when utilizing the encoder to rework inference datasets.
Then mix the match and rework actions into one step. fit_transform Methodology. Lastly, create a brand new information body from the encoded information and concatenate it with the remainder of the unique dataset.

