bootstrapping machine learning v0 2 sample

42

Upload: soumya-ranjan

Post on 28-Dec-2015

66 views

Category:

Documents


1 download

DESCRIPTION

Machine learning And Data Analysis

TRANSCRIPT

Page 1: Bootstrapping Machine Learning v0 2 Sample
Page 2: Bootstrapping Machine Learning v0 2 Sample

Thank you for downloading this sample of Bootstrapping Machine Learning! I hope you enjoy it.

If you have any thoughts to share, please email me at [email protected] and I will reply to you personally. I will also take your feedback into account for future revi-sions of the book.

Louis

THANKS

Page 3: Bootstrapping Machine Learning v0 2 Sample

Bootstrapping Machine Learning

Website www.louisdorard.com/mlebook

Version 0.2 — 13 October 2013 All rights reserved

Copyright © 2013 by Louis Dorard

Page 4: Bootstrapping Machine Learning v0 2 Sample

1. Introduction1. Who is this book for2. Structure of this book

2. Machine Learning and Artificial Intelligence1. Motivations2. Machine Learning-powered apps3. A graphical example4. Learning vs hand-coding the rules5. When ML fails

3. Concepts and APIs1. Types of ML problems2. The two pillars of ML3. Big data4. Recommendation5. ML as a Service

4. Examples5. Applying Machine Learning to your problem

1. Transforming your problem into an ML problem2. Feature engineering3. Preparing the data4. Measuring performance5. Beating the competition

CONTENTS

Page 5: Bootstrapping Machine Learning v0 2 Sample

6. Getting real: case studies1. A house valuation tool2. Detecting languages3. Automatic tagging of email

7. What next?Appendix: Google Prediction API

Page 6: Bootstrapping Machine Learning v0 2 Sample

1

INTRODUCTION

Page 7: Bootstrapping Machine Learning v0 2 Sample

Whether we are computer engineers, scientists, hackers, programmers, analysts, thinkers, whether we are building businesses, applications, for ourselves or for others, we are in the business of manipulating infor-mation. We collect it, we move it from one place to another, and we store it as data. The next step after gathering this data is to use our ma-chines to analyze it, to learn from it, and to create new information based on these learnings.

This new information may consist of answers to specific questions we ask about the data, such as “how much?” and “which?”. For instance, we may have recorded real estate transaction prices and we would now ask the price of a property that has not been sold yet. Or we may have received comments to a post on a website and would like to identify which are spam or offensive, to filter them out. Because the answers to these questions are not to be found directly in the data, we call them predictions.

As you may guess, the ability to make such predictions holds tremen-dous value. It can be built into applications to make them smart and thus improve users' experience. Observing user behavior and gathering data from user interactions with an application can be used to make rec-ommendations (think Amazon) and thus to further engage users. It is

WHO IS THIS BOOK FOR?

Page 8: Bootstrapping Machine Learning v0 2 Sample

like having personalized virtual assistants for each user, who work ex-tremely fast. Predictions can also be used to improve a business by ana-lyzing and making sense of customer data. One example of that is fore-seeing which customers are likely to cancel a service subscription, and taking appropriate action before they leave. In all of these examples, making predictions from data results in increased revenue.

Up until a few years ago, the science of making such predictions by hav-ing a machine "learn" from data required a strong background in mathe-matics, probability, statistics, computer science, and a lot of practice. Your only chance would have been to hire experts in the field, or you would have had to become one yourself. But the 2010s have seen a ma-jor technological advance with the introduction of new Prediction Ap-plication Programming Interfaces (API) that can be used with no prior knowledge of data science. Indeed, they simply consist in two meth-ods: one method to feed data, and one method to ask questions and get predictions. It is precisely these Prediction APIs that have motivated this book.

You will find many textbooks on the market to teach you the inner workings of the Machine Learning algorithms and of the Data Mining techniques that are used to make predictions based on data. They will teach you everything from the theory behind these algorithms to how to implement them and how to apply them to real-world problems. This book is not one of them. My intent is to cover only the basic con-

7WHO IS THIS BOOK FOR?

Page 9: Bootstrapping Machine Learning v0 2 Sample

