Sentiment Analysis Analysis Part 2 —Support Vector Machines

Lucas Oliveira
Aug 1, 2017 · 2 min read

In the next set of topics we will dive into different approachs to solve the hello world problem of the NLP world, the sentiment analysis.

Check the other parts: Part1 Part2 Part3

The code for this implementation is at

Sentiment analysis is an area of research that aims to tell if the sentiment of a portion of text is positive or negative.

The SVM Classifier

This classifier works trying to create a line that divides the dataset leaving the larger margin as possible between points called support vectors. As per the figure below, the line A has a larger margin than the line B, so the points divided by the line A have to travel much more to cross the division, than if the data was divided by B, so in this case we would choose the line A.

The Code

For this task we will use scikit-learn, an open source machine learning library.

Our dataset is composed of movie reviews and labels telling whether the review is negative or positive. Let’s load the dataset:

The reviews file is a little big, so it is in zip format. Let’s Extract it:

Now that we have the reviews.txt and labels.txt files, we load them to the memory:

Next we load the module to transform our review inputs into binary vectors with the help of the class :

After that we split the data into training and test set with the function:

We then create our SVM classifier with the class and train it:

Training the model took about 2 seconds.

After training, we use the function to check the performance of the classifier:

Computing the score took about 1 second only!

Running the classifier a few times we get around 85% of accuracy, basically the same of the result of the naive bayes classifier.

Please recommend this post so we can spread the knowledge

Leave any questions and comments below

See ya!


Deep learning and natural language processing with python.

Lucas Oliveira

Written by

Engineer focused on Artificial Inteligence



Deep learning and natural language processing with python.

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