One hot Encoding with nominal categorical features in Python?

One hot Encoding with nominal categorical features in Python

Recipe Objective

We can not pass categorical variables in models so how to handle categorical variables. We can use one hor encoding to do this.

So this is the recipe on how we can do One hot Encode with nominal categorical features in Python.

Build a Chatbot in Python from Scratch!

Step 1 - Import the library

import numpy as np from sklearn.preprocessing import LabelBinarizer

We have only imported numpy and LabelBinarizer which is needed.

Step 2 - Creating an array

We have created an array on which we will perform the operation. x = np.array([["Texas"], ["California"], ["Texas"], ["Delaware"], ["Texas"]])

Step 3 - One hot encoding

We have created an object LabelBinarizer to change the catergorical variables. We have use fit_transform to change the variables and printed the class. one_hot = LabelBinarizer() print(one_hot.fit_transform(x)) print(one_hot.classes_) So the output comes as

[[0 0 1]
 [1 0 0]
 [0 0 1]
 [0 1 0]
 [0 0 1]]

["California" "Delaware" "Texas"]

Download Materials

What Users are saying..

profile image

Ed Godalle

Director Data Analytics at EY / EY Tech
linkedin profile url

I am the Director of Data Analytics with over 10+ years of IT experience. I have a background in SQL, Python, and Big Data working with Accenture, IBM, and Infosys. I am looking to enhance my skills... Read More

Relevant Projects

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.

Learn to Build Generative Models Using PyTorch Autoencoders
In this deep learning project, you will learn how to build a Generative Model using Autoencoders in PyTorch

Customer Churn Prediction Analysis using Ensemble Techniques
In this machine learning churn project, we implement a churn prediction model in python using ensemble techniques.

Learn to Build a Polynomial Regression Model from Scratch
In this Machine Learning Regression project, you will learn to build a polynomial regression model to predict points scored by the sports team.

BERT Text Classification using DistilBERT and ALBERT Models
This Project Explains how to perform Text Classification using ALBERT and DistilBERT

Recommender System Machine Learning Project for Beginners-4
Collaborative Filtering Recommender System Project - Comparison of different model based and memory based methods to build recommendation system using collaborative filtering.

Build a Face Recognition System in Python using FaceNet
In this deep learning project, you will build your own face recognition system in Python using OpenCV and FaceNet by extracting features from an image of a person's face.

NLP Project on LDA Topic Modelling Python using RACE Dataset
Use the RACE dataset to extract a dominant topic from each document and perform LDA topic modeling in python.

Build a Collaborative Filtering Recommender System in Python
Use the Amazon Reviews/Ratings dataset of 2 Million records to build a recommender system using memory-based collaborative filtering in Python.

Build Customer Propensity to Purchase Model in Python
In this machine learning project, you will learn to build a machine learning model to estimate customer propensity to purchase.