Reader small image

You're reading from  Machine Learning for Mobile

Product typeBook
Published inDec 2018
PublisherPackt
ISBN-139781788629355
Edition1st Edition
Right arrow
Authors (2):
Revathi Gopalakrishnan
Revathi Gopalakrishnan
author image
Revathi Gopalakrishnan

Revathi Gopalakrishnan is a software professional with more than 17 years of experience in the IT industry. She has worked extensively in mobile application development and has played various roles, including developer and architect, and has led various enterprise mobile enablement initiatives for large organizations. She has also worked on a host of consumer applications for various customers around the globe. She has an interest in emerging areas, and machine learning is one of them. Through this book, she has tried to bring out how machine learning can make mobile application development more interesting and super cool. Revathi resides in Chennai and enjoys her weekends with her husband and her two lovely daughters.
Read more about Revathi Gopalakrishnan

Avinash Venkateswarlu
Avinash Venkateswarlu
author image
Avinash Venkateswarlu

Avinash Venkateswarlu has more than 3 years' experience in IT and is currently exploring mobile machine learning. He has worked in enterprise mobile enablement projects and is interested in emerging technologies such as mobile machine learning and cryptocurrency. Venkateswarlu works in Chennai, but enjoys spending his weekends in his home town, Nellore. He likes to do farming or yoga when he is not in front of his laptop exploring emerging technologies.
Read more about Avinash Venkateswarlu

View More author details
Right arrow

Chapter 7. Spam Message Detection

This chapter will provide you with an overview of natural language processing (NLP) and discuss how NLP can be combined with machine learning to provide solutions to problems. Then, the chapter will take a real-world use case of doingspam message detection by utilizing NLP, combined with the linear SVM classification model. The program will be implemented as a mobile application using Core ML for iOS.

To handle text in machine learning algorithms, we will go through the various NLP techniques that will be used on the text data to make it ready for learning algorithms. Once the text is prepared, we will see how we can classify it using the linear SVM model. 

Problem definition: The bulk SMS message data is provided, and these messages need to be classified as spam or non-spam messages.

We will be covering the following topics in this chapter:

  • Understanding NLP
  • Understanding the linear SVM algorithm
  • Solving the problem using linear SVM in Core ML:
    • Technical requirements...

Understanding NLP


NLP is a huge topic, and it is beyond the scope of this book to go into detail on the subject. However, in this section, we will go through the high-level details of NLP and try to understand the key concepts required to prepare and process the textual data using NLP, in order to make it ready for consumption by machine learning algorithms for prediction

Introducing NLP

Huge, unstructured textual data is getting generated on a daily basis. Social media, websites such as Twitter and Facebook, and communication apps, such as WhatsApp, generate an enormous volume of this unstructured data daily—not to mention the volume created by blogs, news articles, product reviews, service reviews, advertisements, emails, and SMS. So, to summarize, there is huge data (in TBS).

However, it is not possible for a computer to get any insight from this data and to carry out specific actions based on the insights, directly from this huge data, because of the following reasons:

  • The data is unstructured...

Understanding linear SVM algorithm


InChapter 2, Supervised and Unsupervised Learning Algorithms, we covered the SVM algorithm and now have an idea of how the SVM model works. A linear support vector machine or linear SVM is a linear classifier that tries to find a hyperplane with the largest margin that splits the input space into two regions. 

Note

A hyperplane is a generalization of a plane. In one dimension, a hyperplane is called a point. In two dimensions, it is a line. In three dimensions, it is a plane. In more dimensions, you can call it a hyperplane.

As we saw, the goal of SVM is to identify the hyperplane that tries to find the largest margin that splits the input space into two regions. If the input space is linearly separable, it is easy to separate them. However, in real life, we find that the input space is very non-linear:

In the preceding scenario, the SVM can help us separate the red and blue balls by using what is called a Kernel Trick, which is the method of using a linear...

Solving the problem using linear SVM in Core ML


In this section, we are going to look at how we can solve the spam message detection problem using all the concepts we have gone through in this chapter.

We are going to take a bunch of SMS messages and attempt to classify them as spam or non-spam. This is a classification problem and we will use the linear SVM algorithm to perform this, considering the advantages of using this algorithm for text classification.

We are going to use NLP techniques to convert the data-SMS messages into a feature vector to feed into the linear SVM algorithm. We are going to use the scikit-learn vectorizer methods to transform the SMS messages into the TF-IDF vector, which could be fed into the linear SVM model to perform SMS spam detection (classification into spam and non-spam).

About the data

The data that we are using to create the model that detects the spam messages is taken from http://www.dt.fee.unicamp.br/~tiago/smsspamcollection/, which contains 747 spam...

Summary


In this chapter, we went through many things, such as, understanding NLP at a high level. There are various steps involved in NLP, such as text preprocessing, as well as techniques to carry this out, such as feature engineering and methods to perform feature engineering and classification or clustering of the feature vectors. We also looked into the linear SVM algorithm in which we went through the details of the SVM algorithm, the kernel function, and how it is more applicable to text classification.

We solved our problem using linear SVM in Core ML and we also saw a practical example of performing spam message detection using the linear SVM algorithm model that we developed in scikit learn and converted into a Core ML model. We wrote an iOS application using the converted Core ML model.

In the next chapter, we will be introduced to another ML framework, Fritz, which tries to solve the common problems that we see in model deployment and upgrades, and the unification of handling ML...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Machine Learning for Mobile
Published in: Dec 2018Publisher: PacktISBN-13: 9781788629355
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Authors (2)

author image
Revathi Gopalakrishnan

Revathi Gopalakrishnan is a software professional with more than 17 years of experience in the IT industry. She has worked extensively in mobile application development and has played various roles, including developer and architect, and has led various enterprise mobile enablement initiatives for large organizations. She has also worked on a host of consumer applications for various customers around the globe. She has an interest in emerging areas, and machine learning is one of them. Through this book, she has tried to bring out how machine learning can make mobile application development more interesting and super cool. Revathi resides in Chennai and enjoys her weekends with her husband and her two lovely daughters.
Read more about Revathi Gopalakrishnan

author image
Avinash Venkateswarlu

Avinash Venkateswarlu has more than 3 years' experience in IT and is currently exploring mobile machine learning. He has worked in enterprise mobile enablement projects and is interested in emerging technologies such as mobile machine learning and cryptocurrency. Venkateswarlu works in Chennai, but enjoys spending his weekends in his home town, Nellore. He likes to do farming or yoga when he is not in front of his laptop exploring emerging technologies.
Read more about Avinash Venkateswarlu