1. Home
  2. Explore
  3. Computer Science
  4. Data Science Basics with Python

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Data Science Basics with Python

Transcript

can you learn data science even if you don't know anything about data science at all or even python in just an hour you can I'm going to teach you the basics of data science and python along the way and we're going to break down the concepts so that they're very easy to understand and you will actually for the first time in your life understand and use slope intercept form let's go so first off the tools that we're going to use today are visual studio code and something called Anaconda Anaconda is a tool that you can install that will make setting up your data science environment a lot easier we have a video to show you how to do that in just a few minutes you can pause take a look at that video and then come right back it's not a complicated process so to get started let's first create a folder where we'll put all of our code so I'm going to create a folder here and let's just call it explore data and then I'm going to open this in Visual Studio

code and you can do that by just dragging and dropping the folder onto Visual Studio code now we have our empty folder open and we want to write some code now data scientists use a lot of tools but one of the ones they use the most often is something called Jupiter notebooks so if we look in our extensions here we can install jupyter notebooks the other item that we can install if we want to probably better even is just install the python extension for visual studio code which gives you intellisense and linting and also jupyter notebooks I already have this installed so let's go ahead and create a new Jupiter notebook so we'll do the new file and call it student data we're going to be working with some student data today exploring students who passed and failed a class and trying to figure out what happened there let's go ahead and create this notebook here and in a notebook we have these things called cells and we can write code in the cells and execute them we

can also use something called markdown and markdown just lets us annotate student data right this notebook analyzes uh pass fail data for a class okay and then when we hit the little check mark here it just adds that text to the notebook all right so let's go ahead and add a new cell and put some code below so first off we just need some data to work with Okay so let's just bring in a python list which is what we have here and we're going to set it equal to the

data variable and that variable data now exists but how do we see it well there's a variable Explorer here in Visual Studio code and if we open that and scroll down a little bit here's our list here it has 22 items in it another thing that we can do is that we can just print out from the cell what's in the cell right so we need to say print data and this will print that out below the cell here so these are all of the items that are in the list now lists are great for working with data in

Python but they're not so great for working with numerical values and a lot of data Sciences statistics and math and so there's a package called numpy that actually has an array type that's better for this so let's add a new cell here and we're going to import numpy as in p okay and then let's create a new variable called grades and then we're going to set that equal to numpy and we're going to create a new array and we're just going to pass in that data object that we have from the first cell

which will give us an array and then let's just print out that grades object here okay so these two objects look the same what's the difference well the most obvious difference is that there's commas here but not here but the real difference is that the array with this here is much better for doing mathematical operations let me give you an example so up here if we take this data list object and we multiply it by two what do you think will happen let's run and find out

it actually doubles the number of objects in the array right so here's the first set okay and then here's the second it literally took the array multiplied it by two and gave you twice the objects in the array okay so what happens when we multiply this grades object by two in this case it takes the array and it multiplies each item in the array by two so arrays and numpy are much better for working with numbers for doing mathematical operations on those numbers all right so let's go ahead and keep on working with this array object here now one of the things that we can do is we can take a look at the shape of this array so let's do that so we can see that there are 22 items in this array another way that we could see this is if we go back to the variable Explorer here here's our grades array we can open that up and take a look at it and you can see here the same thing 22 items in the array and when the variable Explorer

launches here we can see all of those items here now notice that each one of these items has an index and this is added for us automatically an arrays start at zero okay so the first item is always in the zero position and then the second item is actually in the first position when you first get started with programming it's a little tough to wrap your head around but you'll get the hang of it very quickly so knowing that let's look at how we can get items out of this array so for instance if we wanted the first item in the array because arrays start at zero we would put zero and this would give us the very first item that's this item here okay now if we wanted to get the 47 that would be 0 1 2 right so if we change this to 2 we should get 47 and if we wanted this 3 that should be zero one two three four five this should give us the three okay so what else can we do with arrays well we can do mathematical operations on the whole array so for instance if we wanted to

