# 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 https://github.com/iolucas/nlpython/blob/master/blog/sentiment-analysis-analysis/svm.ipynb

#### 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("reviews.zip", 'r') as zip_ref:
zip_ref.extractall(".")```

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

```with open("reviews.txt") as f:
with open("labels.txt") as f:

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()
onehot_enc.fit(reviews_tokens)```

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

```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()
lsvm.fit(onehot_enc.transform(X_train), 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.

#### Leave any questions and comments below

See ya!

1. Like!! I blog frequently and I really thank you for your content. The article has truly peaked my interest.

2. I have noticed you don’t monetize nlpython.com, 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
in gooogle: murgrabia’s tools

3. 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.

4. 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.

5. Thank you a lot for sharing this with all folks you really recognize what you are speaking approximately! Bookmarked. Kindly also visit my web site =). We could have a link change agreement among us!

6. Thanks for any other excellent article. Where else may anybody get that kind of info in such an ideal means of writing? I have a presentation next week, and I am at the look for such information.

7. 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. . . . . .

8. 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!

9. Since the admin ᧐f thiѕ ѕite is working, no uncertainty very ԛuickⅼy it will be famouѕ,
due to its quality contents.

10. 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!

11. 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.

12. Hi there i am kavin, its my first time to commenting anywhere,
post.

13. Aw, this was an incredibly nice post. Finding the time and actual effort to
generate a very good article… but what can I say… I put things off a whole
lot and never seem to get nearly anything done.

14. Appreciate the recommendation. Let me try it out.

15. I am sure this paragraph has touched all the internet visitors, its really really pleasant article on building up new website.

16. Thanks iin support of sharing such a pleasant idea, piece of writing is pleasant,
thats why i have read it fully

17. 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

Thank you so much and I am taking a look forward to touch you.

Will you please drop me a e-mail?

19. 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.
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.
Tessa

20. 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.

21. 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.

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?

23. Hi! This post couldn’t be written any better! Reading this post reminds me of my previous
will have a good read. Thank you for sharing!

24. 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.

25. Very good website you have here but I was wondering if you knew of