🌐
Practical Business Python
pbpython.com › categorical-encoding.html
Guide to Encoding Categorical Values in Python - Practical Business Python
Another approach to encoding categorical values is to use a technique called label encoding. Label encoding is simply converting each value in a column to a number. For example, the body_style column contains 5 different values.
🌐
Analytics Vidhya
analyticsvidhya.com › home › what are categorical data encoding methods | binary encoding
What are Categorical Data Encoding Methods | Binary Encoding
May 1, 2025 - If you want to change the Base of encoding scheme you may use Base N encoder. In the case when categories are more and binary encoding is not able to handle the dimensionality then we can use a larger base such as 4 or 8. Clear your Understanding how you can perform Lable Encoding in Python
Discussions

python - How to encode a categorical variable in sklearn? - Stack Overflow
I'm trying to use the car evaluation dataset from the UCI repository and I wonder whether there is a convenient way to binarize categorical variables in sklearn. One approach would be to use the More on stackoverflow.com
🌐 stackoverflow.com
"Feature Importance" for categorical variables
Step 1: CatBoost on raw categories Step 2: SHAP Tree Explainer More on reddit.com
🌐 r/AskStatistics
5
1
August 19, 2023
How to handle missing categorical values when the existing ones are quite meaningful
Use xgboost, then you just don't care about missing valies, mark them as nulls and let xgboost handle them. What xgboost will do is grow the trees ignoring null values and then send them to the right or left branch depending on what is better. The difference between this and assigning a no-val value to the category is that the same instance can go left in one tree and right on another one, so nulls are treated like wildcards the only rule is that they all go together. For this to work you need to encode the categorical feature, use the catboost encoder with xgboost works great. More on reddit.com
🌐 r/learnmachinelearning
4
3
September 1, 2022
Imputation for categorical values (Specifically via KNN imputation)
I often treat missing as another categorical value. Missings are often missing for a reason - missingness contains information and maybe you don’t want to throw that away. More on reddit.com
🌐 r/datascience
9
8
June 18, 2022
🌐
MachineLearningMastery
machinelearningmastery.com › home › blog › 3 ways to encode categorical variables for deep learning
3 Ways to Encode Categorical Variables for Deep Learning - MachineLearningMastery.com
August 26, 2020 - As far as I know, the summary of encoding could be: when applying “OrdinalEncoder” we convert categorical labels to integers and, the 9 original fe · Welcome! I'm Jason Brownlee PhD and I help developers get results with machine learning. Read more · Your First Deep Learning Project in Python with Keras Step-by-Step
🌐
Scikit-learn course
inria.github.io › scikit-learn-mooc › python_scripts › 03_categorical_pipeline.html
Encoding of categorical variables — Scikit-learn course
OneHotEncoder is an alternative encoder that prevents the downstream models to make a false assumption about the ordering of categories. For a given feature, it creates as many new columns as there are possible categories.
🌐
DataCamp
datacamp.com › tutorial › categorical-data
Handling Machine Learning Categorical Data with Python Tutorial | DataCamp
February 23, 2023 - In this tutorial, we have explored various techniques for analyzing and encoding categorical variables in Python, including one-hot encoding and label encoding, which are two commonly used techniques.
🌐
GeeksforGeeks
geeksforgeeks.org › machine learning › encoding-categorical-data-in-sklearn
Encoding Categorical Data in Sklearn - GeeksforGeeks
September 17, 2025 - Here we will use Label encoding converts each category into a unique integer, making it suitable for ordinal data or when models need numeric input. fit_transform: Learns and applies the mapping. .classes_: Shows the mapping order. Python ·
🌐
Medium
soumenatta.medium.com › categorical-data-encoding-techniques-in-python-a-complete-guide-a913aae19a22
Categorical Data Encoding Techniques in Python: A Complete Guide | by Dr. Soumen Atta, Ph.D. | Medium
May 4, 2023 - In order to use categorical data in machine learning models, it needs to be encoded as numerical values. In this tutorial, we will explore various techniques for categorical data encoding in Python.
🌐
CodeSignal
codesignal.com › learn › courses › cleaning-and-transforming-data-with-pandas › lessons › encoding-categorical-variables-using-python
Encoding Categorical Variables Using Python
Encoding using map: We use the map method to replace each label in the Gender column based on our specified dictionary: {'Male': 1, 'Female': 0}. This dictionary tells Python to encode Male as 1 and Female as 0. Adding a new column: The new column Gender_Encoded is created and added to the DataFrame. This column contains the numerical representation of the Gender column, which can now be used for further analysis or as input into a machine learning algorithm. In this lesson, we explored the importance and different methods of encoding categorical variables, with a specific focus on using dictionary mapping in Python.
Find elsewhere
🌐
PyPI
pypi.org › project › category-encoders
category-encoders · PyPI
A package for encoding categorical variables for machine learning
🌐
Medium
garg-shelvi.medium.com › category-encoders-c2a9bb192f0a
How to Encode Categorical Data | by Shelvi Garg | Medium
July 12, 2022 - category_encoders is an amazing python library that provides 15 different encoding schemes.
🌐
Scikit-learn
contrib.scikit-learn.org › category_encoders
Category Encoders — Category Encoders 2.11.1 documentation
import category_encoders as ce encoder = ce.BackwardDifferenceEncoder(cols=[...]) encoder = ce.BaseNEncoder(cols=[...]) encoder = ce.BinaryEncoder(cols=[...]) encoder = ce.CatBoostEncoder(cols=[...]) encoder = ce.CountEncoder(cols=[...]) encoder = ce.CountTargetEncoder(cols=[...]) encoder = ce.GLMMEncoder(cols=[...]) encoder = ce.GrayEncoder(cols=[...]) encoder = ce.HashingEncoder(cols=[...]) encoder = ce.HelmertEncoder(cols=[...]) encoder = ce.JamesSteinEncoder(cols=[...]) encoder = ce.LeaveOneOutEncoder(cols=[...]) encoder = ce.MEstimateEncoder(cols=[...]) encoder = ce.MultiHotEncoder(cols=[
🌐
GitHub
github.com › scikit-learn-contrib › category_encoders
GitHub - scikit-learn-contrib/category_encoders: A library of sklearn compatible categorical variable encoders · GitHub
All of the encoders are fully compatible sklearn transformers, so they can be used in pipelines or in your existing scripts. Supported input formats include numpy arrays and pandas dataframes. If the cols parameter isn't passed, all columns with object or pandas categorical data type will be encoded.
Author: scikit-learn-contrib
🌐
Hands-on Mentor
handsonmentor.com › post › handling-and-encoding-categorical-data-in-python
Handling and Encoding Categorical Data in Python
August 2, 2024 - This method involves converting each value in a category to a unique integer. Consider a column 'Manufacturer' in our used cars dataset. Each unique manufacturer can be represented by a unique integer through label encoding.
🌐
Towards Data Science
towardsdatascience.com › home › latest › categorical feature encoding
Categorical Feature Encoding | Towards Data Science
March 5, 2025 - There are a couple ways to accomplish this task in python, and I’ll focus on what I believe to be the two easiest methods. ... The first part is instantiating a one-hot encoder from scikit-learn and more information on this function can be found at the documentation
🌐
Medium
medium.com › @jaberi.mohamedhabib › encoding-categorical-variables-methods-and-techniques-in-pandas-scikit-learn-and-using-dummy-216ae2d5128d
Encoding Categorical Variables: Methods and Techniques in Pandas, Scikit-learn, and Using Dummy Function | by JABERI Mohamed Habib | Medium
September 27, 2024 - Target Encoding (also known as mean encoding) replaces each category with the mean value of the target variable. This is useful in cases where there’s a strong relationship between a categorical feature and the target variable.
🌐
Towards Data Science
towardsdatascience.com › home › data science › encoding categorical data, explained: a visual guide with code example for beginners
Encoding Categorical Data, Explained: A Visual Guide with Code Example for Beginners | Towards Data Science
September 2, 2024 - So, well, encoding is about translating your categorical data into a language that machines can understand, while preserving as much meaning as possible. It's not about finding a perfect encoding, but about choosing the method that best suits your specific needs and constraints. Approach it thoughtfully, and you'll set a strong foundation for your machine learning works. python ·
🌐
Towards Data Science
towardsdatascience.com › home › artificial intelligence › all about categorical variable encoding
All about Categorical Variable Encoding | Towards Data Science
July 16, 2019 - 1) One Hot Encoding 2) Label Encoding 3) Ordinal Encoding 4) Helmert Encoding 5) Binary Encoding 6) Frequency Encoding 7) Mean Encoding 8) Weight of Evidence Encoding 9) Probability Ratio Encoding 10) Hashing Encoding 11) Backward Difference Encoding 12) Leave One Out Encoding 13) James-Stein Encoding 14) M-estimator Encoding (updated) ... For explanation, I will use this data frame, which has two independent variables or features(Temperature and Color) and one label (Target). It also has Rec-No, which is a sequence number of the record. There is a total of 10 records in this data frame. Python code would look as below. ... We will use Pandas and Scikit-learn and category_encoders (Scikit-learn contribution library) to show different encoding methods in Python.
Top answer
1 of 3
31

