Encoding categorical data is a crucial preprocessing step in machine learning pipelines. In simple terms, it involves converting categorical variables into numeric representations. This process is essential because many machine learning algorithms cannot directly handle categorical data.
While algorithms like Decision Trees and Random Forests can work with categorical data without encoding, others like Linear Regression, Logistic Regression, and K-Means Clustering require encoded inputs.
Here are several common encoding techniques:
1)N-1 Dummy Encoding:
Creates dummy variables for a categorical variable, generating k-1 binary columns for k categories.
Code Example:
pd.get_dummies(df, columns=[‘Column_Name’], drop_first=True)
2) One-Hot Encoding:
Similar to N-1 Dummy Encoding, but creates binary columns for all categories (k columns).
Code Example:
pd.get_dummies(df, columns=[‘Column_Name’])
3) Label Encoding:
Assigns numeric labels to categories based on alphabetical order or frequency.
Note: Let say the unique value in the columns are Car and Bike then the encoded column will consider Bike as 0 and Car as 1.
Code Example:
from sklearn.preprocessing import LabelEncoder
labelencoder = LabelEncoder()
df[‘Encoded_Column’] = labelencoder.fit_transform(df[‘Column_Name’])
4) Ordinal Encoding:
Assigns numeric labels to categories based on a specified order.
Code Example:
from sklearn.preprocessing import OrdinalEncoder
ordinalencoder = OrdinalEncoder(categories=[[‘Water’, ‘Mineral’, ‘Gas’]])
df[‘Encoded_Column’]=ordinalencoder.fit_transform(df[‘Column_Name’].values.reshape(-1, 1))
5) Frequency Encoding:
Replaces each label with the percentage of observations within the category.
6) Target Encoding:
Also known as mean encoding, replaces each category with its corresponding mean in classification tasks.
Understanding these encoding techniques empowers you to preprocess categorical data effectively for machine learning models. Each method has its advantages and use cases, so choose the one that best fits your dataset and model requirements.
By employing appropriate encoding techniques, you can enhance the performance of your machine learning models and extract meaningful insights from your data.
Add a Comment