figure out what the mean score was okay so the average test score here we could do a mean on the array and we see that it's a 49 must have been a really hard test so what we want to do here is determine why the scores are so low so to do that we need to bring in some additional data into this array and what we want to do is bring in the amount of hours that each student studied along with their grade so let's go ahead and add in this study hours a list item here

and then what we want to do is just say we have a student data new object called student data and we're going to set that equal to the numpy array okay and then inside of that array we want to have two objects we want to have the study hours which is the number of hours each student studied and the grades okay and what this is going to do is create an array with two arrays inside of it let's go ahead and run this so let's take a look at this in our variable Explorer here so if you pull it up you can see we now have two arrays inside and each array has 22 items and if we were to pull that up here in the variable Explorer here's the array one which is the study hours and here's the second array which is the grades now when you're working with an array that has multiple arrays inside of it how do you get a single item from one of those arrays well let's take a look at our student data here and if we pull back the first item the item in the zero

place we're going to get the first array and if we want the first item from the first array can you guess what we need here another zero so we're gonna get the first array first item from first array so it should be ten so let's run that right if we wanted the first item from the second array that would be one zero okay all right so let's do some analysis here so let's go ahead and do a couple of things first we want to get the average study time so let's go ahead and create a variable

that's the average study and let's look at the student data right and the study hours was in the zero position the first array so we're going to get the mean of that array of the average and then let's get the average grade okay so that would be student data same thing here and actually in vs code we can just copy this line down with command or alt shift down it gives us the line over and it just makes it easier for us to code here so we can say average grade and then we'll just change this to a one and then let's go ahead and print out what this says so in Python that's a print statement and you can use this formatted string so F and then a string and we can say average study time the colon and then we use these little brackets and we can put the variable right in there so average study and then on another line which is backslash n we'll put the average grade same thing average grade and you can see Visual Studio code's helping us

out here this character here is called a carriage return line break just puts this text on a different line uh whoops okay let's run this and the average study time was 10 hours and the average grades of 49. how'd you like to study for 10 hours and still get a 49. I'd like to know what was on this test when we're dealing with multiple arrays inside of an array it can start to get kind of confusing right just the idea of having a raise inside of an array starts to get tough and so there's a package called pandas that will allow us to work with this data in a table which is going to be something that looks a lot more familiar so let's do that so let's add a new cell here and we're going to import a package called pandas now if you're running this and you get an error saying that the package couldn't be found what you can do here in your terminal if you have Anaconda like I do is that you can just issue a conda install command and then install

the package that you're missing so in this case we could run conda install pandas and that will install pandas so if you run into any of those missing package errors give that a shot let's go ahead and make our data set a little bit more complicated so to do that what we want to do is create this thing called a data frame in pandas a data frame you can think of as a table and we're adding some data here so we're actually putting the name of the student so this is new this is new data and then we have the student data right which is our study hours the first array from student data study hours and then the second array from student a to the grade we're putting in grade right the difference here is we're actually naming these things now so we actually know what they are and we can go ahead and look at this thing which is called a data frame and see its structure so we can print it out here from the cell and you see it just looks more like a table

we're still working with multiple arrays this is a structure that probably just makes more sense to your brain this is called a data frame it's provided by pandas the other way that we could look at this is if we come to the variable Explorer here and we go down and look for our DF students which is our data frame you can see there's 22 elements and it's got three arrays inside of it right but when we open it up it looks more like a table which is a data structure we understand kind of like a spreadsheet all right so how do we work with data frames well let's take a look at how we can get an item out of a data frame so in a data frame let's say we wanted to get the first item Now with an array we would just use zero right so we wanted the first item from the first array it would be zero and one if we run this on the data frame though we get an error because data frames don't work that way what we need to do instead is we need to use the Loc method and then we can pass in and say

