• 7 hours ago
Learn how probability turns uncertainty into practical predictions, from estimating the chance of free shipping to assessing whether next month’s orders may rise or fall. This introductory statistics lesson explains probability on a scale from zero to one, then uses order data and simple Python analysis to make the ideas tangible.

Explore how historical results can reveal conditional probability: in this dataset, orders increased about 55% of the time overall, while the pattern differed after above- and below-average months. Follow the examples to understand data analysis, monthly order trends, and probability calculations—and use the shared code and dataset to practice statistics in Python.

Category

📚
Learning
Transcript
00:00What's going on everybody? Welcome back to another video. Today we are continuing our statistics series and today we're looking
00:06at probability. Now probability is just a way of measuring how likely something is to happen. Now this happens on
00:13a scale of 0 to 1, basically being 0% to 100%, but the numbers that we're looking at are
00:180 to 1, with 0 being it's impossible, it's not going to happen, or 1 being it is for sure
00:25going to happen.
00:26And of course, in between, it's either likely or unlikely, or there's a 50-50 chance. We don't know. Now,
00:33probability is super useful. It lets you make predictions about something that might look uncertain or you can't really know,
00:41but you're basing it in the actual numbers and data.
00:44Now let's get hands on and we're going to start really simple and then we're going to work up to
00:50kind of something more challenging or more difficult even towards the end of this lesson. And I really need you
00:55to stay with me because we're going to start really simple with a little bit of coding. But as we
01:00go further along, it's...
01:02not too much code. It's just going to take a long time to write out. So I'm going to be
01:05copying and pasting and just explaining the code because I wrote it for, you know, the full course that I
01:11have on Analyst Builder, but I don't want this to be like a 35 minute video. So I'm going to
01:15kind of keep it a little simpler. We'll start out with this data set though.
01:19Now, in this data set, this is a new one. We're working with orders. In these orders, we have an
01:24order date, and that's going to really be important later on, and I'll explain that. Then we also have the
01:29category, the number of items, the order value, shipping cost, and days to deliver. Now, what if we wanted to
01:37know, what's the probability that the next order that comes in is going to get free shipping? Okay, free shipping.
01:44Some of these shippings are $12. Some are completely free.
01:49We want to know, in this next order, what's the probability that person will have free shipping? We can actually
01:55write this really simply. And we're going to come down here. I'm going to say we want to look at
02:00the length of orders first off. And we can run this. So we have 15,000 orders. So that's really
02:07important. We have 15,000 orders. And let's actually come up here and we'll give this a variable name. So
02:13we'll call that our total. And next, we need to determine...
02:16how many are actually getting free shipping right now? So we're going to say that that is our orders, and
02:22we're looking at this column of free shipping. So we're going to say shipping underscore cost, and let's close this
02:29out. Let's say where it's equal to zero, and we'll do dot sum. So let's run this, and let's copy
02:37this free shipping just so we can see how many we have here. And of course, I'm just writing this.
02:43terribly. Don't mind me. We have 9,292 out of, and we'll look at the total, 15,000, right? So
02:53we have 15,000 total, and there's 9,292 that have free shipping. So all we have to do is
03:00the percentage of free shipping is equal to the free underscore shipping divided by the total. And let's go ahead
03:09and run this, and let's copy this down.
03:12And it looks like about 62% of our actual orders had free shipping. So what does that mean? That
03:20means that in general, when somebody orders something, that means that in general, when somebody orders something, there's about a
03:2662% chance or probability that that person is gonna get free shipping. And so on our next order, what's
03:33the probability you'll get free shipping? About 62%. Now, this is a very simplified version.
03:40probability. This is probably the simplest form. Let's take a look at a real use case and this is using
03:47this data right here. Now for what we're about to do I'm going to copy in the code and explain
03:52it instead of write it out and you'll see why it's just a lot of code. But I'm gonna but
03:56I'm gonna copy this over and explain it as we go and I'll get the first one going.
04:01In the statistics course, this was a whole separate lesson. And so I'm going to explain it as we go.
04:06So what we did is we took this orders data and we basically just grouped it on this order date.
04:13And we say, okay, based off of this order date, and this is based off the month, by the way,
04:18so we're doing this based off each month. So each month we have our order date, our orders per month,
04:24and then we included this month label. We don't technically need that, but we...
04:28added it as a string just in case, okay? So really what we're looking at is our order date and
04:34our orders. Now, what we want to do is we want to predict whether the next month is going to
04:42be higher or lower than the previous month. Now, there's a lot of ways you can actually go about that
04:48with this data. There's a lot of variables we could take into consideration. There's, you know, outside factors, like if
04:56there's sales going on.
04:57Is it a holiday or not a holiday? You know, if there's a sale around Christmas time, I'm sure it's
05:02gonna go up, right? But we're just taking it purely from a data perspective and we're not taking in outside
05:08information or things that could change it. Although, in the real world, we would maybe wanna do that. So what
05:14we're looking at is we're saying, okay, from this month, which is January, to February, it went down. And then
05:22from February to March, it went up and then down.
05:27and then up, and then down, and then up, and then down. And we are going to predict whether it's
05:33going to be up or down. Now let's bring in this next code. And what we're gonna do is we're
05:37gonna take a look at this. So right here we have monthly previous orders, and we're comparing these orders to
05:45the previous month. That's what this dot shift one does. It's just looking at this number versus this number. Then
05:52we're looking at whether they increased or not. So we're saying, is the monthly order
05:56greater than the previous order. We're also determining the exact amount of change, so we're just subtracting this number versus
06:04this number, so we can get our change, and that's gonna be for a later visualization that we'll pull up
06:09in just a little bit. And in the end, the months where the orders increased were six out of 11.
06:15And so the probability that the next month is greater than the current month
06:21is about 55 or 54.5%. Now you may think, oh, that's kind of a coin flip, right? It's around
06:2750%. I mean, it doesn't give us a lot of information and I kind of agree with you, but there
06:34is some other information that we can pull just from this column of orders. Let me copy over this data
06:40and let's take a look at this. Now, before we run this code, right up here, we did something called
06:46an average order and that's just taking the average amount of orders
06:51that we have. So we're getting this average line, and again, we'll visualize this in just a little bit. And
06:56then we also, in here, looked at these transactions, and we just dropped any that didn't have any data in
07:02it. That's all it is. So when we're looking at this, we're copying basically this entire data set, and we're
07:09saying it's going to be above average.
07:11if the previous order is greater than the average amount of orders. And then we're doing a count on this.
07:17So now we know how many were above average, how many were below average. So here we have different probabilities.
07:24Now these are conditional, okay? So this is conditional probability based off of different things. So when one of these
07:31orders, let's say this one right here, this is gonna be below our average. In fact, let's look at this
07:37average orders real quick.
07:39just so we can see it. It's 1,250. So this is gonna be below average. And so what we're
07:44looking at is, okay, if this is below average already, did it increase in the next month? So that's what
07:53we're doing right here. The probability it increased given the above or it increased below. So if it's below average
08:00right here, we know that every single time it increased in the next month. But,
08:08If in that month it was above average, then the probability that it increased again was only about 16.7%.
08:16So this is a very, very, very important piece of information with the exact same data that we were given
08:23before. With this one, we're just saying, oh, we just have about a 55% chance. But using the exact
08:29same data, we can see that if the most recent month or the previous month was below average, it's most
08:35likely going to increase in the next month.
08:37Even though this says 100% chance, it doesn't mean it is going to happen. It just means historically it
08:43has happened. So there's a high kind of probability that it will happen. With this one, we're confident that if
08:50it was an above average month, let's say 1326, what's the likelihood that this one is gonna be 1385 or
08:59some higher number? It's a pretty low chance because that was an above average month. So now, let me come
09:04over here. Let's visualize this.
09:07Let's go ahead and run this. So now we're just visualizing our data. January, February, March, April, May. And here's
09:14what we have. Green means it's above average. And then red means it's below average. Now if it is in
09:21the red, if it was below average, every time, 100% of the time in our data, it went up.
09:26So it went boom, there's up. If it's red, boom, it went up. Boom, it went up.
09:31But then with the green, oftentimes it comes back down towards the average because this is above average. And so
09:37these are two really great data points. Let's actually come over here and we're just going to visualize this even
09:42though you kind of know it already. We've already talked about it. So when we're looking at our data, we
09:49know that overall it's around 55%, but it's only 17% after an above average month and then 100%
09:56after a below average month. So if we come back up here,
10:01We have our data right here. It increased from, what is that, November to December. So now we're looking at
10:07our next month. What is the likelihood that it goes positive or higher than this month right here? It's almost
10:14100% chance. It can't ever be 100%. There's very few things in this world that are 100%. But it's
10:21going to be, hey, we're about...
10:23you know, as confident as we can be that we'll have an above average month. This is purely looking at
10:28the data. We aren't taking into account other external factors, but this is really good information to have. This is
10:35really useful and tangible information. And so this is not just probability. This is also conditional probability where we're taking
10:42in several different factors like the average.
10:46and previous months. So we have two separate conditions that we're using. We could include more conditions. And again, that's
10:53just conditional probability. Whether you have two conditions or 10 conditions, it's still the same thing. But this is actionable
11:00probability that we can then go to stakeholders. We can go to our boss and say, hey, here's how it's
11:05been going for the past several months. Here's our probability that we're going to have a better month than the
11:10previous one.
11:10I think that is super cool. If you didn't already, you can follow along with this code with the link
11:16down below because it has the GitHub repo in there. You can get this exact file and run this code
11:21yourself, change the conditions, mess around with it because it's really useful and valuable to get your hands on this
11:26code.
11:27With that being said, I hope you enjoyed this lesson. If you haven't already, I have a full statistics course
11:32on analystbuilder.com. I will leave a link down in the description where I go a lot more in depth
11:36and I cover a lot more things even within just probability, but also other topics as well. Be sure to
11:42like and subscribe and I will see you in the next video.

Recommended