1. Home
  2. Explore
  3. Computer Science
  4. Probabilistic Classification

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Probabilistic Classification

Transcript

hello welcome to the section on probabilistic classification so so far we've talked about classifiers that given a record you return for us a prediction of which class the target variable lies in and now we want to talk in this section about an extension of that idea instead of having a classifier that simply predicts one value we want to think about a classifier that can make more general types of predictions so if we have a classifier that predicts one value we'll say call it v-hat that would be called a point classifier

or a point predictor it makes just one guess a classifier that produces more than a single guess might for example produce a list of guesses or an ordered list of guesses so it says here are the top three things so for example we've got a camera it's sitting on a car it's looking at all the things out on the street and it says well you know that could be a pedestrian or it could be a person riding a scooter or it could be a person who is riding a bicycle and

um those should be the the best guesses that the predictor makes it knows it's not a car but it can't determine exactly which one of those three things it is and it may rank those guesses this is most likely to be a uh pedestrian second choice of scooter third choice bicyclist um another possible output of a predictor or a classifier would be a probability distribution on possible targets so with probability 0.9 it's a pedestrian with probability of 0.05 somebody on a scooter

with probably 0.05 it's somebody on a bicycle and these are very common in fact probabilistic classification is probably the most common type of classifier that people use today um we also are of course quite familiar with the idea of probabilistic predictions from weather forecasts if you look at your weather app on your phone it will tell you the chance of rain today is 10 for example

so let's just consider a list classifier um so this is a classifier that produces an ordered list we might call these v top v second and v third as its best first second and third best uh guesses for what the true y should be what the true v should be and of course it may produce three guesses it may produce a top ten or it could be a variable number sometimes it will know with with this very strong certainty that it's a pedestrian and others it may have more uncertainty and so it produces two

or three or ten guesses and so some predictors do produce a variable number of guesses and some predictors always produce a fixed number um and so if we're trying to uh uh evaluate such a predictor well you know we'd be obviously happiest if the true v is the first choice of the predictor and a little bit less happy if it's the second choice and so on um one place you very often see a list predictions and a recommendation system so amazon has a predictor for example that looks at

your recent browsing history and looks at your recent purchases and tries to figure out what you're most likely to be interested in for your next purchase and show you those things and so it will figure out your top 10 list of things you're most likely to buy or most likely be interested in and show you those uh one way to to make a list classifier is to use unembedding that we've seen so far so for example we've used nearest neighbor unembedding to construct a classifier we have a prediction algorithm that produces a y-hat we have a bunch of different representatives embedded in rm psi 1 through side k and our predictor so far has always just returned for us the representative which is closest to y hat but it could equally well return for us the three closest representatives to y hat and

those would be the three um our best predictions and the closest one would be the top guess the second closest one would be the second guess and so on so we're going to spend some time in the next few sections talking about probabilistic methods and so we really need to start looking at probability in earnest so this is where the co-requisite in this class of knowing about probability is going to start to come into play we're not going to use a great deal of probability but we will

certainly use sum and so uh just a brief review right here of probability our probability distribution on our target set b it's it's a function uh for each element of our target set it gives us a real number and those real numbers have to be non-negative and add up to one and those are the probabilities associated with those elements of the target set b so p of v is the probability of the value v

um so for example if the target set is rain and shine we might have p of rain is 0.15 and p of shine is 0.85 if our predictor is predicting a probability that it will rain of 15 and the probability that it will not rain over 85 percent um if we have a probability distribution like this well we have numbered the entries in the target set v as v1 through v capital k okay categories or k classes well then we've also got uh pvi which is the probability of vi we can think about

as the ice entry in a probability distribution vector and so our probability distribution assigns a number to each of those k classes and so we've actually got k numbers which form give us a k dimensional vector and so in vector notation we might say that p is greater than or equal to zero and what that means is that that inequality should be interpreted element wise all of the entries p sub i of p as a vector are non-negative and the fact that the because this is a probability distribution