give us the first item here and the first item is it's really a row right so it's Dan Dan's study hours in dance grade it's that first row and if we were to go back and look at the data frame right here so if we wanted for example Vicky in the fifth position here we could go ahead and just pass in a five and that will give us actually uh Vicky who only studied for an hour and got a three ouch now let's say that we only wanted to know the grade from Vicky right so we want the item in the fifth position but we only want the grade so we can actually pass in here as the second parameter the column that we want back and because the data frame knows about the column names we can just tell it the grade column and if we run this we just get back the 3.0 and if we change this to name we should get back just Vicky so there's a lot of other ways to get stuff out of data frames and searching for data so one of the ways is to use filter

Expressions so we can take this data frame of students and we can say for the name column give me the data where the name equals Vicky that's what this expression says here so let's run this and we get Vicky and her study hours and terrible terrible grade now another thing that we could do here instead of doing it this way is we can use something called the query method so we can write query here and then we can actually write a query which is a string but it is essentially the same thing we just say where name equals Vicky but Vicky has to be in quotes because Vicky is a string data type so if we run this we should get back exactly what we had before but we don't because I left out an equals the double equals is important and now we should get back Vicky there we go all right another thing that we can do here is if we go back to the way we were doing it before where we're saying DF students we can actually just hit the dot here and then the data frame

knows about the columns that it has so it knows that there's a grade column here right or it knows that there's a name column and then we just do equals and say Vicky and run this and we'll get back Vicky and this is the method that we'll be using for sort of the rest of this video to access data in a data frame all right so those are the fundamentals of a data frame sort of back up and take a look at a more real world scenario something you might actually do so it's common in data science to import data from some sort of an external Source in this case we're pulling in a CSV file so we're downloading a CSV and we're loading that CSV into a panda's data frame so we're reading in the file the delimiter is a comma and we're inferring the column headers let's just run this so that we can see how it works so if we run this here and then look at our file explorer you'll see that the CSV file actually gets downloaded right and here's the CSV file here's the name the

study hours and the grade it's kind of like a table in comma separated format so what we have here and let's go ahead and clear the cell output to make this a little less messy and if we look at this data frame again this DF students it's going to look exactly like it did before only this time the data has been loaded from this CSV file instead of us just loading static data because usually data scientists are working with lots and lots and lots of rows thousands of rows but this data is a little bit different and the difference is that it's a little bit more real world and one of the ways that it's real world is that there are some null values somewhere in here okay so one of the first things that data scientists have to do is they have to clean up the data so that they can actually work with it so in this case to find any missing values we can say DF students and then just look for is null right so if we run this it will tell us

whether or not the value in each column is null or not null so in this case false it's not null in any of these places but this isn't super helpful for us there is a better way for us to see exactly which values are null so first of all we can hear and just say sum and just get a sum total of how many uh values we have that are null in each column so there's one value that's null in study hours and two values that are null in the grade column so the next question is then well what do we do

about that well there's a couple different things that you can do one of the things that we could do here is we could go ahead and replace anything any missing value with an average value right so for missing a grade for student one of the things we could do is just replace that with the average of all the other scores since we don't have a score for them so what we can do here is we can fix just the study hours column to start with so let's go with DF students and then study hours so we're going to set that equal to and then we're going to fix the missing values inside of that column so we're going to say DF students dot study hours dot fill in a and basically we're telling it we're going to fill in all of the missing values and then in the parentheses we need to tell it what we're going to fill it with so we're going to fill it with the study hours and we're just going to calculate the mean of all the study hours and we're going to fill any missing study

hours values in that data frame with the average all right so if we go back and run this we should see that study hours now shows as having zero null values right because we filled in the one missing value with the average the problem with that is that it kind of makes your data not correct okay and the reason for that is because you don't know if the student got the average they could have gotten less than Vicki right and pulled the whole average way down and so sometimes it's better to just delete any null values in a data set which is commonly what a data scientist will do so let's go ahead and do that so we're going to set our whole data frame here and we're going to set it equal to DF students and then we're just going to drop we'll use the drop in a to drop any rows that have null values so to do that we need to tell it which axis we're looking at rows our axis zero columns are axis one right so if we set axis one it would drop