cepts to grasp in order to start making predictions from data and to bootstrap the use of Machine Learning in your projects, applications, or business. I will teach you just enough for you to use Prediction APIs ef-fectively. I will explain how to formulate your problem as a Machine Learning problem, and what the algorithms behind the APIs are sup-posed to do — not how they work. If you ever decide in the end that you want to become an expert in Machine Learning, I believe you will be in a much better position to do so after reading this book, and it will have served as a practical introduction to a vast and complex subject.

I am writing this book to help you make better applications and busi-nesses. I want to show you that nowadays, anyone is in a position to ex-ploit the power of Machine Learning algorithms with minimal coding experience and with the use of Prediction APIs. It doesn't take a Mas-ters degree and it doesn't require a big budget. Actually, you will save a lot of money by not hiring an expert and you will even have better chances to incorporate your domain knowledge by handling things yourself.

I am writing this book for bootstrappers who understand why you should do a job yourself before hiring someone else to do it. I am writ-ing for Über-builders who know how important it is to master all the aspects of a product. I am writing for developers who want to extend their skill-set and for thinkers who want to broaden their horizons. I

8WHO IS THIS BOOK FOR?

Page 10: Bootstrapping Machine Learning v0 2 Sample

am writing to help you succeed and differentiate yourself by using Ma-chine Learning.

9WHO IS THIS BOOK FOR?

Page 11: Bootstrapping Machine Learning v0 2 Sample

Nowadays, anyone is in

a position to exploit the

power of Machine

Learning algorithms with

minimal coding

experience and with the

use of Prediction APIs.

Page 12: Bootstrapping Machine Learning v0 2 Sample

I propose that we start this journey into the world of Machine Learning by first situating ML within what we call Artificial Intelligence and among techniques used to make computers "intelligent."

I will continue with some basic concepts and terminology so we can characterize what ML does more precisely and then give an overview of Prediction APIs and their methods.

ML is about learning from examples, so I’ll give you examples of ML uses to help you learn the terminology and what ML does. If at any point in this book you find that things get too abstract, I invite you to refer to these examples to give context.

The last things we will need to discuss before we can start having some fun with Prediction APIs will be how to formulate your problem into a ML problem, how to prepare your data and how to integrate predic-tions in your application.

Be ready to get your hands dirty after that, because we'll be manipulat-ing these APIs (at last!) on some concrete use cases.

STRUCTURE OF THIS BOOK

Page 13: Bootstrapping Machine Learning v0 2 Sample

To end this journey, I will leave you a few ideas of things you could do next.

You will also find an appendix at the end of this book which provides more details on Google Prediction API.

Happy reading!

12STRUCTURE OF THIS BOOK

Page 14: Bootstrapping Machine Learning v0 2 Sample

2

MACHINE LEARNING AND ARTIFICIAL INTELLIGENCE

Page 15: Bootstrapping Machine Learning v0 2 Sample

Computers are really good at processing lots of information, much more than humans and at faster speeds — think of the millions of mes-sages flowing to the Twitter servers who then have to figure out trend-ing topics in real time. But they lack intelligence. They need to be pro-grammed with precise lists of basic instructions to perform. This makes it challenging to have them do certain things that humans do very eas-ily, but for which we do not know the exact process. An example of that is recognizing hand-written digits: can you explain to me, step-by-step, how you are able to recognize the numbers that form the ZIP code on a hand-written address?

There are diverse techniques to make computers do "intelligent" things, i.e. "human-like" things, and they form the field of Artificial Intelli-gence (AI). A word of caution, first, on this terminology. "Intelligence" and "human-like" may not be what you may think it is. Consider for in-stance that the AI techniques used in chess programs work by examin-ing possible sequences of moves (player A plays this move, then player B may play these moves, then player A may play those moves, etc.) and by evaluating who has the advantage at the end of each of these se-quences, simply by counting who has more pieces on the board. It is ac-tually quite a dumb technique, but it does the job!

MOTIVATIONS

Page 16: Bootstrapping Machine Learning v0 2 Sample

Machine Learning (ML) is a set of AI techniques where intelligence is built by referring to examples. In the hand-written digit recognition problem, you would use ML by providing examples of hand-written digits (in the form of scanned images) along with their corresponding integers between 0 and 9. We refer to these examples as the “data". ML is a set of techniques that make so-called intelligent predictions based on the data.