the entries have to sum up to one we can express that as one transpose p is equal to one where the one on the left there is the vector of all ones and so that's simply saying that the entries have to sum up to one so we can either think about our probability distributions as functions which mapped script v to the reals or we can think about them as k dimensional vectors both of those are convenient and we will make use of both choices of notation

a probabilistic classifier produces a probability distribution p hat on v given u so we have a classifier it takes in the input u and it returns for us a prediction and instead of that prediction being y hat um an embedded value of uh v or instead of it being v hat a prediction of which class the target variable lies in it produces as p hat and so the hat here indicates prediction and p hat is a probability distribution on v so it's a number associated with each of the different classes which

think which informs us how likely the classifier thinks each one of the different classes is to correspond to that particular u so we'll write this as p hat is g of u this is capital g so the capital g is used in the same way we used it before in that we use little g's to indicate a predictor that's taking input x and producing an output y and a big g to use to indicate a predictor that's that's unembedded that's composed with the embedding and unembedding operations

to take in an input u and produce an output v and here because we're dealing with probabilistic classification we're using capital g to indicate a predictor that takes in an input u and returns for us an output which is a probability distribution on v so when you read this you should keep in mind that we've got something a little tricky going on here g is a function and it's returning a distribution which is itself a function so we want a function that returns a function

we can call the function so we get p hat as g of u and then i can take p hat and evaluate it at a particular class v i so i can take p hat of v i and that would give us the probability that v is v i when the independent variable is u of course i could just write that all at once i could say g of u evaluated at v i which i'd write as g parenthesis u parentheses v i um and so here um of course this is totally fine we used functions returning concrete objects

such as numbers and vectors and matrices and here we've got a function that's returning a more abstract object which is a function and modern programming languages such as julia can happily do this we've seen this already when we pass around loss functions in julia and so here we're going to pass around probability distributions which are just functions

now in some sense the point classifier and probability class probabilistic classifiers are related to each other um we can say well for example a point classifier is a special case of a probabilistic classifier it's a probabilistic classifier that's saying the probability of a particular target is a hundred percent and the probability of all the other classes is zero so we could construct such a thing we'd have p hat of v being one if v is v hat and zero otherwise

and that would be a distribution that corresponds to a hundred percent certainty that the guess is v hat and zero percent certainty for the others zero percent probability of the others um and typically this is not what you want of course yes it's a probabilistic classifier you've taken a point classifier and constructed a probabilistic classifier but you haven't gained any more information out of this you can also go the other way you can if you've got a probabilistic classifier you can construct a point classifier so what you do is you say well my probabilistic classifier gave me a probability distribution p hat let me look uh out of all of the v's for the one that's most likely the one which maximizes p hat of v and uh that would be a way of translating a probabilistic prediction into a deterministic prediction into a very specific prediction which is actionable um so for example if you uh

have a predictor of the weather that's telling you the probability of rain at some point you have to make that actionable you have to decide am i going to carry an umbrella outside or not and so one way to do that would be to say is it more likely that it's going to rain or that it's going to shine that it's not going gonna rain and if it's more likely it's gonna rain then you carry your umbrella if it's more likely that it's gonna be sunny then you don't carry your umbrella and of course there are many ways of translating probability probability distributions into actions and this is just one way of doing it this particular method of translating a probabilistic prediction into a point prediction is called maximum likelihood we look for the class that's most likely that has highest probability we can also generate a list classifier from a probabilistic classifier simply by giving the value sorted by probability

there are several different ways of constructing a probabilistic classifier um all of the ones that we've seen so far for point classifiers can be extended so one can have a tree braced tree-based probabilistic classifier which is a decision tree has nodes labeled by feature and threshold value and the leaves contain distributions rather than point predictions you can have a nearest neighbor probabilistic classifier now the way that works is then if you have a

predicted if you have a query point x then you look for the k nearest neighbors 2x so the x sub i's in your data set which are the closest to x and then amongst those you look at the distribution of vi's that you have so if three of them are true and five of them are false then one would predict a probability of three-fifths for the corresponding target variable at the point x you can also do probabilistic classifiers which are based on linear predictors or based on neural network predictors and we will come to those shortly

