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

Abhinav Agarwal

Graduate Student at Northwestern University
linkedin profile url

I come from Northwestern University, which is ranked 9th in the US. Although the high-quality academics at school taught me all the basics I needed, obtaining practical experience was a challenge.... Read More

Relevant Projects

Deploy Transformer-BART Model on Paperspace Cloud
In this MLOps Project you will learn how to deploy a Tranaformer BART Model for Abstractive Text Summarization on Paperspace Private Cloud

Census Income Data Set Project-Predict Adult Census Income
Use the Adult Income dataset to predict whether income exceeds 50K yr based oncensus data.

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

PyTorch Project to Build a GAN Model on MNIST Dataset
In this deep learning project, you will learn how to build a GAN Model on MNIST Dataset for generating new images of handwritten digits.

Deep Learning Project for Time Series Forecasting in Python
Deep Learning for Time Series Forecasting in Python -A Hands-On Approach to Build Deep Learning Models (MLP, CNN, LSTM, and a Hybrid Model CNN-LSTM) on Time Series Data.

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.

Build a Logistic Regression Model in Python from Scratch
Regression project to implement logistic regression in python from scratch on streaming app data.

Deploy Transformer BART Model for Text summarization on GCP
Learn to Deploy a Machine Learning Model for the Abstractive Text Summarization on Google Cloud Platform (GCP)

Build a Review Classification Model using Gated Recurrent Unit
In this Machine Learning project, you will build a classification model in python to classify the reviews of an app on a scale of 1 to 5 using Gated Recurrent Unit.

Isolation Forest Model and LOF for Anomaly Detection in Python
Credit Card Fraud Detection Project - Build an Isolation Forest Model and Local Outlier Factor (LOF) in Python to identify fraudulent credit card transactions.