If computers can be just as smart as a human-being on a given task, the immediate benefit is that this task may be performed with greater speed and at lower costs. For certain tasks, our own human reasoning may be, just as in Machine Learning, to compare to examples, or in other words to refer to our own previous experience. In this case, we might even expect computer to outsmart us, thanks to their ability to process more information.

An example of referring to experience is establishing diagnoses based on patients symptoms. Doctors refer to previous experience for this (their own or the collective experience that has been synthesized in the literature). They refer to examples of previous patients for who a diag-nose had been made, and they compare new patients to them. The hope with computers is that their processing power would allow them to consider many more previous patients, but also that they will be able to consider more information about each patient (demographic, diet,

15MOTIVATIONS

Page 17: Bootstrapping Machine Learning v0 2 Sample

clinical measurements, medical history, etc.). Doing so should improve the overall accuracy of predictions.

16MOTIVATIONS

Page 18: Bootstrapping Machine Learning v0 2 Sample

Machine Learning is a set

of AI techniques where

intelligence is built by

referring to examples.

Page 19: Bootstrapping Machine Learning v0 2 Sample

Let me now give a few examples of how ML is used in popular web and mobile apps.

On the web, giant tech companies such as Google, Yahoo and Microsoft use Machine Learning extensively to perform tasks that require intelli-gence, at a huge scale. They are able to show personalized content to each of the hundreds of millions of visitors of their web properties, by predicting what is most likely to engage each user; besides, every time a user interacts with their website, it gives them example data of which content interests which user. This content can be articles on a news site homepage or ads on a search engine results page, for instance.

Amazon was one of the first sites to personalize users' home pages with tailored recommendations of products to buy, based on previous buys. Recommendations were then also adopted by streaming services such as Netflix for movies and Spotify for music. They help users navi-gate the huge volume of information and content available to them on the Internet. The recommendations made to a user are not only based on his activity on the site, but it is based on the activity of all users — or at least of other users that have been deemed to be similar to this user.

MACHINE LEARNING-POWERED APPS

Page 20: Bootstrapping Machine Learning v0 2 Sample

Google Mail (Gmail) is an interesting example of a web application which uses Machine Learning to offer killer new features to its users. With Priority Inbox, Google have taken email categorization a step fur-ther than traditional email clients who are able to identify spam emails: the system can also identify important emails. I personally find it a huge time saver as it helps me filter the noise in my inbox and keep it clean. Besides, I can help the system learn what is important to me by marking or unmarking emails as important. Unlike desktop clients, Gmail is continuously learning and updating in real time. As you can see, Machine Learning opens up new ways for users to interact with web applications, with the promise of improving their experience. ML-powered features act as individual assistants to applications' users, by making intelligent predictions based on collected data.

On mobile, Apple has empowered the owners of their iDevices with an actual virtual assistant, Siri. Queries are made to it by voice: the user holds down a button, speaks, and waits for Siri to process the query and to respond. The type of queries that Siri can process are making a call, sending a text, finding a business, getting directions, scheduling re-minders and meetings, searching the web, and more. In Siri, ML is used all over the place: for speech recognition (transforming the audio re-cording of the query to text), for recognizing the type of query among all those that can be processed, and for extracting the “parameters” of the query (e.g. for a query of the type “reminder”, extract the name of the task and time to be reminded at — “remind me to do a certain task

19MACHINE LEARNING-POWERED APPS

Page 21: Bootstrapping Machine Learning v0 2 Sample

at a certain time”). Once this is done, Siri calls the appropriate service/API with the right parameters, and voilà! The aim here is to allow mo-bile users to do more with their devices, with less interaction. Often times, mobile usage situations make it more difficult to interact with the device than with a desktop machine. Using Machine Learning is a way to make it easier to interact with mobile apps.

The photo upload interface of the Facebook mobile app gives us an ex-ample of how ML can help improve the usage of an app. Immediately after selecting a photo, the app scans the image for faces, and little dia-logs are positioned below every face that has been detected, prompting the user to tag the person whose face appears on the picture. This cre-ates more engagement and allows Facebook to gently remind its users that the experience is more fun if they tag friends on their pictures.

