Sitemap
TDS Archive

An archive of data science, data analytics, data engineering, machine learning, and artificial intelligence writing from the former Towards Data Science Medium publication.

Member-only story

Mastering Long Short-Term Memory with Python: Unleashing the Power of LSTM in NLP

17 min readNov 28, 2023

--

Photo by Sven Brandsma on Unsplash

This work is a continuation of my article about RNNs and NLP with Python. A natural progression of a deep learning network with a simple recurrent layer is a deep learning network with a Long Short Term Memory (LSTM for short) layer.

As with the RNN and NLP, I will try to explain the LSTM layer in great detail and code the forward pass of the layer from scratch.

All the codes can be viewed here: https://github.com/Eligijus112/NLP-python

We will work with the same dataset¹ as in the previous article:

# Data wrangling
import pandas as pd

# Reading the data
d = pd.read_csv('input/Tweets.csv', header=None)

# Adding the columns
d.columns = ['INDEX', 'GAME', "SENTIMENT", 'TEXT']

# Leaving only the positive and the negative sentiments
d = d[d['SENTIMENT'].isin(['Positive', 'Negative'])]

# Encoding the sentiments that the negative will be 1 and the positive 0
d['SENTIMENT'] = d['SENTIMENT'].apply(lambda x: 0 if x == 'Positive' else 1)

# Dropping missing values
d = d.dropna()

--

--

TDS Archive
TDS Archive

Published in TDS Archive

An archive of data science, data analytics, data engineering, machine learning, and artificial intelligence writing from the former Towards Data Science Medium publication.

Eligijus Bujokas
Eligijus Bujokas

Written by Eligijus Bujokas

A person who tries to understand the world through data and equations