How to encode ordinal categorical features in Python?
DATA MUNGING DATA CLEANING PYTHON MACHINE LEARNING RECIPES PANDAS CHEATSHEET     ALL TAGS

How to encode ordinal categorical features in Python?

How to encode ordinal categorical features in Python?

This recipe helps you encode ordinal categorical features in Python

0

Recipe Objective

Have you ever tried to encode ordinal categorical features by making a simple function which is quiet easy to understand and change.

So this is the recipe on how we can encode ordinal categorical features in Python.

Step 1 - Import the library

import pandas as pd

We have imported pandas which will be needed for the dataset.

Step 2 - Setting up the Data

We have created a dataframe with one feature "score" with categorical variables "Low", "Medium" and "High". df = pd.DataFrame({"Score": ["Low", "Low", "Medium", "Medium", "High", "Low", "Medium","High", "Low"]}) print(df)

Step 3 - Encoding variable

We have created an object scale_mapper in which we have passed the encoding parameter i.e putting numerical values instead of categorical variable. We have made a feature scale in which there will be numerical encoded values. scale_mapper = {"Low":1, "Medium":2, "High":3} df["Scale"] = df["Score"].replace(scale_mapper) print(df) So the output comes as:

    Score
0     Low
1     Low
2  Medium
3  Medium
4    High
5     Low
6  Medium
7    High
8     Low

    Score  Scale
0     Low      1
1     Low      1
2  Medium      2
3  Medium      2
4    High      3
5     Low      1
6  Medium      2
7    High      3
8     Low      1

Relevant Projects

Forecast Inventory demand using historical sales data in R
In this machine learning project, you will develop a machine learning model to accurately forecast inventory demand based on historical sales data.

Machine Learning project for Retail Price Optimization
In this machine learning pricing project, we implement a retail price optimization algorithm using regression trees. This is one of the first steps to building a dynamic pricing model.

Data Science Project-TalkingData AdTracking Fraud Detection
Machine Learning Project in R-Detect fraudulent click traffic for mobile app ads using R data science programming language.

Predict Churn for a Telecom company using Logistic Regression
Machine Learning Project in R- Predict the customer churn of telecom sector and find out the key drivers that lead to churn. Learn how the logistic regression model using R can be used to identify the customer churn in telecom dataset.

Ecommerce product reviews - Pairwise ranking and sentiment analysis
This project analyzes a dataset containing ecommerce product reviews. The goal is to use machine learning models to perform sentiment analysis on product reviews and rank them based on relevance. Reviews play a key role in product recommendation systems.

Sequence Classification with LSTM RNN in Python with Keras
In this project, we are going to work on Sequence to Sequence Prediction using IMDB Movie Review Dataset​ using Keras in Python.

Data Science Project in Python on BigMart Sales Prediction
The goal of this data science project is to build a predictive model and find out the sales of each product at a given Big Mart store.

Credit Card Fraud Detection as a Classification Problem
In this data science project, we will predict the credit card fraud in the transactional dataset using some of the predictive models.

German Credit Dataset Analysis to Classify Loan Applications
In this data science project, you will work with German credit dataset using classification techniques like Decision Tree, Neural Networks etc to classify loan applications using R.

Deep Learning with Keras in R to Predict Customer Churn
In this deep learning project, we will predict customer churn using Artificial Neural Networks and learn how to model an ANN in R with the keras deep learning package.