Input Preparation for Variable Length Input Sequence

Rishikaroy
Jul 17 · 2 min read

Machine Learning is study of various Algorithmic Model to analyze the datasets for a particular problem.

Machine Learning requires vectorized datasets for the application of various Algoritmic models.Suppose,you are given a problem statement of variable length input sequence,in that case your datasets should be changed into equal length Sequence.

To convert the datsets into equal length sequence,concept of padding can be used. Padding can be used, whereby you would have to fix the length of each sample (either to the length of the longest sample, or to a fixed length — longer samples would be trimmed or filtered somehow to fit into that length).Machine Learning have various methods of padding:

In this program,we were provided with vairiable length input sequence which is converted to equal length sequence by padding of zeroes(Maximum length of input sequence is 22)
pad_sequences() function in the Keras deep learning library can be used to pad variable length sequences.

Python Implementation:

import pandas as pd
import csv
with open(r’C:\Users\1729398\Downloads\test.csv’) as ifile:
read = csv.reader(ifile)
count1=0
for row in read:
count1=len(row)
while(count1!=22):
row.append(‘0’)
count1+=1

with open(r’C:\Users\1729398\Downloads\b1.csv’,”a”) as csvFile:
writer = csv.writer(csvFile)
writer.writerow(row)
csvFile.close()
from keras.preprocessing.sequence import pad_sequencessequences = [
[298,300],
[500],
[469,191]
]
padded = pad_sequences(sequences, padding=’post’)
print(padded)

This is an educational post and it is inspired from Prof.Jason Brownlee’s Tutorials.

Rishikaroy

Written by

Data Science enthusiast

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade