Machine Learning : One-hot encoding for inferencing

 One-hot encoding is a technique used in machine learning and data preprocessing to convert categorical data into a binary format.


This allows algorithms to handle categorical data more effectively.  In One-hot encoding, each category is represented by a binary vector where all elements are zero except for the position corresponding to the category, which is set to one. 

The ipynb file for this activity
https://github.com/niranjanmeegammana/ML-course/blob/main/projects/MLC_Onehot_Encoding_for_Inferencing.ipynb


import pandas as pd


# Data with a categorical column

data = {

    'Fruit': ['Apple', 'Orange', 'Banana', 'Apple', 'Banana', 'Orange']

}


# Convert the data into a DataFrame

df = pd.DataFrame(data)

print(df)


# Perform one-hot encoding using pandas' get_dummies() function

onehot_encoded = pd.get_dummies(df['Fruit'])


# Concatenate the one-hot encoded DataFrame with the original DataFrame

df_encoded = pd.concat([df, onehot_encoded], axis=1)


print(df_encoded)


Example with two category columns

import pandas as pd


# Sample data with two categorical columns

data = {

    'Fruit': ['Apple', 'Orange', 'Banana', 'Apple', 'Banana', 'Orange'],

    'Color': ['Red', 'Orange', 'Yellow', 'Red', 'Yellow', 'Orange']

}


# Convert the data into a DataFrame

df = pd.DataFrame(data)


print (df)


# Perform one-hot encoding on both categorical columns

onehot_encoded = pd.get_dummies(df, columns=['Fruit', 'Color'])


print(onehot_encoded)


Inferencing 

When we create a model, we use multiple records with one hot encoding. However, when Inferencing we use one record which may not have all data in category columns for one hot encoding. Therefore, the inferencing features will not match features seen by the model in training. 

To solve this problem we can create an empty record that represents all features used in training and update values from the new record we will be testing.


Create a record with fruit: Apple and    Color:  Red.

Convert the record to a data frame record. 

# Create the record as a dictionary

record = {

    'Fruit': ['Apple'],

    'Color': ['Red']

}


# Convert the record into a DataFrame

df_record = pd.DataFrame(record)

df_record 


# Perform one-hot encoding on the DataFrame

onehot_encoded_record = pd.get_dummies(df_record)

print(onehot_encoded_record)


Create an empty df record from onehot_encoded, and fill data with zero.

# Create an empty DataFrame with the same columns as onehot_encoded

empty_df_record = pd.DataFrame(columns=onehot_encoded.columns)

# Fill the empty DataFrame with zeros

empty_df_record = empty_df_record.append(pd.Series([0] * len(empty_df_record.columns), index=empty_df_record.columns), ignore_index=True)

print(empty_df_record)

Update empty_df_record with onehot_encoded_record values and save it as new_onehot_record

#Create a copy of empty_df_record 

import copy


# Create a deep copy

new_onehot_record = copy.deepcopy(empty_df_record)

# Update new_onehot_record with values from onehot_encoded_record


new_onehot_record.update(onehot_encoded_record)

new_onehot_record

This record can be used for inferencing with the model.

print(new_onehot_record)

Niranjan Meegammana 

Cyber security and  ML researcher

Sri Lanka Institute of Information Technology 

Shilpa Sayura Foundation 



Comments

Popular posts from this blog

AI-Typical Phrases to Avoid in Academic Writing

Tips for Humanizing AI Text

Overfilling and Underfitting in Machine Learning