Sentiment Analysis Analysis Part 2 – Support Vector Machines

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:

import zipfile
with zipfile.ZipFile("", 'r') as zip_ref:

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

with open("reviews.txt") as f:
    reviews ="\n")
with open("labels.txt") as f:
   labels ="\n")
reviews_tokens = [review.split() for review in reviews]

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

from sklearn.preprocessing import MultiLabelBinarizer

onehot_enc = MultiLabelBinarizer()

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

from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(reviews_tokens, labels, test_size=0.25, random_state=None)

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

from sklearn.svm import LinearSVC
lsvm = LinearSVC(), y_train)

Training the model took about 2 seconds.

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

score = lsvm.score(onehot_enc.transform(X_test), y_test)

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!

29 comments / Add your comment below

  1. I have noticed you don’t monetize, don’t waste your traffic, you can earn additional bucks every month with new monetization method.
    This is the best adsense alternative for any type
    of website (they approve all websites), for more info simply search
    in gooogle: murgrabia’s tools

  2. I’d like to thank you for the efforts you’ve put in penning this website.
    I’m hoping to see the same high-grade content
    from you in the future as well. In fact, your creative writing abilities has encouraged me to get my very own site now 😉 – Calator prin Romania.

  3. After examine just a few of the blog posts in your website now, and I truly like your way of blogging. I bookmarked it to my bookmark web site list and will probably be checking again soon. Pls take a look at my web page as effectively and let me know what you think.

  4. Howdy very nice web site!! Guy .. Beautiful .. Wonderful .. I’ll bookmark your website and take the feeds additionally…I am satisfied to find numerous useful information right here within the submit, we need develop more strategies on this regard, thanks for sharing. . . . . .

  5. Right here is the right website for anyone who would like to understand this topic.

    You know so much its almost hard to argue with you (not that I really would want to…HaHa).
    You definitely put a fresh spin on a topic which has been discussed for years.
    Wonderful stuff, just great!

  6. Sentiment Analysis Analysis Part 2 – Support Vector Machines este unul din cele mai tari bloguri despre calatori
    pe care le-am descoperit. Tine-o la fel si
    continua cu asemenea informatii. Multumesc!

  7. Normally I do not read post on blogs, however I would like to say that this write-up very forced me to
    try and do so! Your writing style has been amazed me.
    Thank you, quite great article.

  8. I just could not depart your web site before suggesting that I actually
    enjoyed the standard information an individual supply in your visitors?

    Is gonna be again incessantly in order to check up on new posts

  9. On the subject of purchasing, these types
    of books are definitely library fare for me. They only good on the first read and if I not going to
    read or reference a book ever again, why own it?
    Obviously this is my own opinion and I don know Zes mum
    or her reading preference. I have far too many books already so I have
    to be picky about what I actually want to own.. The guy makes
    a lot of good points but I think he also wrong about a lot of points as well.

    We have never done family dinners but kidlet has impeccable table manners and will
    actually ask for fruit (and frequently,
    olives) as a snack. This morning he asked me for
    egg salad on crackers for his breakfast.

    full lace wigs I wanted to be included but I didn know HOW.
    I would have loved for my mom to take some time
    out of her day to at least attempt to help me instead of
    ignoring the situation all together. I really appreciate how awesome of a mom you are with Violet, even though I don know
    her, but just because I know how she feels.. Flow Blue Patterns The
    motifs are assembled and arranged differently by each Flow Blue artists,
    and thus the patterns of the various manufacturers vary although they include similar motifs.
    Also, although the motifs are oriental, the interpretation is Western in spirit and the
    border is often definitely English in motif as well as feeling.
    However, once you have purchased a few pieces of Flow
    Blue, you will feel most certainly feel its charm. full lace wigs

    wigs for women When the Atlantis team encounters Ronon for the first time in season 2’s “Runner”, they arrange a removal of
    the tracking device in his back. Ronon returns with them to Atlantis, where
    he learns that Sateda had been completely destroyed.[5]
    Sheppard offers Ronon a place on his team in “Duet” after Ronon proves his worth against the Marines and
    Teyla in hand to hand combat and demonstrates his experience with guns.[6] Ronon finds out in “Trinity” that about 300 Satedans survived the Wraith attacks and moved to other planets.[10] In season 3’s “Sateda”, Ronon gets revenge on the Wraith who
    originally turned him into a Runner.[4] Ronon meets with three of
    his old Satedan friends in season 4’s “Reunion” and seriously considers leaving
    the Atlantis Expedition for the first time until he learns that his old friends are Wraith
    Worshippers. In the end, Ronon remains with the Atlantis Expedition, stating that
    his friends are “right here”.[11]. wigs for women

    U Tip Extensions And your kids will inherit a piece of it too.Oppression doesn just
    go away because you change a few laws around.
    It lingers for decades, centuries even.”I would be more inclined to agree with you that the word still holds power today if the person saying it did have power over the person it directed at”That just thick.
    He a black cop, she a white lady. Always polite, always ultra helpful.
    We all loved working with her. In fact, every time my kids visited the store she would go out of her way to play with them and keep them entertained..
    U Tip Extensions

    Lace Wigs I get it for a while every few years. That ALL about light and film stock and lenses.
    Most of what I do for film now (theatrical lighting) is a special
    dept that usually doesn exist in film as the cinematographer and
    their dept takes care of the lighting of a film.
    Well, as far as the 3D printers, not much. They do the wheels for the cars
    like I mentioned, and the 6th graders design pucks for the knock hockey boards they
    construct in class. We just got them last year, we have 3 Taz 6s, 2 Taz
    5s, and a Makerbot Replicator Mini which the administration bought for fuck knows why.
    Lace Wigs

    wigs online He states his conversion was bolstered by a church in Liberia where a Bishop Kun Kun is pastor.
    They claimed to have heard from God to fast 54 days for his deliverance.
    After the fast they claim God gave them spiritual powers to infiltrate
    his coven in the city of Liberia and preach to him.
    I used prosaide on many clients and never had a reaction.If you don want
    to buy and mix PAX paints, you can always use something like this.The instructions for the bald cap are pretty accurate.
    No, I wouldn advise trying to paint it anywhere other than on your head.
    Follow the instructions they provided, and here a couple tips.
    wigs online

    wigs for women Let stop here. You keep arguing against something you have a
    beef with rather than having a dialog (repeatedly responding to unrelated paragraphs with your sanctions talking point,
    etc.) You keep referring to “NK” as a monolithic actor
    instead of separating the population and the regime.
    And you keep normalizing it. There was an urgent medical reason to embrace cancer with
    a smile: a “positive attitude” is supposedly essential to
    recovery. There were many times during chemotherapy, she encountered this assertion on websites, in books, from
    oncology nurses and fellow sufferers. One study found 60% of women who
    had been treated for the disease attributing their continued survival to
    a “positive attitude” wigs for women.

  10. You can certainly see your enthusiasm within the work you write. The world hopes for even more passionate writers such as you who aren’t afraid to say how they believe. All the time follow your heart.

  11. Hiya, I’m really glad I’ve found this info. Today bloggers publish just about gossips and web and this is actually irritating. A good web site with interesting content, this is what I need. Thanks for keeping this website, I will be visiting it. Do you do newsletters? Can’t find it.

  12. Thanks , I’ve recently been searching for info about this topic for
    a while and yours is the greatest I have found out so far. But,
    what about the bottom line? Are you certain concerning the source?

  13. Hi! This post couldn’t be written any better! Reading this post reminds me of my previous
    room mate! He always kept chatting about this.
    I will forward this article to him. Pretty sure he
    will have a good read. Thank you for sharing!

  14. I’ve been exploring for a little bit for any high quality articles or blog posts on this kind of area .
    Exploring in Yahoo I eventually stumbled
    upon this site. Studying this information So i’m happy to show that I have an incredibly
    good uncanny feeling I came upon just what I needed.
    I so much without a doubt will make sure to don?t forget
    this site and provides it a glance on a continuing basis.

  15. Very good website you have here but I was wondering if you knew of
    any community forums that cover the same topics talked about in this article?
    I’d really love to be a part of online community where I can get advice from other knowledgeable people that
    share the same interest. If you have any recommendations, please let me know.
    Bless you!

  16. Thank you, I have recently been searching for info about this subject for a long time and yours is the greatest I’ve found out till now.
    However, what concerning the bottom line? Are you certain in regards to the supply?

  17. You really make it seem so easy together with your presentation however I find this
    matter to be really one thing that I think I might by no means understand.

    It kind of feels too complex and very wide for me. I am having a look ahead in your next submit, I will try to get the hold of it!

Leave a Reply

Your email address will not be published.