Hello and welcome to a video on histograms. We want to use a histogram when we'd like to see the shape of our distribution.
Histogram show frequency data, which means they show how many of a certain type of data we have in set ranges called bins.
Let's start by loading in some data. So first we'll load our libraries. We have pandas as PD and from Google.colab
we're importing drive, and these are the two that we'll use to actually set our file path in import
our Excel file as a data frame. I'm using NumPy as NP to generate some
random numbers below in an example histogram, but you won't use that in your homework more than likely for your
histogram at least. We're also importing Matplotlib.pyplot as PLT and this is really
important because it's what we're going to use to generate our histogram. Let's go ahead
and run this block of code and get our variables all set in place. Now we have IV1, IV2 and DV available
to us. Now what if we would like to make a histogram? Let's see what one looks like
with some simple example data. Then we'll make a histogram for IV1. Remember histogram show
frequency data. So this section here is just to generate some random numbers. Let's
actually set this equal to 1,000. Next
we have PLT.Hist and that is the function that you'll use to create histograms.
We'll put data where data is supposed to go. Set our number of bins equal to 30. Set the color to sky
skyblue and the edge color to black. Next we'll add titles and labels. We're deciding
to show the grid with this plot but we could set this to false if we wanted to. And we'll display the plot with PLT.show.
Let's go ahead and run this code block. And you can see now we have
a histogram of random data. What's interesting about generating random numbers
like this is that as you generate more and more numbers, your histogram will become more and more normally
distributed. So if I were to set this to 250,
you can see that it's a little less normally distributed.
If I were to set this to 100, we can see
it's even less normally distributed. If we were to set this to 2000,
now we can see it's very normally distributed. And that is just the case when you generate random numbers.
This is part of something called the central limit theorem, which is something that you'll study very briefly in class.
But it's really cool to see an actual visual example of it working. So if you're curious
about whether you can use a color other in the sky blue, you can. You can check out the colors available on this website
or you can just Google what colors can I use in Python and it will come up with several different websites that will
show you examples of colors you can use. I just like this one because it lists both
the simple colors you can use and also the more involved colors you can do things like coral
or chocolate or green yellow or whatever speaks to you.
Okay now let's take a look at some real data using IV1. So we're going to set data equal to df IV1 this
time. You don't have to do it this way. We've got data right here. Which is being
called by data above. We could actually just set this equal to df['IV1'] right here. But for
the sake of consistency with the code above we're going to set that equal to data.
the bins equal to 30. The color is sky blue and the edge color is black. Next we'll set
our title, x-label, y-label, grid equal to true, and display the plot.
Let's go. Alright, that's pretty different. We can see in the first example
for a histogram that the data is very normally distributed, whereas in this second example it's more positively skewed. We've
got some skewness to our data this time. Interesting. This tells us something about our variable that we did not
know previously and may affect how we perform statistical analysis on it. It's always
important to visualize your data before you actually do any kind of analysis. Now, what if we want our histogram to look
a little different? We could set our bins equal to 20 and
see how that affects the overall shape. I think I preferred
30 because we
can see that we have a drop off here now. But that's just a matter of personal preference.
So long as you have more than 10 or so bins, and you have fewer bins than you actually
have in your distribution. So for example, we have a 170 data points in our data
frame that we have loaded in. We don't want 170 bins. I'll show you what that looks like
because then we're not actually binning our data. It's just showing each and every single value
that we have. And this doesn't really look like a shape that we can use.
Whereas if we set it to something more like between 30 and 50, suddenly we
can see the shape of our data much more clearly. So just be careful when you're setting your bins that you're
doing enough, but not too many. And if you're confused about whether or not you've got too many or enough reach out to your instructor
or you know talk to another student, talk to a teacher and see if you can get some feedback. All right,
that's it. Everybody have a great day.