columns we don't want to drop columns we want to drop rows so we say axis zero so if we do that and we run this and then we go back and look now we'll see that we don't have any null values right we've just dropped the rows that had null values in them all right so let's go back to where we were before when we were working with our arrays when we calculated the mean study time and the mean grade and we just printed that out this is that same thing just with a data frame so the average study hour is 10 and a half average grade 49.18 so let's go ahead and get all of the students who studied for more than the mean amount of study time so to do that we'll take our data frame of students and we'll say give us everywhere where the study hours was greater than the mean study time okay let's go ahead and run this and now we get everybody who studied for more than the mean amount of time which I believe was 10.52 so this is everything that's more than 10.52 so it

would be interesting to know here for these people who studied more than the average what was their average grade well since this actually returns another data frame as you can see here we can just say for this grade column give us the mean right what is the mean grade for everyone who studied more than the average amount of hours if we run this we see 66.7 if we assume that a 60 is passing and the mean is 66.7 it would be nice if we could go ahead and flag in the table who passed and who failed so we want to add a column to this data frame which just says whether or not someone passed so to do that let's create a variable called past it's going to be our new column and then we want to use this thing called series on PD and you see the little yellow squiggly it's because we haven't imported pandas we did but then we deleted that cell so let's go ahead and put it back here

there we go and in that series what we want to do is just say from the data frame here with the grade was the grade greater than or equal to 60 right this return to true if it was and a false if it wasn't and it puts those trues and falses in the past variable and then we want to take that data frame and we want to concatenate here this past column onto the rest of the data frame adding a new column so we're going to say we want to take the students here and we want the past

column and then we can take this opportunity to rename it and just make it say passed here with a capital letter and then finally we need to tell it what axis are we trying to append a row are we trying to append a column remember rows are axis zero columns are axis one so we'll say axis equals one so this should append a new column to our data frame so let's run this and then let's go ahead and come back to our data frame here and refresh and look we have a new column with a false or true about

whether or not someone passed or failed the test now because data frames are designed for tabular data or data that's in tables we can perform all sorts of operations on it for instance we could put students into two groups those who failed and those who passed and look at the data that way and that's called a group I so let's do that so in our data frame here we're just say Group by and then what do we want to group by well we want to group by the students and we have that new past

column that we added very handy and then we want to get the count of how many passed or failed in each group so if we run this it tells us who passed how many and how many failed but it does it in the table format which is kind of hard to look at so if instead we just take the name right if we Group by just one dimension the name and we run that then we'll actually get just one set of numbers here 15 people failed and seven past crazy hard test now what if we wanted to know for all of

these students what was the average study hours in grade for those who passed and those who failed to do that we can just print out our data frame here of students and use the group by and we want to group by that past column and then we want to know though from that we want to know the mean of the average study hours and grade or the average study hours on grade so we could look at the grade and then say mean like this but this will only give us the mean if we want to get the study hours

and the grade and the mean of both of those so to do that we want to take our data frame of students we want to group by and we want to group by whether or not those students passed but we want the mean of both the study hours and the grade hmm well because we know that this is actually data frame itself when we group it we could say grade and then give us the mean and if we run this it actually will give us that but we if we want the average study hours and the grade how do we do that we can actually do both of those at the same time using this array syntax here and saying give us the study hours and give us the grade and calculate the mean of both of those so if we run this

you can see that we now get the average study hours for those who failed eight and those who passed 14 and the same with the grade so we can do both of those things at the same time using that syntax so the last thing I want to show you here is that data frames are really versatile and you can do a lot with them uh including like sorting the data and descending your ascending order now these data frames that we've been looking at they're really versatile but one of the things you might have noticed is that when you alter a data frame you have to assign the value back to the original data frame if you make a change to the data frame and you don't assign it back to itself it doesn't actually affect or change the data frame so what we can do is come in and say our data frame and then we set it equal to itself we're going to sort it let's say we sort it let's say we want to sort it by grade and we want to sort it just ascending by