so here we have a nearest neighbor classifier we've embedded the use in r2 to give us two dimensional x's given a query point we would look at the corresponding point in the plane and we would like to predict either red or blue for the corresponding class now the way we do it is here we look at the k nearest neighbors of our query point so here k has been chosen to be eight so for any given query point we look at the eight nearest neighbors let's look at an example

so we'll have a query point say right here and we'll draw a circle that contains eight neighbors which i would guess is something like that and then of those eight neighbors we will count how many of them are red and how many of them are blue and that will give us the prediction probability distribution the empirical distribution that we're seeing amongst the nearest eight neighbors and so here the colors in this plot are chosen accordingly so if the prediction is

uh blue 100 red zero percent then that's this color which is dark blue if the prediction is red hundred percent blue zero percent that's this orange color and of course the colors in between vary and really we're only predicting one probability here because once we've got a prediction probability for a blue the prediction probability for red is one minus the probability for blue

so now we want to consider well suppose we've got a probabilistic classifier or a list classifier how are we going to evaluate how good they are um and there are several different ways of doing this it's not quite as simple as it is for a point classifier if we're trying to judge a list classifier then we may look at the error rate as a function of the list rank so for example we look we have a test data say set we have a true value of v and we have our predictor is given us is giving us

uh v top the second and v third as our as its guesses and then we ask ourselves well you know how many of our of the how many points in the test set did the predictor get v top to be the actual true v and we might find that say 68 of the samples the v top was actually the true v and then we could say well how many of the test set points was the true value among the top two guesses and that might be has to be higher than 68 that might be 79 and amongst the top three guesses might be 85

and so if you want just one number you might decide well i'm really concerned that it'd be in the top three as long as it's in the top three i'm happy and uh in which case 85 percent might be a reasonable accuracy another way to do it might be to score the test data set so for every data point vi in the test data set we look at whether it was the top guess in which case we could be three points the second best guess in which it gets two points or the third best guess in which case it gets one point and then we add the scores over the entire test set and that's a measure of how good a job our list classifier did

now to judge a probabilistic classifier that's a reasonably subtle idea what do we have well we have a data set with using v's in it and then for each one of those data points we have a prediction p hat and so [Music] um so for each data point uh we have a true value the v and we have a prediction which is a probability distribution p hat over the set of possible v's and one way to say whether this is a good prediction or not would be to say let's look at the p hat that we got and see

whether it gives a high probability to the actual value that happened v so for example if we've got a prediction of rain and um we look back at historical data and we look at the predictions of rain based on the weather the day before and we look on whether it did rain or not well what we'd like to see is that on days when it rained the probability predicted for rain was large and on days when it didn't rain we'd like the probability predicted for rain to be small and if we see something like that we might say well that's a good predictor now we've got to meet this idea formal um the the most common way of formalizing this idea is to use what's called a log likelihood

so we have a data set u1 through un v1 through vn and we're going to have a prediction at each of those uis which will call p hat i it's the output of our predictor when we feed ui in and it's a probability distribution over the v's now if this were the true distribution of so if p hat i was the true probability distribution that the v i's that v i was generated according to then we could ask ourselves what is the probability of seeing that sequence v1 through vn

so if i is 1 the probability of getting the particular v that we got is given by evaluating p hat one at that particular v which is just v one similarly for v for i is equal to two we have to evaluate p hat two at v two and those give us the probabilities of getting those outcomes under that distribution if we make the probabilistic assumption that each of those data points is independent then the probability of getting the entire data set is the product of the individual probabilities

and so the probability of seeing that entire data set under those distributions p hat 1 through p hat n is simply the product from i is one to n of p hat i evaluated at v i so this is just a probability it says when you've got some distribution and what's the probability of getting that particular outcome and we can look at it as well just the probability of getting the outcome but we can also look at it another way and look at it as a measure of the distributions p hat 1 through p hat n and

