One hot Encoding with nominal categorical features in Python?
DATA MUNGING DATA CLEANING PYTHON MACHINE LEARNING RECIPES PANDAS CHEATSHEET     ALL TAGS

One hot Encoding with nominal categorical features in Python?

One hot Encoding with nominal categorical features in Python?

One hot Encoding with nominal categorical features in Python

3

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.

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"]

Relevant Projects

Loan Eligibility Prediction using Gradient Boosting Classifier
This data science in python project predicts if a loan should be given to an applicant or not. We predict if the customer is eligible for loan based on several factors like credit score and past history.

Predict Employee Computer Access Needs in Python
Data Science Project in Python- Given his or her job role, predict employee access needs using amazon employee database.

Demand prediction of driver availability using multistep time series analysis
In this supervised learning machine learning project, you will predict the availability of a driver in a specific area by using multi step time series analysis.

Topic modelling using Kmeans clustering to group customer reviews
In this Kmeans clustering machine learning project, you will perform topic modelling in order to group customer reviews based on recurring patterns.

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

Zillow’s Home Value Prediction (Zestimate)
Data Science Project in R -Build a machine learning algorithm to predict the future sale prices of homes.

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.

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.

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.

Data Science Project - Instacart Market Basket Analysis
Data Science Project - Build a recommendation engine which will predict the products to be purchased by an Instacart consumer again.