grade so let's go ahead and run this and then let's come back to our data viewer and refresh and we should see yep Vicky and her terrible grade there at the bottom sorted by grade if we wanted to sort by name or study hours let's say study hours and then if we wanted to do it in reverse right we wanted to say highest to lowest highest first then we would say ascending is equal to false and if we run that come back to our data frame we should see study hours the most of the top descending here

and we do descending something that data scientists do a lot is work with charts and graphs and this is because when you're working and trying to identify complex patterns in data it's easy to see if you can look at it it's hard to see if you have to examine the numbers themselves so charts and graphs help the data scientists do that and we're going to take a look at how they do that now so the first thing we're going to do is we're going to start with the data set which is exactly the data set that we were exploring before with student data we'll go ahead and run this and this will pull in the grades CSV file which we already have but it'll get pulled in again because we've made additional requests for it see right there grade csv.1 and then here we're pulling in the grades putting that in a data frame like we talked about adding that passes column whether or not they passed or failed and then just printing out the data frame here so it's exactly what we

had before when we were exploring the data and learning about data frames by the way when I jump over to these CSV files here you might notice that they're different colors and this is powered by an extension called rainbow CSV you might want to check that out right here so one of the cool things about Visual Studio code is extensions and there are tons of extensions for those of you doing data science working with data one of those being rainbow CSC all right so we've got our student data now we want to make a chart or a graph so to do that we're going to use a library called matplotlib and again if you don't have this installed you can just in your terminal run conda install matplotlib just like this and that will install it so if you run this and you get an error try installing matplotlib in either python using pip or if you're using Anaconda like I am with conda install so first let's pull in matplotlib here and then we're going to import Pi plot which is the plot plots

are the charts as PLT and that's what we'll call it here the variable PLT now what we want to do is create a bar chart here to look at this student data and along the x-axis we're going to put the students name okay so name across the bottom and then we want their grade across the side so you would think that this the the y-axis would be called y but it's not when you're using matplotlib the the plot item here it's called height I'm sorry I didn't name it and here we're going to put the grade for the students and with that we can

just go ahead and say plt.show and this should be all we need to create a bar chart of the students and their grades and there we go we have a bar chart it's not easy to look at but there it is so let's take a minute and customize this chart make it a little easier on the eyes so let's start customizing here let's add a title to this chart which is called student data student I'll tell you what let's talk student grades or descriptive there and then let's add a label to the x-axis which is just X label we'll call this name and then the Y which was height before is actually now y label it is y label and we'll call that grade and then the most problematic thing with this chart is that these X ticks are all running together and you can't read them so if we turn them 90 degrees they'll be a lot easier to read so let's go ahead and do that let's say plot dot X ticks and then we're just going to say rotation equals 90. so

we're going to rotate them 90 degrees just go ahead and run this and now this chart's a whole lot easier to read because we can actually read the names on the bottom and there's our favorite student Vicky with that horrible score so we can also do things like change the color here so if we change the background say well excuse me not the background but the color of the bars this will change the color of the bars to Orange so we have a lot of control over the charts in the way that they look when you're working with charts what we've got here is something that we've called a plot but it's important to be specific plot is actually the chart that's the plot the whole thing itself if we include all the way out to the edge that's called a figure now the figure can have multiple plots or charts within it and the figure is created for you automatically if you don't specify it but if you do specify it you get a lot more control over the size and shape