when we do that instead of calling it the probability of getting v's we call it the likelihood of p hats being correct so the likelihood is simply another name for the probability but we call it the probability when we're looking at the probability of outcomes and we call it the likelihood when we're talking about the likelihood of probability distributions and so we'd like this quantity the probability or the likelihood to be large and this is a fundamental measure of how

well the predicted distribution matches the data just as in our main versus sun example if the probability of rain is large on days when it did rain that's a good predictor so we can compare two different classifiers by looking at their associated likelihoods which one has the largest probability value their largest likelihood the one with the largest likelihood we would consider the better classifier

now it's actually more convenient to work with log probabilities rather than purely probabilities now one reason for that is that the likelihood is the product and when we take the log we're going to get a sum uh there are some other reasons for that as well which we'll see um so what we work with is actually the negative log likelihood that's simply the negative log of the probability of getting v 1 through v n under our predicted distributions p hat 1 through p hat n

that's the negative log of the product of the p hat is evaluated to the v i's which is negative of the sum of the logs of the pi's of the eyes now the negative log likelihood is actually a positive quantity and we would like it to be small if the negative log is small that means the property probability itself is large one of the things about this is that this is if we just look at the log of the probabilities then that gets smaller the more data points we have

and so we need to normalize in some way and the way we do that is we look at the average negative log likelihood so that's here l minus 1 on n the sum from i is 1 to n of the log of p hat i of v i and this is a quantity that we can compare the effectiveness of different classifiers on different size data sets so now we have a a very nice performance metric which is this average negative log likelihood now just as we did when we were looking at loss functions when we're looking at

the square loss and the absolute loss we asked ourselves the question if we just looked at constant classifiers which would be the best constant classifier which minimizes the average value of those losses here we have a particular performance metric the average negative log likelihood we can ask ourselves the question which is the best constant predictor the best constant probabilistic classifier that minimizes that particular performance metric um so how is this going to work well

we're going to have a data set which is just v1 through vn a bunch of classes and we're going to look for a classifier that doesn't depend on the use so the user don't even exist all we're going to do is try to predict a distribution p hat um and that distribution p hat has to be a probability distribution on script v so what do we do we're going to choose script we're going to choose p hat to minimize the average negative log likelihood minus 1 on n the sum from i is 1 to n of the log of p hat of v i and we can choose any distribution p hat we want of course it has to be a probability distribution so it has to be non-negative at every point v and it has to sum to 1.

now it turns out that the optimal constant probabilistic classifier is very nice it's a very sensible quantity it's actually the empirical distribution of the data so the empirical distribution of the data we simply count up for each v in script v the fraction of the data which are equal to that particular class so it's simply the empirical distribution it's simply the the counts divided by the total number of data elements we have we'll call that q and then the p hat that minimizes the average negative log likelihood is q

and this is this is very nice this is simp this is just like when we talked about minimizing the square loss we found that the best constant predictor was the mean when we talked about minimizing the absolute loss we found the best constant predictor is the median now we're predicting probabilities and when we think about minimizing the average negative log likelihood the best constant predictor is the empirical distribution

now the negative log likelihood can be computed in a particular way so we have the negative log likelihood is minus 1 on n the sum from i is 1 to n of the log of p hat v i now each of the v i's lives in one of the classes now remember what the classes are we use this notation v is v 1 up to v k notice those are subscripts to indicate the classes we've got the data elements which are v superscript i from i to 1 up to n now if i compute this quantity the sum from i is one up to n of the log

of p hat of v i well i can split that sum up i can split it up into those eyes for which v i is say v one and then i can take the log of p hat of v i but all of those v i's are v one so i can just write that v one and then i can do the next category such that vi is v2 of the log of p-hat of v2 and so on up to k categories sum from our over i such that v i is v k of the log of p hat of v k so by splitting it up like this i just split it up into into uh categories notice that this in inside here