if your data is a pandas DataFrame, then you can simply call get_dummies. Assume that your data frame is df, and you want to have one binary variable per level of variable 'key'. You can simply call:

pd.get_dummies(df['key'])

and then delete one of the dummy variables, to avoid the multi-colinearity problem. I hope this helps ...

2 of 3
16

The basic method is

import numpy as np
import pandas as pd, os
from sklearn.feature_extraction import DictVectorizer

def one_hot_dataframe(data, cols, replace=False):
    vec = DictVectorizer()
    mkdict = lambda row: dict((col, row[col]) for col in cols)
    vecData = pd.DataFrame(vec.fit_transform(data[cols].apply(mkdict, axis=1)).toarray())
    vecData.columns = vec.get_feature_names()
    vecData.index = data.index
    if replace is True:
        data = data.drop(cols, axis=1)
        data = data.join(vecData)
    return (data, vecData, vec)

data = {'state': ['Ohio', 'Ohio', 'Ohio', 'Nevada', 'Nevada'],
        'year': [2000, 2001, 2002, 2001, 2002],
        'pop': [1.5, 1.7, 3.6, 2.4, 2.9]}

df = pd.DataFrame(data)

df2, _, _ = one_hot_dataframe(df, ['state'], replace=True)
print df2

Here is how to do in sparse format

