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
This python source code does the following: 1. Creates custom pandas dataframe 2. Creates a mapping list for encoding 3. Uses "replace" function to manually encode the categorical variables
In [1]:
## How to encode ordinal categorical features in Python 
def Kickstarter_Example_31():
    print()
    print(format('How to encode ordinal categorical features in Python', '*^82'))

    import warnings
    warnings.filterwarnings("ignore")

    # Load library
    import pandas as pd

    # Create features
    df = pd.DataFrame({'Score': ['Low', 'Low', 'Medium', 'Medium', 'High']})

    # View data frame
    print(); print(df)

    # Create Scale Map
    scale_mapper = {'Low':1, 'Medium':2, 'High':3}

    # Map feature values to scale
    df['Scale'] = df['Score'].replace(scale_mapper)

    # View data frame
    print(); print(df)

Kickstarter_Example_31()
***************How to encode ordinal categorical features in Python***************

    Score
0     Low
1     Low
2  Medium
3  Medium
4    High

    Score  Scale
0     Low      1
1     Low      1
2  Medium      2
3  Medium      2
4    High      3

Relevant Projects

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.

Data Science Project on Wine Quality Prediction in R
In this R data science project, we will explore wine dataset to assess red wine quality. The objective of this data science project is to explore which chemical properties will influence the quality of red wines.

Mercari Price Suggestion Challenge Data Science Project
Data Science Project in Python- Build a machine learning algorithm that automatically suggests the right product prices.

Choosing the right Time Series Forecasting Methods
There are different time series forecasting methods to forecast stock price, demand etc. In this machine learning project, you will learn to determine which forecasting method to be used when and how to apply with time series forecasting example.

Time Series Forecasting with LSTM Neural Network Python
Deep Learning Project- Learn to apply deep learning paradigm to forecast univariate time series data.

Predict Macro Economic Trends using Kaggle Financial Dataset
In this machine learning project, you will uncover the predictive value in an uncertain world by using various artificial intelligence, machine learning, advanced regression and feature transformation techniques.

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.

Resume parsing with Machine learning - NLP with Python OCR and Spacy
In this machine learning resume parser example we use the popular Spacy NLP python library for OCR and text classification.

Walmart Sales Forecasting Data Science Project
Data Science Project in R-Predict the sales for each department using historical markdown data from the Walmart dataset containing data of 45 Walmart stores.

Human Activity Recognition Using Smartphones Data Set
In this deep learning project, you will build a classification system where to precisely identify human fitness activities.