of the chart so up here at the top we're just going to Define a variable called Figure and then we're going to say that that is a plot figure and then once we do that we can start to change the dimensions of the plot itself and we can do that with figs so we might want this chart to be wider or taller just changing the dimensions now the Fig size takes two variables a width and a height but they're in inches so for instance if we wanted a chart that was 10 inches by 4 inches 10 4. but because we're dealing with screens we don't think in inches we think in pixels so one thing that you can do here is because the DPI is 100 by default on these plots in matplotlib you could just multiply by a hundred to get the pixel so this would be a thousand pixels by 400 pixels if we wanted something that was 1280 by 720 that would be 12.8 by that's right 7.2 and we actually don't need to change anything else we just go ahead and run this it will

resize that chart to that size so that's an important thing to know is just how you make charts bigger or smaller with matplotlib and you do that with the figure and fix size okay now as we said this whole thing is called a figure and a figure can have multiple plots those are charts and that's commonly done because it helps us visualize data and we're going to do that now when we want to have multiple plots in a figure we need to use the subplots item on the plot element there and then where the fix size is the first thing we need to tell it is how many rows and how many columns this figure is going to have so in this case we want to have one row and two columns and the fixed size is a little bit big here let's take this back down to ten by four or a thousand pixels by 400 pixels when you don't change the the standard DPI now we no longer are working directly with the plot element and instead we're working with a an axis element all right so that

actually is returned by the subplots here along with our figure and we'll just say on the first axis okay we have two columns 0 and 1. so we're going to put a chart in the First Column which is 8x0 a chart in the second column will be ax one so in ax0 we're going to put a bar chart and the rest of this pretty much stays the same now we're no longer working with the plot element and instead we're working with this ax 0 right that's where we're defining the plot and instead of title it's set title and instead of all this stuff here we're just gonna have this 8x0 like we did before down here and instead of X text we're going to say set X tick labels and then inside of that we're going to say DF students do you have students.name and they're going to rotate the labels 90 degrees just like we did before and this gives us basically the same bar chart that we created now we want to create a pie chart to show the pass and fail totals we're going to put that next to the bar

chart so to do that we're going to get a total count of all of the passes so a count of how many people failed and how many people passed so to do that we're going to take our data frame and we're going to look at the past column and in order to sum up all of the people who passed all the trues and all the falses what we need to do is use the value counts method here which will give us some of all the truths and the sum of all the falses two values and then we want to pass those in and make a pie chart so on the axis one which is the second column we're going to create a pie chart and in that pie chart we're going to put the past counts as the data that we want graphed and then for the labels we're just going to use the pass counts as well because that's going to tell us true or false false and that's all we need to know here and then just like with the bar chart if we want a title we can call set title here and pass in a new title let's call this

passing grades perfect and then finally we're just going to call fig.show right because we don't have a plot element anymore we have a figure element okay so let's go ahead and run this and see what we've got all right we got an error we did get charts but one of them didn't render and we got an error but this is great because it gives us a chance to debug us and see what went wrong so if we come up here to the top here we can see this which is called a stack trace and we can see it's telling us line 11 here this line This label right here it's telling us got an unexpected argument label okay and that's because it's labels not label right and this is what makes programming

kind of tedious sometimes it's because you have to get this exactly right so let's change that run it and now look we have our passing grades on the right as a pie chart and our student grades on the left two plots one figure all right now it's worth noting that charting and plotting is so foundational to data science that you don't even need to use matplotlib directly it's kind of built into the data frame itself so we can actually plot this or chart this directly from the data frame so let's call DF students and then let's just say that we want to do a plot what kind of plot well we want a bar plot on the x-axis we want the name and in this case it is the why not the height this time which is nice so on the Y we want what we want the grade and then we can actually set the big size here as well so let's set that equal to 10 by 4 seem to be a good one and then just like with the previous chart we can set the color let's set it to something else here this

is actually all you need to create a nice bar chart really easy to do directly off the data frame all right now it's time to get into some actual data science here and figure out what's going on in this data and what what we want to do is take a look at these grades and something that data scientists are often interested in when looking at data is the distribution of that data across the sample now what's a sample the sample is the data that we have okay the grades the grades here are our sample and then there's the population the population could be any student who might ever take this class so we look at data for the sample because we can't look at the entire population and then we make an assessment or a judgment about the entire population based on the sample we have the larger the sample and the more data points the better our prediction can be for the entire population when we examine the distribution or how the data is spread out across the sample a great