You will find these examples presented in more details in Chapter 4, but bear with me for a moment until I introduce the main concepts of Machine Learning and some terminology (Chapter 3).

20MACHINE LEARNING-POWERED APPS

Page 22: Bootstrapping Machine Learning v0 2 Sample

ML-powered features act

as individual assistants to

applications’ users, by

making intelligent

predictions based on

collected user data.

Page 23: Bootstrapping Machine Learning v0 2 Sample

Let's now have a look at how ML works with a graphical illustration of the intuition behind a categorization algorithm. Consider objects that are characterized by two attributes, and to which you want to assign one of two categories. For instance, let's say you want to categorize iris flower species (this is the “hello world” of ML). We’re not a botanists, so we’re going to measure petal lengths and widths to characterize the flowers.

Suppose we have collected example data, consisting of a collection of records of petal length and width measurements of setosa and virginica

A GRAPHICAL EXAMPLEpe

tal l

engt

h

petal width

Page 24: Bootstrapping Machine Learning v0 2 Sample

iris flowers. We can plot these measurements as points on a 2D graph showing the petal length on the horizontal axis and the width on the vertical axis; the species information determines the color of each plot-ted point: orange for virginica and blue for setosa.

Now imagine that we're given new pairs of length-width petal measure-ments, but that we're not given any information about the correspond-ing species. We want to predict the most likely species for each of the measurements. In ML-speak, we say that we're given new inputs (here the length-width pairs) and that we want to predict the most likely out-put for each of the inputs.

We plot the new inputs in black for the moment.

23A GRAPHICAL EXAMPLE

Page 25: Bootstrapping Machine Learning v0 2 Sample

It looks fairly simple which color to assign to them, i.e. which predic-tions to make. The leftmost point should be blue and the rightmost point should be orange. Actually, let's just draw a line that separates the orange points from the blue points.

In the future we will affect colors based on which side of the line points fall on. That's it! That's our algorithm. What we're implicitly doing here is comparing new inputs to the example inputs we had, looking at the color (output) of those that are “in the same area”. Our learnings from the examples that were given to us originally have taken the form of a line.

24A GRAPHICAL EXAMPLE

Page 26: Bootstrapping Machine Learning v0 2 Sample

You will notice that there are several lines which would do the job of separating the orange examples from the blue examples. As you can see on the next figure, the line you choose to draw has an impact on some predictions, hence on the quality of predictions.

Machine Learning theory guides the choice of an optimal line. It would also allow us to find an optimal line of separation when the orange and blue populations, as plotted from the examples, would overlap. And it can even find a non linear boundary that would be better suited at sepa-rating the two populations.

25A GRAPHICAL EXAMPLE

Page 27: Bootstrapping Machine Learning v0 2 Sample

26A GRAPHICAL EXAMPLE

Page 28: Bootstrapping Machine Learning v0 2 Sample

As you'll see later, this has been implemented for us in Prediction APIs, so we don't need to worry about how the boundary for separating or-ange from blue was actually found. We just need to know that it is de-termined from the examples.

27A GRAPHICAL EXAMPLE

Page 29: Bootstrapping Machine Learning v0 2 Sample

Before Machine Learning came about, the way we would have catego-rized objects would have been with a set of hard-coded rules, or, put it another way, a combination of if-then-else statements. For instance, we would have said: if the petal width is smaller than w1 and if the length is smaller than l1 then this is a setosa flower; otherwise, if the width is smaller than w2, ... and so on.

This was the sort of things that was being done in the field of language processing, which consists in extracting meaningful information from text or from spoken word: algorithms were based on complex sets of ex-pertly hand-crafted rules that required very deep insights into the prob-lems at hand. There were lots of parameters to consider, and you had to think of all the possible cases.

The use of ML makes predictions much easier than with rule-based sys-tems. ML algorithms are flexible as they create their own “rules” for the problem at hand, based on the analysis of example inputs and corre-sponding outputs. There is no need to code any rule by hand, just to provide examples. In the previous illustration of the functioning of an ML algorithm for categorizing objects, a simple rule was created by

LEARNING VS HAND-CODING THE RULES

Page 30: Bootstrapping Machine Learning v0 2 Sample

