- [Instructor] In the preceding lesson's Intro
to Data Science section, we begin talking a little
bit about basic descriptive statistics,
and I mentioned the concept of getting
to know your data.
So a lot of the basics descriptive statistics
are for that particular purpose.
And another way to kind of get to know
the data that you're working with is with measures
of central tendency.
Specifically, the mean, the median, and the mode.
A measure of central tendency is some sort of individual
value representing a central value
in your set of values.
Some way to think about the center point,
or something that's in some way typical
of the values in your data set.
And as we work through these videos
you're going to work with much larger data sets
and you'll want to kind of explore the data
and get a sense of the data before you actually
work with it, so we'll starting talking
about that in some of the later videos.
Now the three measures of central tendency
that we're going to take a look at in this video
are the the mean, which is simply
the average of a set of values.
The median, which is the middle value
in a set of values when you've already arranged
those values into ascending order.
And then finally, we're going to talk about mode.
And the mode is simply the most frequently occurring value.
Now there are some problems with mode,
in that it could be that you have exactly
the same number of each value in which case
there is no mode, or you could have multiple values
that have the most frequently occurring number
of instances, and in those cases the libraries
that you use to calculate the mode,
will often simply give you an exception
at execution time.
So let me switch over to my Terminal window
here and let's start-up a new ipython session.
And for the purpose of this mean, median, and mode
discussion, let's go ahead and create a list of grades,
and as you saw earlier in this lesson,
a list is simply a square bracket delimited
set of values.
For this purpose we're going to use the five values
85, 93, 45, 89, and 85.
So we've got a five element list.
Now you'll notice by the way that one
of those five elements is duplicated,
85 appears twice in the list.
So, we've got this grades array
and before we get into pre-defined libraries
that enable you to do things like mean, median, and mode,
let's go ahead and do the mean on our own
using some built-in functions that are part
of Python itself.
So I think I mentioned this earlier in this lesson,
there's are sum function that can be used
to total up the values in a sequence of values,
and there's also a function that helps you determine
the length of a sequence.
And if I know the total number of grades
and I know the total of those grades,
I can use those values to calculate the average
grade, which is the the mean grade.
So let's do that, we're gonna use the sum function.
And the sum function is interesting because you can
simply hand it an entire list of values
no matter many elements are in it, and it will
give you back the total of the items
in that list.
And I want to divide that by the number
of elements in the list, and it turns out
that there is a len for length function
built-in to Python that can give you
the total number of elements in the sequence
you pass it as an argument.
So in this case, the sequence is the grades list
that we defined up above.
So sum is going to calculate the total
of these five values, and this is why
I have not been declaring variables named
sum all along.
I've been calling them total.
And then separately the len function
will look at the list called grades,
and every list knows how many elements it has,
and the len function simply accesses
that piece of data and gives it back.
So len with grades is an argument is going to simply
give me the value five.
And when I press enter we can see that the mean
grade, or the average grade based on the numbers
up above, is 79.4.
Now this is actually a demonstration
of a little bit of functional style programming in Python.
These two functions are examples of reductions.
They look at a collection of values and give you back
a single value that represents that collection
in some way.
In the first case, sum gives you the total
of the items in the collection, and in the second case
len just gives you the number of items that count,
if you will, of items in the collection.
And count was one of the descriptive statistics
that we mentioned in the previous lessons
Intro to Data Science section.
So len is basically the count statistic.
Now of course, one thing that's nice
about using these pre-defined functions
is we don't have to define our own looping structure here,
and by not defining our own looping structure
we're avoiding the common errors associated
with looping where we might do something wrong,
in terms of how we implement the loop
in the first place.
Here we're using what's known as internal iteration.
The sum function already knows how to walk
it's way through a sequence of numbers
and calculate their total, we don't have to say
how to do that, we just have to tell the function
what we want.
We want the total of all those grades,
and it gives us back a result.
So a lot of functional style programming
in Python is done by delegating to functions
built-in to the language, and delegating
to functions or methods that are part
of the standard library.
Now along those lines I want introduce
the statistics library, which is one of many
Python standard library modules that's available
to you in your Python installation.
And you can access it's capabilities
simply by importing the statistics module.
Now previously when I did an import,
I used the from module name import piece of the module
format, so from import.
Here I'm simply saying import the entire statistics module.
Now when you do that, in order to access it's contents,
you must use the name of the module
followed by a dot, and then the name
of whatever you want to use within that module.
And it turns this module, among it's capabilities,
has functions named mean, median, and mode
that we can use to produce the descriptives
statistics we talked about at the beginning
of this video.
So if I go ahead and start to type statistics
and I hit tab, you can see there's
a couple of things that start with stati,
so I'm gonna select statistics,
and then I'm gonna type a dot.
And just to show you there's a bunch of stuff
to find in the statistics module,
watch what happens when I press the tab key here,
it starts displaying a big list of all the different
things that are part of the statistics module,
and among them you can see here mean, median, and mode,
and a lot of other things as well.
Some of which we'll discuss in later examples.
So let's say we wanna use the mean function,
and again, like the sum function
and the len function up above, mean knows
how to walk it's way through a collection of items
you simply have to tell it what you want
to calculate the average of, in this case
the list of grades, and when you execute it, it will
go ahead and figure out not only how
to total up those grades, but how many grades
there were and produce correct average,
or mean of those grades in this case.
Let's go ahead and recall that and we'll change
this to median.
Now you may recall that the median
depends on the grades being in sorted order
to figure out the middle element.
Well if you look at the grades up above here,
they are not in sorted order.
So the median function takes care of that for you.
Then it goes and figures out the middle element,
which in our case because we have five elements
will be whatever appears as the middle position,
it's an odd number of elements so we'll
always have one middle.
If there were an even number of elements
the two middle elements would be averaged by default.
So let's go ahead and get the median,
and here we can see that the median is 85.
So if we were to reorder these, we'd have
45, 85, 85, 89, and 93, so the second of the 85 values
would be in the middle position,
and that's why got a median of 85.
And similarly, let's go ahead and do the mode.
And you can see in this case, excuse me,
that the mode also is 85 because there are two 85s
and only one each of the other three values
in our original list.
So those are the basic descriptive statistics,
mean, median, and mode.
And by the way as long as we talked
about the fact that the median function
is sorting the data for you, there is actually
as you might expect in Python,
a way to sort the data built into the language,
via the function called Sorted.
So let's go ahead and try that.
We'll do sorted with grades as an argument.
And what this is going to do, it won't modify
the grades list, what it will do is make a copy
of the list, but give it back
to us in sorted, ascending order.
So you can see this is the set of values sorted,
and by the way now we can easily see
what the median is which is the middle element
in the sorted set of lists.
The sorted set of values excuse me.
Oh and by the way, just one last thing
before I forget, if you go in evaluate grades
you can see that the original list
was left unchanged by the use of sorted
back in SnipIt seven.