8.8 Anomaly Detection | Finding unusual events -- [Machine Learning | Andrew Ng]
 Let's look at our second unsupervised learning algorithm. Anomaly detection algorithms look at an unlabeled data set of normal events and thereby learns to detect or to raise a red flag for if there is an unusual or an anomalous event. Let's look at an example. Some of my friends were working on using anomaly detection to detect possible problems with aircraft engines that were being manufactured. When a company makes an aircraft engine, you really want that aircraft engine to be reliable and function well because an aircraft engine failure has very negative consequences. So some of my friends were using anomaly detection to check if an aircraft engine after it was manufactured seemed anomalous or if there seemed to be anything wrong with it. Here's the idea. After an aircraft engine rolls off the assembly line, you can compute a number of different features of the aircraft engine. So say feature X1 measures the heat generated by the engine, feature X2 measures the vibration intensity, and so on and so forth for additional features as well. But to simplify this slide a bit, I'm going to use just two features, X1 and X2, corresponding to the heat and the vibrations of the engine. Now it turns out that aircraft engine manufacturers don't make that many bad engines. And so the easier type of data to collect would be if you have manufactured M aircraft engines to collect the features X1 and X2 about how these M engines behave. And probably most of them are just fine, they're normal engines rather than ones with a defect or a flaw in them. And the anomaly detection problem is after the learning algorithm has seen these M examples of how aircraft engines typically behave in terms of how much heat they generate and how much they vibrate, if a brand new aircraft engine were to roll off the assembly line and if it had a new feature vector given by X test, we'd like to know does this engine look similar to ones that have been manufactured before? So is this probably okay? Or is there something really weird about this engine which might cause this performance to be suspect, meaning that maybe we should inspect it even more carefully before we let it get shipped out and be installed in an airplane and then hopefully nothing will go wrong with it. Here's how an anomaly detection algorithm works. Let me plot the examples X1 through XM over here via these crosses where each cross, each data point in this plot corresponds to a specific engine with a specific amount of heat and a specific amount of vibrations. If this new aircraft engine X test rolls off the assembly line and if you were to plot these values of X1 and X2 and if it were here, you'd say, okay, that looks probably okay. It looks very similar to other aircraft engines. Maybe I don't need to worry about this one. But if this new aircraft engine has a heat and vibration signature that is say all the way down here, then this data point down here looks very different than the ones we saw up on top. And so we will probably say, boy, this looks like an anomaly. This doesn't look like the examples I've seen before. We better inspect this more carefully before we let this engine get installed on an airplane. How can you have an algorithm address this problem? The most common way to carry out anomaly detection is through a technique called density estimation. And what that means is when you're given your training set of these M examples, the first thing you do is build a model for the probability of X. In other words, the learning algorithm will try to figure out what are the values of the features X1 and X2 that have high probability and what are the values that are less likely or have lower chance or lower probability of being seen in the data set. In this example that we have here, I think it is quite likely to see examples in that little ellipse in the middle. So that region in the middle would have high probability. Maybe things in this ellipse have a little bit lower probability. Things in this ellipse or this oval have even lower probability and things outside have even lower probability. The details of how you decide from the training sets what regions are higher versus lower probability is something we'll see in the next few videos. And having modeled or having learned to model for P of X when you are given the new test example X test, what you will do is then compute the probability of X test and if it is small or more precisely if it is less than some small number that I'm going to call epsilon. This is a Greek alphabet epsilon, which you should think of as a small number, which means that P of X is very small or in other words, the specific value of X that you saw for a certain user was very unlikely relative to other users that you have seen. But if P of X test is less than some small threshold or some small number epsilon, we will raise a flag to say that this could be an anomaly. So for example, if X test was all the way down here, the probability of an example landing all the way out here is actually quite low. And so hopefully P of X test for this value of X test will be less than epsilon. And so we would flag this as an anomaly. Whereas in contrast, if P of X test is not less than epsilon, if P of X says is greater than equal to epsilon, then we will say that it looks okay, this doesn't look like an anomaly. And that corresponds to if you had an example in here, say, where our model P of X will say that examples near the middle here, they're actually quite high probability, it's a very high chance that the new airplane engine will have features close to these inner ellipses. And so P of X test will be large for those examples and we'll say it's okay and it's not an anomaly. Anomaly detection is used today in many applications. It is frequently used in fraud detection, where, for example, if you are running a website with many different features, if you compute Xi to be the features of user eyes activities. So for example, the features Xi might be the features of a particular user eyes activities. And examples of features might include how often does this user log in? And how many web pages do they visit? How many transactions are they making? Or how many posts on the discussion forum are they making? What is their typing speed? How many characters per second do they seem able to type? With data like this, you can then model P of X from data to model what is the typical behavior of a given user. In a common workflow of fraud detection, you wouldn't automatically turn off an account just because it seemed anomalous, but instead you may ask the security team to take a closer look or put in some additional security checks, such as ask the user to verify their identity with a cell phone number or ask them to pause a capture to prove that they're human and so on. But algorithms like this are routinely used today to try to find unusual or maybe slightly suspicious activity so they can more carefully screen those accounts to make sure there isn't something fraudulent. And this type of fraud detection is used both to find fake accounts and this type of algorithm is also used frequently to try to identify financial fraud, such as if there's a very unusual pattern of purchases, then that may be something well worth a security team taking a more careful look at. Anomaly detection is also frequently used in manufacturing. You saw an example on the previous slide with aircraft engine manufacturing, but many manufacturers in multiple continents in many, many factories will routinely use anomaly detection to see if whatever they just manufactured, anything from an airplane engine to a printed circuit board to a smartphone to a motor to many, many things to see if you've just manufactured a unit that somehow behaves strangely because that may indicate that there's something wrong with your airplane engine or printed circuit boards or what have you that might cause you to want to take a more careful look before you ship that object to a customer. It's also used to monitor computers in clusters and in data centers where if XI are the features of a certain machine I, such as if the features captured the memory usage, the number of disk accesses per second, CPU load, features can also be ratios such as the ratio of CPU load to network traffic. And if ever a specific computer behaves very differently than other computers, it might be worth taking a look at that computer to see if something is wrong with it, such as if it has had a hot disk failure or a network card failure or something's wrong with it, or if maybe it has been hacked into. Anomaly detection is one of those algorithms that is very widely used even though you don't seem to hear people talk about it that much. I remember the first time I worked on the commercial application of anomaly detection was when I was helping a telco company put in place anomaly detection to see when any one of the cell towers was behaving in an unusual way because that probably meant there was something wrong with the cell tower and so they want to get a technician to take a look. So hopefully that helped more people get good cell phone coverage. And I've also used anomaly detection to find fraudulent financial transactions. And these days I often use it to help manufacturing companies find anomalous parts that they may have manufactured but should inspect more often. So it is a very useful tool to have in your tool chest. And in the next few videos, we'll talk about how you can build and get these algorithms to work for yourself. In order to get anomalous detection algorithms to work, we'll need to use a Gaussian distribution to model the data, pfx. So let's go on to the next video to talk about Gaussian distributions.
