What is padding in NLP?

This recipe explains what is padding in NLP

Recipe Objective

What is padding in NLP?

Padding As we know all the neural networks needs to have the inputs that should be in similar shape and size. When we pre-process the texts and use the texts as an inputs for our Model. Note that not all the sequences have the same length, as we can say naturally some of the sequences are long in lengths and some are short. Where we know that we need to have the inputs with the same size, now here padding comes into picture. The inputs should be in same size at that time padding is necessary.

NLP Techniques to Learn for your Next NLP Project

Step 1 - Take Sample text

Detail1 = ['Jon', '26', 'Canada'] Detail2 = ['Heena', '24', 'India'] Detail3 = ['Shawn', '27', 'California']

Here we are taking the sample text as "name", "age" and "address" of different person.

Step 2 - Apply left padding

for Details in [Detail1,Detail2,Detail3]: for entry in Details: print(entry.ljust(25), end='') print()

Jon                      26                       Canada                   
Heena                    24                       India                    
Shawn                    27                       California               

In the above we applying left padding to text by using .ljust

Step 3 - Center Padding

Sample_text = ["Jon playes cricket", "His favourite player is MS Dhoni","Sometimes he loves to play football"] for text in Sample_text: print(text.center(50, ' '))

                Jon playes cricket                
         His favourite player is MS Dhoni         
       Sometimes he loves to play football        

Step 4 - Right Padding

for ele in [Detail1, Detail2, Detail3]: for entry in ele: print(entry.rjust(30), end='') print()

                           Jon                            26                        Canada
                         Heena                            24                         India
                         Shawn                            27                    California

What Users are saying..

profile image

Savvy Sahai

Data Science Intern, Capgemini
linkedin profile url

As a student looking to break into the field of data engineering and data science, one can get really confused as to which path to take. Very few ways to do it are Google, YouTube, etc. I was one of... Read More

Relevant Projects

Build Classification Algorithms for Digital Transformation[Banking]
Implement a machine learning approach using various classification techniques in Python to examine the digitalisation process of bank customers.

Detectron2 Object Detection and Segmentation Example Python
Object Detection using Detectron2 - Build a Dectectron2 model to detect the zones and inhibitions in antibiogram images.

Forecasting Business KPI's with Tensorflow and Python
In this machine learning project, you will use the video clip of an IPL match played between CSK and RCB to forecast key performance indicators like the number of appearances of a brand logo, the frames, and the shortest and longest area percentage in the video.

Learn How to Build PyTorch Neural Networks from Scratch
In this deep learning project, you will learn how to build PyTorch neural networks from scratch.

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

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.

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.

Deep Learning Project for Text Detection in Images using Python
CV2 Text Detection Code for Images using Python -Build a CRNN deep learning model to predict the single-line text in a given image.

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.

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