inside each of these sums the the terms don't depend on i so i've really got just a certain number of terms that are all the same and the number that i've got well it just the it's just the number of data points for which vi is v1 in this sum the number of data points which vi is v2 in this sum and so on so when i work out this sum and taking into account the factor of n well then the fraction of terms is q v j those are the those are that's the fraction of the terms in this sum for which v i

is equal to category v j and for those each one of those terms the quantity log p hat of v i is just log p hat of the corresponding class v j so i end up with the sum over the categories rather than the sum over the data points and i have that the average negative negative log likelihood is the sum over the categories of the empirical distribution of each of the categories times the log of the probability of that category this quantity is called the cross entropy of p hat relative to q

where p hat is the distribution that is being evaluated it's the distribution that's produced by the predictor and q is the underlying true empirical distribution of the v's we might write this as uh h oops

h of q p hat is equal to minus the sum from j is one up to k of q j log p hat j using our vector notation there's also a related quantity when p hat and q are the same distribution so we'll call them both p that's quantity is called h of p that's the sum over k of p k log p k negative p k log p k that's called the entropy of a probability distribution and both of these quantities the cross entropy and the entropy are very important mathematically very important practically

they're used very widely in coding theory and in information theory and in machine learning they're fundamental properties of probability distributions we don't need much of the theory of that here or in fact any of the theory of it here we just want to be able to determine which is the p hat that minimizes the average negative log likelihood another way to say it is if i've got a q which p hat minimizes h of q p hat because h of q p hat the cross entropy is the average negative log likelihood

there's a convenient quantity to use for this and that's called the callback libra divergence of q and p so we've got h of q p we'd like to minimize that over p but actually instead of looking at h of q p we're going to look at this thing called the callback libelo divergence dkl of qp and that's h of qp minus h of q minus the entropy of q this is convenient for the following reason we want to minimize h of q p over p but actually well h of q p is just d k l of q p plus h of q

if i'm minimizing this quantity over p well this additional term here doesn't matter because it doesn't depend on p just depends on q it's just a constant so we're just shifting the objective function in our optimization problem um this quantity the callback libra divergence uh has a very nice property that it's non-negative one way to think about it is that it's a measure of how similar to distributions probability distributions p and q are and when they are the same well then dqp is zero

because if we look back in our definition of entropy h of p is the cross entropy of p with itself and so when q is equal to p we clearly have that the callback libelou divergence of q and p is zero and when q and p are not equal well then the callback divergence is always non-negative all right this is uh easy to see if i just look at what the sum is dkl of qp is the sum over j of qj log pj on qj so here we're using our vector notation pj is simply a shorthand for p of v j

um so here i've used the fact that i've subtracted one log from another and that's simply the log of the ratio um now the log of pj over qj uh has a nice bound now the log of a quantity log this is one and then the logarithm looks like that and i can compare that with the function x minus 1 function x minus 1 is the tangent right there and so the log of x is less than or equal to x minus 1. this implies that this sum is less than or equal to is greater than or equal to this sum

and this sum we can expand as being equal to the sum over j of p j minus the sum over j of q j and both of those terms are equal to one and so because both p and q are distributions and so the overall sum adds up to zero hence the callback light blur divergence of p in q is greater than or equal to zero here we used uh the fact that q was non-zero when we did this division um uh but uh in fact this proof can be shown to hold even if some of the qs are equal to zero

and that tells us the best constant predictor the best constant predictor is the given by the p hat that minimizes the average negative log likelihood the average negative long likelihood is equal to the cross entropy and so we would like to minimize h of q p hat with respect to p hat we can make this term zero by setting p hat equal to q and of course this is just a constant term and so when p hat is q um that's the best constant predictor best constant predictor as a

probability distribution equal to the empirical distribution the resulting uh cross entropy is the entropy of q so let's summarize uh a point classifier makes a single guess of the given u whereas a probabilistic classifier guesses a probability distribution on the target set on the target set of classes v given the u and we judge a probabilistic classifier by its average log likelihood on test data you

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent