How to do Category encoding and string lookup using keras?

This recipe helps you do Category encoding and string lookup using keras

Recipe Objective

Category encoding and string lookup using keras.

one-hot encoding is the representation of categorical variables as binary vectors.

The keras provides a to_categorical() method. It can encode the strings data into numerical or integer data.

Step 1- Importing Libraries.

from keras.preprocessing.text import one_hot from keras.preprocessing.text import text_to_word_sequence from keras.preprocessing.text import Tokenizer

Step 2- Encoding the text.

Define the text that you want to encode.

#Define text text = 'a book or other written or printed work, regarded in terms of its content rather than its physical form' #Size of the vocabulary words = set(text_to_word_sequence(text)) vocab = len(words)

Step 3- One hot encode the text

# integer encode the document result = one_hot(text, round(vocab_size)) print(result)

[6, 2, 7, 3, 2, 7, 7, 1, 5, 2, 7, 4, 1, 2, 7, 4, 1, 4, 5]

What Users are saying..

profile image

Anand Kumpatla

Sr Data Scientist @ Doubleslash Software Solutions Pvt Ltd
linkedin profile url

ProjectPro is a unique platform and helps many people in the industry to solve real-life problems with a step-by-step walkthrough of projects. A platform with some fantastic resources to gain... Read More

Relevant Projects

Build CNN Image Classification Models for Real Time Prediction
Image Classification Project to build a CNN model in Python that can classify images into social security cards, driving licenses, and other key identity information.

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.

Learn to Build a Neural network from Scratch using NumPy
In this deep learning project, you will learn to build a neural network from scratch using NumPy

Deep Learning Project for Beginners with Source Code Part 1
Learn to implement deep neural networks in Python .

Build a Autoregressive and Moving Average Time Series Model
In this time series project, you will learn to build Autoregressive and Moving Average Time Series Models to forecast future readings, optimize performance, and harness the power of predictive analytics for sensor data.

Learn to Build an End-to-End Machine Learning Pipeline - Part 1
In this Machine Learning Project, you will learn how to build an end-to-end machine learning pipeline for predicting truck delays, addressing a major challenge in the logistics industry.

Build a Customer Churn Prediction Model using Decision Trees
Develop a customer churn prediction model using decision tree machine learning algorithms and data science on streaming service data.

PyCaret Project to Build and Deploy an ML App using Streamlit
In this PyCaret Project, you will build a customer segmentation model with PyCaret and deploy the machine learning application using Streamlit.

Build ARCH and GARCH Models in Time Series using Python
In this Project we will build an ARCH and a GARCH model using Python

MLOps Project to Build Search Relevancy Algorithm with SBERT
In this MLOps SBERT project you will learn to build and deploy an accurate and scalable search algorithm on AWS using SBERT and ANNOY to enhance search relevancy in news articles.