Hello and welcome to a module on scaling variables in Python. You may remember Z score scaling
from your textbook. This is what we will be doing today. First, let's load in some data and import our libraries.
We've imported these two before a couple of times. We're going to be using those to import our
data. We'll also be importing the statistics module so that we can use it down below to do some
basic statistics. We're importing mapplotlib.pyplot to do some visualizations
of our variables. And we're importing z-score from sci-py.stats to actually perform
our z-score distribution. This is some code we're pretty familiar with by now. So let's go ahead
and run it and we have all the variables that we'll be using today. Let's work
with dv. Let's make a histogram for dv, so we can visualize the shape of its distribution. Then we'll scale
the variable and then we'll take a look at the distribution afterward. The best case scenario to apply a z-score distribution
is when you have a distribution that is approximately normally distributed. This is the code for our histogram.
And we can see that dv is not quite normally distributed, but it's close enough for our purposes. Now let's
scale dv. So first to score it or to do
a z-score distribution of it. We are going to use the z-score function,
we'll apply it to dv in our data frame, and we will put this in a new variable called z-scores in our data
frame. Let's print the head so we can take a look. You'll notice after we printed them, we got
a new distribution of z-scores. As a result of transforming dv, when we transform a variable into a z-distribution,
the shape of the distribution remains the same. Let's take a look at a histogram of z-scores to see the shape of its distribution.
So this is the same histogram code as above, but we are using the z-scores variable instead of dv.
And you can see that the shape of this distribution is exactly the same as the one for
dv. It did not change. This will always be the case when performing a z-score
transformation of a distribution. However, z-score distributions are special in other ways. For example, let's
find the mean and standard deviation of our z-score distributions. We'll be using
statistics.mean and statistics.stdev standard deviation to find these numbers.
You'll see that our mean is zero and our standard deviation is one. A z-score distribution will always
have the mean of zero and a standard deviation of one. This is a good way to check your work to make sure that you haven't used the wrong
variable or something like that. All right, see you in the next video.