learning a boundary between the example inputs in the two categories: if a new input is on this side of the boundary, it is from this category; if it is on the other side, it is from the other category. In the case of a lin-ear boundary and for an input represented by a (x1, x2) pair, the rule takes the form:

if (a*x1 + b*x2 + c >= 0) then category1;

else category2;

This is only one rule, but a very “smart” one since it applies to a quan-tity (a*x1 + b*x2 + c) which is calculated from a combination of the in-put representation and constants (a, b and c) derived from an analysis of the examples.

The late 1980s saw a revolution in language processing with the intro-duction of ML, that led to important improvements over rule-based sys-tems. Some of the earliest ML algorithms, called “decision trees”, pro-duced systems of hard if-then rules similar to existing hand-written rules, but automatically. The more recent algorithms do not follow the structure of decision trees, which were mostly motivated by the desire to make predictions based on decisions that remained easily interpret-able by humans.

Do you find yourself writing rules by hand? It's time to try for a little ML!

29LEARNING VS HAND-CODING THE RULES

Page 31: Bootstrapping Machine Learning v0 2 Sample

As a side note, whereas we illustrated an ML algorithm that is based on a geometric argument, it is worth knowing that there is a whole other class of algorithms based on probabilistic arguments, but I won't go into any details on their functioning since it would require notions of probability theory. These algorithms are popular in language process-ing because they are generally more robust when given unfamiliar or erroneous input, as is very common in the real world. They also pro-duce more reliable results when integrated into larger systems compris-ing multiple subtasks, as they allow a fine modeling of the uncertainty in the predictions.

30LEARNING VS HAND-CODING THE RULES

Page 32: Bootstrapping Machine Learning v0 2 Sample

Machine Learning is

flexible as it

automatically creates its

own rules for the problem

at hand, based on an

analysis of examples.

Page 33: Bootstrapping Machine Learning v0 2 Sample

It looks like, in theory, you can use ML to predict anything. You pro-vide a few examples, let the algorithms figure out the rules, and then apply them to new inputs to get predictions. Hum… I'm sure you didn't think it would be that easy, right?

REPRESENTATIVENESS OF TRAINING DATA

There are three notable things that can make ML fail. The first is when new objects aren't similar enough to any of the examples. We say that the machine fails to “generalize”. Generalizing means, in everyday lan-guage, extending your experience of something to something else which is close. As you saw in the previous categorization example, this is precisely what the machine does. The examples make up the ma-

WHEN ML FAILS

Page 34: Bootstrapping Machine Learning v0 2 Sample

chine's “experience”, and the machine colors new points based on the color of close example points.

The generalization problem occurs when there are not enough exam-ples, or when the examples are not representative enough. If the exam-ples were representative, new inputs should never fall far from exam-ple inputs.

33WHEN ML FAILS

Page 35: Bootstrapping Machine Learning v0 2 Sample

SIMILAR INPUTS, SIMILAR OUTPUTS?

Another thing that makes ML fail is when, among the examples, there often are similar inputs that are associated to different outputs. When that happens in a classification problem, it becomes difficult to clearly separate classes.

We previously assumed that there was a natural separation between classes. We started with a linear example and admitted we could deal with boundaries of any shape, but we still assumed that there was a boundary, which means that one class is in one region of the space and the other class is in another region. Why should things be this way, actu-ally?

When considering Machine Learning, we implicitly assume that there exists a relationship between inputs and outputs. If this is indeed the

34WHEN ML FAILS

Page 36: Bootstrapping Machine Learning v0 2 Sample

case, then similar inputs should be associated to similar outputs and dissimilar inputs should be associated to dissimilar outputs. When visu-alizing the objects that we aim to classify in a space (for instance a 2D space in the previous illustrations), similarity is equivalent to proximity in that space. This explains why points that are of the same color should be relatively close to each other and far from points of the other color, thus leading to the existence of a boundary between the two col-ors.

The machine relies on that same assumption that similar inputs should be associated to the same outputs, at least most of the time. It allows for some exceptions, but if these exceptions become the rule, the machine becomes unable to understand the underlying structure of the exam-ples that were given to it. Typically, this happens when we haven't cap-tured enough information in the input representation. We know that there is a relationship between the objects and the attribute we try to predict, but if the characterizations of these objects are too poor, the rela-tionship won’t be visible. We need to add more, richer characterizations to our inputs so we can ”break” the similarities of those who were asso-ciated to different outputs. In our previous example of flower categori-zation, this would be done by adding more measurements to petal length and width only, such as sepal length and width for instance.

