Welcome to a module on Performing Calculations in Python. Today we'll be discussing how to find a sum of squares in Python,
several different ways. First, let's load in some data. As usual, I'm importing our libraries at
the top that we'll be using and included is numpy as NP because we will be using numpy today to form some calculations.
This next bit we've seen before a couple of times now, but I will go over it. Filepath is set to the filepath
where classdataS20 is kept. pd.read_excel will read that filepath into a data frame
called DF, and then I'd like to print the head of DF so I can see what variables I'm working with.
We're pretty familiar with this data set by now. So let's talk about the formula we need to
find the sum of squares. It is the sum of all values for x minus xbar
raised to the power of 2. To do this, we first need to know our
x value. Then we need to find our xbar. Then we'll subtract xbar from
x. Then we'll raise it to the power of 2. And then we'll find the sum of all those values.
So we've got a couple of different steps here that we're doing. Let's find our x value first or decide
what our x value is. I'm going to say let's find the sum of squares for
DV in our class data set. Now that we know x, we need to find xbar. Let's use numpy
to find the mean because we're using other numpy functions. First find
the mean. So the mean is equal to np.mean. So you're calling
a function here. The mean function from np. And we want to perform that
mean function on our data frame with dv. So df open brackets
quotation dv end quotation end brackets and parentheses.
Okay. Now that we've got our mean, let's
find a sum of squares. So remember we said that our next step would be to subtract the mean from
all values of dv. So that's exactly what we're doing here. And we're going to save in an variable called variance. So all you have to
do to do this is say I'd like to take dv from this data frame and subtract
mean from it. Done. At this point, if you'd like to check your work, you can
do it by summing up all the values of variance. So we'll do that here and put it in a variable called
variance sum, which is equal to np. sum of variance. And here
variance we've named up here. So that's why we're using just variance and we don't have to refer to it as df['variance']
any other thing because it's not in the data frame. So we'll print variance sum.
Next, we'll square that variance. So variance squared
is equal to variance raised to the power of two.
And remember in our first video, we talked about how to raise things to the power of another number in Python.
This is a little reminder that this is how you do that. Use the double asterisk. And then last,
we sum up all those values of variance squared. So sum of squares -- and this one I'm calling one -- will be equal to
the sum of variance squared. Now we'll print it down
here. The little f tells it that I'd like to put a title in front of it and
then call sum of squares one as our printed
value. But you don't have to do that. You can also just print sum of
squares one or sum of squares. If that's what you named it. Okay,
so here we can see we've got what looks like an integer that's very close to --
not an integer but a float -- that's very close to one. But actually because of this E minus
12, this is actually scientific notation and this is negligibly close to zero.
So that is what you're expecting to see when you sum up x minus x bar. You want the sum of your
variance for x minus x bar to be very close to zero.
Next, we have our sum of squares, which is just over 66,347.
Always leave this number unrounded. So here's another way to do that in fewer
lines. First, we still need to find our mean,
so this line here would still be necessary.
We're going to use mean down here. We're saying our dv minus the mean in parentheses
raised to the power of 2, wrapped in more parentheses and np.sum all the way
on the outside of those parentheses. If you feel like that makes more sense
to you, then by all means use this method. It will return the same result
as the one we've got up
here. Okay. So using numpy, we could actually accomplish this all in one line. But you
would have to be pretty familiar with Python and pretty familiar with numpy, and pretty comfortable
with nesting parentheses to get this to work for you. By all means, if you would like to
do it all in one line, give it a go. And definitely try out all three methods and see which one works best for
you. And you can see here we get the same result as above.