chart for doing that is called a histogram so in our notebook here let's create a histogram so let's get some data here we'll just call it grades and then we'll just pone in from our dsstudents.grade column and then let's create a new figure and then we're going to just say that we want to plot with a new figure and this allows us to set the Fig size right and then we'll just make that a 10 by 4 I think is what we've been doing so far and then we're going to use the plot item to plot a histogram we're just going to tell it that we want a histogram and we want the histogram to be the grades the data that's in the grades variable there now just like we did before we can customize this let's add a title let's just call it data distribution and then let's go ahead and set our X label here we go and we'll just say that this is the value and then we'll set our y label and this is the frequency at which this occurs and then let's just go

ahead and show this figure and run this and we get a histogram so let's talk about what we're looking at here the histogram shows the distribution of the data again across the entire sample so from 0 to 100 this is where the data Falls you can see that most of the grades are actually right between about what 30 40 the low 40s and 60 right maybe mid 40s and 60 right there so most people scored between about a 45 and a 60 and that's why that's the tallest point and then over on the very right

you have the people that scored the highest grades which is only a few of those then over on the left you have Vicky who's bringing the whole average down all right so this histogram is a symmetric shape with most of the data being in the middle and and the fewer being out on the edges which is quite common when looking at sample data for a population so another thing that data scientists do when looking at a histogram like this is try to Define What's called the central tendency and that basically means what's the middle point of the data now there's a couple different ways that you can measure this one of them is something called the mean which is an average another one is called the median which is the number that is right in the center if you line up all of the data from lowest to highest what's the number that's right in the middle equally from both ends and last is the mode and the mode is the number that occurs most often in a sample so let's

go ahead and add that to our chart here and see if it tells us anything different all right so right here before we actually plot our figure let's go ahead and get the Min value which is our students.grade dot men so next we're going to look for the max vowel we're going to set that equal to dfstudents.grade.max and as you can see this these lines are starting to look very similar and we actually need the mean the median and the mode so in vs code we can use a shortcut here to do that where we copy down lines and we do that using option shift down and that just copies the line down we can do that two more times here right so here let's set a Max Val we want let's say the mean and then let's come out here to the edge and get the mean and then for this next one let's do the median I actually just call it there we go and then pull in the median and all of these are built-in functions right on the data frame which is really nice and then the

last one is the mode and as you can guess come out here and ask for the mode the one difference with the mode is that you might have multiple numbers that occur the same amount of times right so maybe the score that occurs the most is 65 and then the score 68 also occurs that same number of times and so because of that mode is a list of items so we need to make sure we get just the first item just by asking for that zero placed item there all right so let's go ahead and print these values out so we can actually look at them and then we want to add this to our chart so to plot lines on a histogram you use the axv line method here and this adds a line we need to tell it the line the value that we want to add here is the Min value and then we can do things like specify the color okay now because we're going to need a line for min max mean median mode again we can just use the option shift down or alt shift down on Windows key to

copy lines down just like this right so we can make this one this is the Min we can make this the max valve it can also be gray let's make this one the mean vowel and let's make it uh cyan and oops I capitalized a v here and we get that nice squiggly to tell us that it doesn't recognize if you see that see that squiggly if we actually Mouse over it'll tell us it's not defined which means we use the wrong variable name so let's go ahead and correct that and then we can make this one the median value and let's just make that red and then lastly let's take this and make this the mode value and let's make that yellow should be fine now that we've done all of that let's go ahead and run that again and check out our chart so now we have a histogram and we have our lines here so if you remember the gray line is the Min this gray line is the max this is the mean median and mode and in our sample they all happen to be in the middle right we could pretty much

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent