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

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.

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.

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.

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.

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

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.

Customer Market Basket Analysis using Apriori and Fpgrowth algorithms
In this NLP AI application, we build the core conversational engine for a chatbot. We use the popular NLTK text classification library to achieve this.

Identifying Product Bundles from Sales Data Using R Language
In this data science project in R, we are going to talk about subjective segmentation which is a clustering technique to find out product bundles in sales data.

Solving Multiple Classification use cases Using H2O
In this project, we are going to talk about H2O and functionality in terms of building Machine Learning models.