[{"start": 0.0, "end": 6.96, "text": " Let's look at our second unsupervised learning algorithm."}, {"start": 6.96, "end": 13.8, "text": " Anomaly detection algorithms look at an unlabeled data set of normal events and thereby learns"}, {"start": 13.8, "end": 20.72, "text": " to detect or to raise a red flag for if there is an unusual or an anomalous event."}, {"start": 20.72, "end": 23.02, "text": " Let's look at an example."}, {"start": 23.02, "end": 29.36, "text": " Some of my friends were working on using anomaly detection to detect possible problems with"}, {"start": 29.36, "end": 33.76, "text": " aircraft engines that were being manufactured."}, {"start": 33.76, "end": 39.04, "text": " When a company makes an aircraft engine, you really want that aircraft engine to be reliable"}, {"start": 39.04, "end": 45.7, "text": " and function well because an aircraft engine failure has very negative consequences."}, {"start": 45.7, "end": 52.8, "text": " So some of my friends were using anomaly detection to check if an aircraft engine after it was"}, {"start": 52.8, "end": 57.92, "text": " manufactured seemed anomalous or if there seemed to be anything wrong with it."}, {"start": 57.92, "end": 61.04, "text": " Here's the idea."}, {"start": 61.04, "end": 66.4, "text": " After an aircraft engine rolls off the assembly line, you can compute a number of different"}, {"start": 66.4, "end": 68.32000000000001, "text": " features of the aircraft engine."}, {"start": 68.32000000000001, "end": 76.16, "text": " So say feature X1 measures the heat generated by the engine, feature X2 measures the vibration"}, {"start": 76.16, "end": 80.92, "text": " intensity, and so on and so forth for additional features as well."}, {"start": 80.92, "end": 86.78, "text": " But to simplify this slide a bit, I'm going to use just two features, X1 and X2, corresponding"}, {"start": 86.78, "end": 90.92, "text": " to the heat and the vibrations of the engine."}, {"start": 90.92, "end": 97.2, "text": " Now it turns out that aircraft engine manufacturers don't make that many bad engines."}, {"start": 97.2, "end": 104.48, "text": " And so the easier type of data to collect would be if you have manufactured M aircraft"}, {"start": 104.48, "end": 111.2, "text": " engines to collect the features X1 and X2 about how these M engines behave."}, {"start": 111.2, "end": 117.28, "text": " And probably most of them are just fine, they're normal engines rather than ones with a defect"}, {"start": 117.28, "end": 120.68, "text": " or a flaw in them."}, {"start": 120.68, "end": 128.84, "text": " And the anomaly detection problem is after the learning algorithm has seen these M examples"}, {"start": 128.84, "end": 133.64000000000001, "text": " of how aircraft engines typically behave in terms of how much heat they generate and how"}, {"start": 133.64000000000001, "end": 140.24, "text": " much they vibrate, if a brand new aircraft engine were to roll off the assembly line"}, {"start": 140.24, "end": 148.5, "text": " and if it had a new feature vector given by X test, we'd like to know does this engine"}, {"start": 148.5, "end": 153.12, "text": " look similar to ones that have been manufactured before?"}, {"start": 153.12, "end": 155.52, "text": " So is this probably okay?"}, {"start": 155.52, "end": 160.08, "text": " Or is there something really weird about this engine which might cause this performance"}, {"start": 160.08, "end": 166.0, "text": " to be suspect, meaning that maybe we should inspect it even more carefully before we let"}, {"start": 166.0, "end": 170.4, "text": " it get shipped out and be installed in an airplane and then hopefully nothing will go"}, {"start": 170.4, "end": 171.4, "text": " wrong with it."}, {"start": 171.4, "end": 174.76, "text": " Here's how an anomaly detection algorithm works."}, {"start": 174.76, "end": 182.8, "text": " Let me plot the examples X1 through XM over here via these crosses where each cross, each"}, {"start": 182.8, "end": 188.44, "text": " data point in this plot corresponds to a specific engine with a specific amount of heat and"}, {"start": 188.44, "end": 192.14, "text": " a specific amount of vibrations."}, {"start": 192.14, "end": 197.67999999999998, "text": " If this new aircraft engine X test rolls off the assembly line and if you were to plot"}, {"start": 197.67999999999998, "end": 205.88, "text": " these values of X1 and X2 and if it were here, you'd say, okay, that looks probably okay."}, {"start": 205.88, "end": 210.44, "text": " It looks very similar to other aircraft engines."}, {"start": 210.44, "end": 214.23999999999998, "text": " Maybe I don't need to worry about this one."}, {"start": 214.23999999999998, "end": 219.26, "text": " But if this new aircraft engine has a heat and vibration signature that is say all the"}, {"start": 219.26, "end": 225.23999999999998, "text": " way down here, then this data point down here looks very different than the ones we saw"}, {"start": 225.23999999999998, "end": 226.56, "text": " up on top."}, {"start": 226.56, "end": 231.48, "text": " And so we will probably say, boy, this looks like an anomaly."}, {"start": 231.48, "end": 234.23999999999998, "text": " This doesn't look like the examples I've seen before."}, {"start": 234.23999999999998, "end": 239.68, "text": " We better inspect this more carefully before we let this engine get installed on an airplane."}, {"start": 239.68, "end": 244.23999999999998, "text": " How can you have an algorithm address this problem?"}, {"start": 244.24, "end": 253.64000000000001, "text": " The most common way to carry out anomaly detection is through a technique called density estimation."}, {"start": 253.64000000000001, "end": 259.56, "text": " And what that means is when you're given your training set of these M examples, the first"}, {"start": 259.56, "end": 268.04, "text": " thing you do is build a model for the probability of X."}, {"start": 268.04, "end": 272.88, "text": " In other words, the learning algorithm will try to figure out what are the values of the"}, {"start": 272.88, "end": 279.2, "text": " features X1 and X2 that have high probability and what are the values that are less likely"}, {"start": 279.2, "end": 285.71999999999997, "text": " or have lower chance or lower probability of being seen in the data set."}, {"start": 285.71999999999997, "end": 292.15999999999997, "text": " In this example that we have here, I think it is quite likely to see examples in that"}, {"start": 292.15999999999997, "end": 293.68, "text": " little ellipse in the middle."}, {"start": 293.68, "end": 298.24, "text": " So that region in the middle would have high probability."}, {"start": 298.24, "end": 302.4, "text": " Maybe things in this ellipse have a little bit lower probability."}, {"start": 302.4, "end": 307.23999999999995, "text": " Things in this ellipse or this oval have even lower probability and things outside have"}, {"start": 307.23999999999995, "end": 311.44, "text": " even lower probability."}, {"start": 311.44, "end": 317.28, "text": " The details of how you decide from the training sets what regions are higher versus lower"}, {"start": 317.28, "end": 323.12, "text": " probability is something we'll see in the next few videos."}, {"start": 323.12, "end": 331.67999999999995, "text": " And having modeled or having learned to model for P of X when you are given the new test"}, {"start": 331.68, "end": 341.28000000000003, "text": " example X test, what you will do is then compute the probability of X test and if it is small"}, {"start": 341.28000000000003, "end": 348.28000000000003, "text": " or more precisely if it is less than some small number that I'm going to call epsilon."}, {"start": 348.28000000000003, "end": 353.86, "text": " This is a Greek alphabet epsilon, which you should think of as a small number, which means"}, {"start": 353.86, "end": 361.2, "text": " that P of X is very small or in other words, the specific value of X that you saw for a"}, {"start": 361.2, "end": 368.47999999999996, "text": " certain user was very unlikely relative to other users that you have seen."}, {"start": 368.47999999999996, "end": 374.52, "text": " But if P of X test is less than some small threshold or some small number epsilon, we"}, {"start": 374.52, "end": 379.28, "text": " will raise a flag to say that this could be an anomaly."}, {"start": 379.28, "end": 386.2, "text": " So for example, if X test was all the way down here, the probability of an example landing"}, {"start": 386.2, "end": 388.91999999999996, "text": " all the way out here is actually quite low."}, {"start": 388.92, "end": 393.68, "text": " And so hopefully P of X test for this value of X test will be less than epsilon."}, {"start": 393.68, "end": 397.8, "text": " And so we would flag this as an anomaly."}, {"start": 397.8, "end": 403.64000000000004, "text": " Whereas in contrast, if P of X test is not less than epsilon, if P of X says is greater"}, {"start": 403.64000000000004, "end": 410.48, "text": " than equal to epsilon, then we will say that it looks okay, this doesn't look like an anomaly."}, {"start": 410.48, "end": 416.0, "text": " And that corresponds to if you had an example in here, say, where our model P of X will"}, {"start": 416.0, "end": 420.8, "text": " say that examples near the middle here, they're actually quite high probability, it's a very"}, {"start": 420.8, "end": 427.46, "text": " high chance that the new airplane engine will have features close to these inner ellipses."}, {"start": 427.46, "end": 431.56, "text": " And so P of X test will be large for those examples and we'll say it's okay and it's"}, {"start": 431.56, "end": 433.88, "text": " not an anomaly."}, {"start": 433.88, "end": 437.4, "text": " Anomaly detection is used today in many applications."}, {"start": 437.4, "end": 443.6, "text": " It is frequently used in fraud detection, where, for example, if you are running a website"}, {"start": 443.6, "end": 451.40000000000003, "text": " with many different features, if you compute Xi to be the features of user eyes activities."}, {"start": 451.40000000000003, "end": 458.24, "text": " So for example, the features Xi might be the features of a particular user eyes activities."}, {"start": 458.24, "end": 463.48, "text": " And examples of features might include how often does this user log in?"}, {"start": 463.48, "end": 466.36, "text": " And how many web pages do they visit?"}, {"start": 466.36, "end": 469.12, "text": " How many transactions are they making?"}, {"start": 469.12, "end": 473.8, "text": " Or how many posts on the discussion forum are they making?"}, {"start": 473.8, "end": 476.0, "text": " What is their typing speed?"}, {"start": 476.0, "end": 480.36, "text": " How many characters per second do they seem able to type?"}, {"start": 480.36, "end": 486.0, "text": " With data like this, you can then model P of X from data to model what is the typical"}, {"start": 486.0, "end": 489.8, "text": " behavior of a given user."}, {"start": 489.8, "end": 494.84000000000003, "text": " In a common workflow of fraud detection, you wouldn't automatically turn off an account"}, {"start": 494.84, "end": 501.4, "text": " just because it seemed anomalous, but instead you may ask the security team to take a closer"}, {"start": 501.4, "end": 507.71999999999997, "text": " look or put in some additional security checks, such as ask the user to verify their identity"}, {"start": 507.71999999999997, "end": 513.0799999999999, "text": " with a cell phone number or ask them to pause a capture to prove that they're human and"}, {"start": 513.0799999999999, "end": 514.4, "text": " so on."}, {"start": 514.4, "end": 520.0799999999999, "text": " But algorithms like this are routinely used today to try to find unusual or maybe slightly"}, {"start": 520.08, "end": 526.24, "text": " suspicious activity so they can more carefully screen those accounts to make sure there isn't"}, {"start": 526.24, "end": 528.32, "text": " something fraudulent."}, {"start": 528.32, "end": 538.36, "text": " And this type of fraud detection is used both to find fake accounts and this type of algorithm"}, {"start": 538.36, "end": 545.96, "text": " is also used frequently to try to identify financial fraud, such as if there's a very"}, {"start": 545.96, "end": 552.2, "text": " unusual pattern of purchases, then that may be something well worth a security team taking"}, {"start": 552.2, "end": 554.6800000000001, "text": " a more careful look at."}, {"start": 554.6800000000001, "end": 558.0, "text": " Anomaly detection is also frequently used in manufacturing."}, {"start": 558.0, "end": 564.72, "text": " You saw an example on the previous slide with aircraft engine manufacturing, but many manufacturers"}, {"start": 564.72, "end": 571.24, "text": " in multiple continents in many, many factories will routinely use anomaly detection to see"}, {"start": 571.24, "end": 576.48, "text": " if whatever they just manufactured, anything from an airplane engine to a printed circuit"}, {"start": 576.48, "end": 582.2, "text": " board to a smartphone to a motor to many, many things to see if you've just manufactured"}, {"start": 582.2, "end": 588.5600000000001, "text": " a unit that somehow behaves strangely because that may indicate that there's something wrong"}, {"start": 588.5600000000001, "end": 593.36, "text": " with your airplane engine or printed circuit boards or what have you that might cause you"}, {"start": 593.36, "end": 598.88, "text": " to want to take a more careful look before you ship that object to a customer."}, {"start": 598.88, "end": 607.2, "text": " It's also used to monitor computers in clusters and in data centers where if XI are the features"}, {"start": 607.2, "end": 612.84, "text": " of a certain machine I, such as if the features captured the memory usage, the number of disk"}, {"start": 612.84, "end": 619.68, "text": " accesses per second, CPU load, features can also be ratios such as the ratio of CPU load"}, {"start": 619.68, "end": 622.52, "text": " to network traffic."}, {"start": 622.52, "end": 628.88, "text": " And if ever a specific computer behaves very differently than other computers, it might"}, {"start": 628.88, "end": 633.36, "text": " be worth taking a look at that computer to see if something is wrong with it, such as"}, {"start": 633.36, "end": 638.36, "text": " if it has had a hot disk failure or a network card failure or something's wrong with it,"}, {"start": 638.36, "end": 641.72, "text": " or if maybe it has been hacked into."}, {"start": 641.72, "end": 646.1999999999999, "text": " Anomaly detection is one of those algorithms that is very widely used even though you don't"}, {"start": 646.1999999999999, "end": 649.84, "text": " seem to hear people talk about it that much."}, {"start": 649.84, "end": 654.08, "text": " I remember the first time I worked on the commercial application of anomaly detection"}, {"start": 654.08, "end": 660.0400000000001, "text": " was when I was helping a telco company put in place anomaly detection to see when any"}, {"start": 660.0400000000001, "end": 664.96, "text": " one of the cell towers was behaving in an unusual way because that probably meant there"}, {"start": 664.96, "end": 669.4, "text": " was something wrong with the cell tower and so they want to get a technician to take a"}, {"start": 669.4, "end": 670.4, "text": " look."}, {"start": 670.4, "end": 674.9200000000001, "text": " So hopefully that helped more people get good cell phone coverage."}, {"start": 674.92, "end": 680.36, "text": " And I've also used anomaly detection to find fraudulent financial transactions."}, {"start": 680.36, "end": 685.5999999999999, "text": " And these days I often use it to help manufacturing companies find anomalous parts that they may"}, {"start": 685.5999999999999, "end": 689.12, "text": " have manufactured but should inspect more often."}, {"start": 689.12, "end": 693.68, "text": " So it is a very useful tool to have in your tool chest."}, {"start": 693.68, "end": 697.4, "text": " And in the next few videos, we'll talk about how you can build and get these algorithms"}, {"start": 697.4, "end": 699.64, "text": " to work for yourself."}, {"start": 699.64, "end": 706.68, "text": " In order to get anomalous detection algorithms to work, we'll need to use a Gaussian distribution"}, {"start": 706.68, "end": 709.28, "text": " to model the data, pfx."}, {"start": 709.28, "end": 730.3199999999999, "text": " So let's go on to the next video to talk about Gaussian distributions."}]