import numpy as np
import pandas as pd, os
import scipy.sparse as sps
import itertools

def one_hot_column(df, cols, vocabs):
    mats = []; df2 = df.drop(cols,axis=1)
    mats.append(sps.lil_matrix(np.array(df2)))
    for i,col in enumerate(cols):
        mat = sps.lil_matrix((len(df), len(vocabs[i])))
        for j,val in enumerate(np.array(df[col])):
            mat[j,vocabs[i][val]] = 1.
        mats.append(mat)

    res = sps.hstack(mats)   
    return res

data = {'state': ['Ohio', 'Ohio', 'Ohio', 'Nevada', 'Nevada'],
        'year': ['2000', '2001', '2002', '2001', '2002'],
        'pop': [1.5, 1.7, 3.6, 2.4, 2.9]}

df = pd.DataFrame(data)
print df

vocabs = []
vals = ['Ohio','Nevada']
vocabs.append(dict(itertools.izip(vals,range(len(vals)))))
vals = ['2000','2001','2002']
vocabs.append(dict(itertools.izip(vals,range(len(vals)))))

print vocabs

print one_hot_column(df, ['state','year'], vocabs).todense()
🌐
Trainindata
feature-engine.trainindata.com › en › 1.8.x › user_guide › encoding › index.html
Categorical Encoding — 1.8.3
In addition to the categorical encoding methods supported by Feature-engine, there are other methods like feature hashing or binary encoding. These methods are supported by the Python library category encoders. For the time being, we decided not to support these transformations because they return features that are not easy to interpret.
🌐
Kaggle
kaggle.com › code › arashnic › an-overview-of-categorical-encoding-methods
An Overview of Categorical Encoding Methods | Kaggle
December 30, 2021 - Explore and run AI code with Kaggle Notebooks | Using data from multiple data sources