Initially, we only had 2 dimensions in this example (1 dimension is 1 attribute/aspect/characterization of the object you try to classify). This

35WHEN ML FAILS

Page 37: Bootstrapping Machine Learning v0 2 Sample

made it possible to visualize objects with a 2D plot. You can still visual-ize things when there are 3 dimensions, but what if there are more? It is common to have tens or hundreds of dimensions in ML problems. There is still a space associated to that, even though we can’t visualize it. Objects of one class lie in one region of that space, and objects of the other class lie in the rest of the space. Maths can extend the notion of proximity to spaces of higher dimensions, determine boundaries, and tell you which side of the boundary an object is by computing the sign of an expression which involves all of the object’s N components (x1, x2, ... , xN). For a linear boundary, for instance, this expression has the following form:

• in 2D: a*x1 + b*x2 + c• in 3D: a*x1 + b*x2 + c*x3 + d• in 4D: a*x1 + b*x2 + c*x3 + d*x4 + e• etc.

36WHEN ML FAILS

Page 38: Bootstrapping Machine Learning v0 2 Sample

NOISE

This issue of having similar inputs associated to dissimilar outputs may persist for another reason: noise in the outputs. By “noise” we mean that the example outputs that we recorded are not what they should be. Let me illustrate this: we implicitly assumed that our flower examples had been correctly classified... but this had to be done by someone at some point, right? An expert botanist had to look at the ex-ample flowers and say: “this is a setosa... this is a virginica”. He thus acted as a “teacher” who made it possible for the machine to learn. But what if the teacher had made mistakes? Besides, there is always a chance of introducing errors when recording data...

Noise is also found in measurements, which are always a bit off be-cause our tools and sensors are imperfect and only work to a certain de-gree of precision. Other times, noise is caused by phenomena that are specific to the domain to which we want to apply predictions and that we fail to capture in the inputs. Ideally, the output should be com-pletely determined from the input. But consider for instance that we re-cord real estate transaction prices along with property attributes, in or-der to create examples for later being able to predict the value of a prop-erty based on its attributes. There are circumstances that can bring a price below or above actual (true) market value, for instance when a property needs to be sold quickly or when someone wants to buy a property that wasn’t for sale originally. Algorithms are designed to deal

37WHEN ML FAILS

Page 39: Bootstrapping Machine Learning v0 2 Sample

with noise to a certain point, but as you can imagine, the more noise there is, the more random things look, the harder it is to learn anything. In any case, the fact that we have “wrong” examples perturbs our abil-ity to learn.

With these three cautionary remarks, it has become clear that our suc-cess in applying Machine Learning is tied to our ability to gather a good set of well-characterized examples…

38WHEN ML FAILS

Page 40: Bootstrapping Machine Learning v0 2 Sample

READ THE RESTWant to read the rest of this book?Interested in learning about the differ-ent types of ML problems (including recommendation), the APIs that exist to tackle them, how to use them for your problem, and in seeing it all in ac-tion with a few case studies?

Head over to www.louisdorard.com/mlebook

to order your full copy or one of the packages.

Page 41: Bootstrapping Machine Learning v0 2 Sample

I studied Machine Learning at Univer-sity College London, where I ob-

tained my Ph.D. At the same time, I also bootstrapped an online busi-ness.

In between that and writing this book, I organized a challenge and work-

shop on website optimization at the Interna-tional Conference on Machine Learning, and then I served as Chief Sci-ence Officer in a startup where I was in charge of starting a Research & Development program and turning it into a product.

I love starting things from scratch and I am passionate about technologi-cal innovation in web and mobile apps. My experience has taught me the importance of simplicity and of starting small.

My goal with this book is to help you create better apps by using Ma-chine Learning and Prediction APIs. 

ABOUT LOUIS DORARD

Page 42: Bootstrapping Machine Learning v0 2 Sample

You can find out more about me on LinkedIn (www.linkedin.com/in/louisdorard) and you can follow me on Twitter (@louisdorard).

41ABOUT LOUIS DORARD