Module 2, topic 18, week 9, Thursday
Descriptive statistics: mean, median, variance, standard deviation
From data to a few well-chosen numbers: the kinds of data, frequency tables, mean, median and mode, quartiles and the box plot, variance and standard deviation, outliers, shape and rescaling. Worked by hand.
Introduction: from data to a few numbers
The sixth lesson of Module 2, and the first one on statistics. The last two lessons were about probability: the distribution was known, and we calculated what the data would look like. From today the direction is turned around. We start with data, a column of numbers that somebody collected, and ask what it tells us. The first step is always the same: describe the data with a few well-chosen numbers. Where is its centre? How far do the values spread? Is it symmetric or lopsided? Are there values that do not fit? This is descriptive statistics. This guide teaches the measures of centre (mean, median, mode), the measures of spread (range, quartiles, variance, standard deviation), how to read the shape of data, how to find outliers, and how these numbers change when the data is rescaled. Every idea comes as a definition and worked examples calculated by hand. Then the guide walks through 26 solved problems from the easiest to the hardest, and at the end there are 40 practice problems with a hint and a full solution for each.
You need the last lesson only lightly: the mean and the standard deviation of a distribution, the z-score and the 68-95-99.7 rule. A calculator with a square root key is enough. As in the whole module, the work is done by hand; Python appears only in one short part near the end.
What you will learn
- 1.Why every analysis and every machine learning project starts with descriptive statistics
- 2.The kinds of data (categorical and numerical) and which summaries fit each kind
- 3.Frequency tables, relative and cumulative frequencies, and histograms
- 4.The mean, its properties, the weighted mean and the mean from a frequency table
- 5.The median and the mode, and when the mean misleads
- 6.The range, quartiles, percentiles, the interquartile range, the five-number summary and the box plot
- 7.The variance and the standard deviation, for a population and for a sample, and why a sample divides by
- 8.How to read a standard deviation: z-scores, the 68-95-99.7 rule, Chebyshev's rule and the coefficient of variation
- 9.What happens to the summaries when the data is shifted or rescaled; standardising and min-max scaling
- 10.The shape of data: skewness, outliers and robust statistics
- 11.Data in classes (grouped data)
- 12.Where these numbers appear in machine learning
| Written | Meaning |
|---|---|
| the values of the data | |
| the number of values in a sample; for a whole population | |
| the sum of all values ( is the Greek capital letter sigma, "sum") | |
| the mean of a sample ("x bar") | |
| the mean of a population | |
| , | the variance and the standard deviation of a sample |
| , | the variance and the standard deviation of a population |
| , , | the first, second and third quartile; is the median |
| the interquartile range, | |
| a frequency: how often a value appears | |
| a z-score: the value minus the mean, divided by the standard deviation |
1Why descriptive statistics?
1.1Nobody can read a million rows
A table of 300 orders can still be looked at. A table of a million rows cannot. Yet a decision has to be made from it: is the typical order large or small, are the delivery times reliable, did the new design change anything? Descriptive statistics replaces the column by a handful of numbers that a person can hold in the head:
- 300 delivery times become: typical 31 minutes, spread about 6 minutes, slowest 58 minutes.
A good summary answers three questions:
- Where is the centre? What is a typical value?
- How large is the spread? How far are the values from the centre and from each other?
- What is the shape? Symmetric or lopsided, one peak or two, with or without unusual values?
1.2Describing and concluding
Statistics has two halves:
| Descriptive statistics | Inferential statistics | |
|---|---|---|
| Question | What does this data look like? | What does it say about the population behind it? |
| Tools | mean, median, standard deviation, tables, charts | tests, confidence intervals |
| Topics | this lesson | Topics 19 and 20 |
The second half is built on the first. A test in Topic 19 compares a sample mean with a claim; a confidence interval in Topic 20 is the sample mean plus or minus a multiple of the standard error . Both need the and the of this lesson.
1.3Where you will meet these ideas again
| Later in the course | What it takes from this lesson |
|---|---|
| Hypothesis testing (Topic 19) | the sample mean and the sample standard deviation |
| Confidence intervals and correlation (Topic 20) | , and the standard error |
| Linear regression | the mean squared error is a variance; the best constant prediction is the mean |
| Feature scaling (Topic 24) | standardising with the mean and the standard deviation; min-max scaling |
| Handling outliers (Topic 25) | quartiles, the IQR rule, z-scores |
| Missing values | filling a gap with the mean or the median |
| Decision trees and random forests | a leaf predicts the mean of its rows and splits to reduce the variance |
| Exploring any new data set | describe() in pandas prints the numbers of this lesson |
2Data and its kinds
2.1Population and sample
The last lesson introduced the words. The population is everything we want to know about; a sample is the part we observed. A number that describes the population is a parameter (, ); a number calculated from the sample is a statistic (, ). Most data is a sample, and the difference matters once in this lesson: in the formula of the variance (Part 7).
2.2Kinds of variables
A column of a table is a variable. What can be calculated with it depends on its kind.
| Kind | What its values are | Examples | Summaries that make sense |
|---|---|---|---|
| Categorical, nominal | names without an order | city, colour, payment method | counts, shares, mode |
| Categorical, ordinal | names with an order | rating (bad, good, excellent), level of education | counts, mode, median |
| Numerical, discrete | counts | number of children, orders per day | everything |
| Numerical, continuous | measurements | height, price, time | everything |
Two traps:
- A number is not always numerical. A postal code, a phone number or a shirt number is a name written with digits. The "mean postal code" means nothing.
- For an ordinal variable the distances are unknown. Is "excellent" as far from "good" as "good" is from "bad"? That is why its mean is doubtful, while its median is fine.
What kind of variable is it?
1 of 8
The city in which a customer lives.
Try it yourself
Show a hint
Show the solution
Answer: (a) nominal, (b) discrete, (c) ordinal, (d) continuous.
(a) Brands are names without an order: nominal. (b) A count of people: discrete. (c) Low, medium, high have an order, but the steps between them are not numbers: ordinal. (d) A measurement that can take every value in an interval: continuous.
3Frequency tables and histograms
3.1Counting the values
Twenty families were asked how many children they have:
A frequency table counts how often every value appears:
| Children | Frequency | Relative frequency | Cumulative frequency |
|---|---|---|---|
| 0 | 4 | 4 | |
| 1 | 7 | 11 | |
| 2 | 6 | 17 | |
| 3 | 2 | 19 | |
| 4 | 1 | 20 | |
| Total | 20 | 1 |
- The frequency is the count. All frequencies together are .
- The relative frequency is the share, . All shares together are 1. It is what the last lesson called a probability, now measured from data.
- The cumulative frequency counts all values up to this one: 17 families have at most 2 children.
3.2Classes and histograms
A continuous variable has too many different values for such a table. Its values are collected in classes (intervals), and the table counts the values in each class. Drawn as bars that touch each other, it is a histogram.
The ages of 30 customers in classes of ten years:
| Age | 20 to 30 | 30 to 40 | 40 to 50 | 50 to 60 | 60 to 70 |
|---|---|---|---|---|---|
| Frequency | 4 | 9 | 10 | 5 | 2 |
By agreement a value on a border belongs to the class that begins there: a customer of exactly 30 is counted in "30 to 40".
3.3What a histogram shows
Symmetric
mean and median almost equal
Skewed to the right
the tail pulls the mean above the median
Skewed to the left
the tail pulls the mean below the median
- Symmetric: the left and the right half look alike. The mean and the median are almost equal.
- Skewed to the right: a long tail of large values (incomes, prices, waiting times). The tail pulls the mean above the median.
- Skewed to the left: a long tail of small values (marks of an easy exam). The mean lies below the median.
The name follows the tail, not the peak. A histogram also shows whether the data has one peak or two (two peaks often mean two mixed groups) and whether single values stand far away from the rest.
Try it yourself
Show a hint
Show the solution
Answer: , 20 students, and .
4The mean
4.1Definition
The mean (the arithmetic average) is the sum of the values divided by their number:
For a whole population the same calculation is written .
Example. The marks of five students are 6, 9, 4, 10 and 6.
4.2The mean is the balance point
The distance of a value from the mean, , is its deviation. For the five marks:
| 6 | 9 | 4 | 10 | 6 | Sum | |
|---|---|---|---|---|---|---|
| 0 |
The deviations always add up to exactly 0: what lies above the mean balances what lies below. Put the values as equal weights on a ruler, and the ruler balances at the mean.
Three consequences:
- The sum of the data is . Five marks with the mean 7 add up to 35, whatever they are.
- Every value takes part. Change one value and the mean changes.
- One extreme value can pull the mean far away (Part 5).
4.3The weighted mean
Sometimes the values do not count equally. A course grade is made of homework (20 percent), a midterm (30 percent) and a final exam (50 percent). A student has 80, 70 and 90 marks:
In general, with the weights :
When the weights add up to 1, as above, the division disappears. The expected value of the last lesson is a weighted mean with the probabilities as weights.
Combining groups. Class A has 20 students with the mean 70; class B has 30 students with the mean 80. The mean of all 50 is not 75. Weight every mean with the size of its group:
4.4The mean from a frequency table
For the 20 families of Part 3 nobody adds twenty numbers. Multiply every value by its frequency:
This is the weighted mean again, with the frequencies as weights.
Try it yourself
Show a hint
Show the solution
Answer: (a) The mean is 8 and the deviations add up to 0. (b) The mean price is 5.
The deviations of any data from its own mean add up to 0. In (b) three quarters of the items cost 4, so the mean lies much nearer to 4 than to 8.
5The median and the mode
5.1The median
The median is the value in the middle of the sorted data: half of the values lie below it and half above.
- Sort the values.
- If is odd, the median is the middle value, number in the list.
- If is even, the median is the mean of the two middle values, numbers and .
An odd number of values. 12, 7, 3, 15, 9:
It is value number 3 of 5.
An even number of values. 8, 3, 10, 5, 12, 7:
5.2When the mean misleads
Five employees of a small company earn 800, 900, 1000, 1100 and 1200 a month.
Now add the owner, who earns 7000:
"The mean salary is 2000" is true and useless: five of the six people earn far less. The median hardly moved. One extreme value can move the mean as far as it likes; it cannot move the median, because the median only asks which value stands in the middle, not how far away the others are.
Five employees earn 800, 900, 1000, 1100 and 1200. Move the salary x of the owner.
Mean: (5,000 + 7,000) / 6 = 2,000
Median: (1,000 + 1,100) / 2 = 1,050
The mean follows the one large value. The median only asks which values stand in the middle, so it does not move at all.
A statistic that extreme values hardly change is called robust. The median is robust; the mean is not.
| Mean | Median | |
|---|---|---|
| Uses | every value | only the order of the values |
| Extreme values | pull it strongly | hardly change it |
| Best for | symmetric data without outliers | skewed data, data with outliers |
| Typical use | marks, heights, measurement errors | incomes, prices of flats, waiting times |
Mean and median together show the shape. Mean above the median: a tail to the right. Mean below the median: a tail to the left. Almost equal: roughly symmetric.
5.3The mode
The mode is the value that appears most often. Shoe sizes sold in a shop: 38, 40, 40, 41, 42, 40, 39, 42. The mode is 40, which appears three times.
- Data can have two modes (bimodal) or more, or none when every value appears once.
- The mode is the only measure of centre for a nominal variable: the most frequent city, the most popular colour.
- For continuous data single values rarely repeat; there the modal class is used, the highest bar of the histogram.
5.4A compromise: the trimmed mean
The trimmed mean removes a fixed share of the smallest and of the largest values and takes the mean of the rest. Judges' scores in some sports work this way: the highest and the lowest score are dropped. For the six salaries, dropping the smallest and the largest value leaves 900, 1000, 1100, 1200 with the mean 1050.
Try it yourself
Show a hint
Show the solution
Answer: The mean is 10, the median is 7. The median describes a typical wait better.
Five of the six customers waited less than 10 minutes. The one wait of 30 minutes pulls the mean up, so the median is the better description.
6Spread by position: range, quartiles and the box plot
6.1Why the centre is not enough
Two machines fill bags of 500 g. Both have the mean 500 g. The bags of machine A weigh between 498 and 502 g; those of machine B between 470 and 530 g. Nobody would call them equally good. A summary needs a number for the spread.
Machine A
range 4
Machine B
range 60
6.2The range
Machine A: . Machine B: . The range is the simplest measure and the most fragile one: it uses only two values, and a single unusual value changes it completely.
6.3Quartiles
The quartiles cut the sorted data into four parts with the same number of values:
- , the first quartile: a quarter of the values lie below it.
- , the second quartile: the median.
- , the third quartile: three quarters of the values lie below it.
How to find them. Sort the data and number the values from 1 to . The quartile for the share (0.25, 0.5 or 0.75) stands at the position
If the position is a whole number, the quartile is that value. If it lies between two values, go the same fraction of the way from the lower to the upper one.
Example. Eleven values, already sorted:
| Position | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 |
|---|---|---|---|---|---|---|---|---|---|---|---|
| Value | 3 | 5 | 7 | 8 | 9 | 11 | 13 | 15 | 16 | 18 | 21 |
A position such as 2.75 means: start at the 2nd value and go 0.75 of the way to the 3rd.
6.4Percentiles
Quartiles are special percentiles. The 90th percentile is the value below which 90 percent of the data lie; its position is . , the median and are the 25th, 50th and 75th percentile. "Your result is in the 90th percentile" means: you did better than 90 percent of the group.
6.5The interquartile range
It is the width of the middle half of the data. For the eleven values: . Like the median, the IQR is robust: the smallest and the largest quarter of the data can do what they like.
6.6The five-number summary and the box plot
Five numbers describe centre, spread and shape at once:
| Minimum | Median | Maximum | ||
|---|---|---|---|---|
| 3 | 7.5 | 11 | 15.5 | 21 |
The box plot draws them: a box from to with a line at the median, and "whiskers" out to the smallest and the largest value that are not outliers.
How to read it: a median in the middle of the box and whiskers of equal length mean symmetric data. A median near the left edge and a long right whisker mean a tail to the right.
6.7Outliers: the 1.5 IQR rule
A value far away from the rest is an outlier. The usual rule builds two fences:
A value outside the fences is called an outlier and is drawn as a single point in a box plot.
For the eleven values: and . All values lie inside, so there is no outlier. If the largest value were 35 instead of 21, it would be one.
An outlier is a question, not a verdict. It may be a typing mistake (an age of 250), a fault of an instrument, or a real and important case (a fraud, a very loyal customer). Find out which before you remove anything.
Choose a data set. The quartiles are found with position = 1 + (n − 1) × p.
| position | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 |
|---|---|---|---|---|---|---|---|---|---|---|---|
| value | 3 | 5 | 7 | 8 | 9 | 11 | 13 | 15 | 16 | 18 | 21 |
Q1: position 1 + 10 × 0.25 = 3.5: halfway from the 3rd value (7) to the next (8). Q1 = 7.5
Median: position 1 + 10 × 0.5 = 6: the 6th value. Median = 11
Q3: position 1 + 10 × 0.75 = 8.5: halfway from the 8th value (15) to the next (16). Q3 = 15.5
IQR = 15.5 − 7.5 = 8. Fences: 7.5 − 1.5 × 8 = −4.5 and 15.5 + 1.5 × 8 = 27.5
Outliers: none
Try it yourself
Show a hint
Show the solution
Answer: , median 14, , IQR 8, upper fence 29. Yes, 38 is an outlier.
The value 38 lies above 29, so it is an outlier. The lower fence is , and no value lies below it.
7The variance and the standard deviation
7.1Spread as distance from the mean
Range and IQR look at positions. The most used measure of spread asks another question: how far are the values from the mean, on average?
The deviations cannot simply be averaged, because they add up to 0 (Part 4). So they are squared first: every square is positive, and large deviations count more.
7.2The variance of a population
When the data is the whole population (all values, mean ):
The variance is the mean of the squared deviations. Its square root is the standard deviation , which has the unit of the data again.
Example. Eight values: 2, 4, 4, 4, 5, 5, 7, 9. Their mean is .
| 2 | 4 | 4 | 4 | 5 | 5 | 7 | 9 | |
|---|---|---|---|---|---|---|---|---|
| 0 | 0 | 2 | 4 | |||||
| 9 | 1 | 1 | 1 | 0 | 0 | 4 | 16 |
"The values lie about 2 away from the mean 5."
7.3The variance of a sample
Almost always the data is a sample, and we use it to estimate the spread of the population. Then the sum of squares is divided by :
If the eight values are a sample:
7.4Why n − 1?
The deviations are measured from the sample mean , and was calculated from the same values. It sits right in the middle of them, closer than the unknown would. So the squared deviations come out a little too small, and dividing by would estimate a spread that is too small. Dividing by the smaller number repairs this exactly.
Another way to see it: the deviations add up to 0. Once of them are known, the last one is fixed. Only of them carry free information. This number is called the degrees of freedom.
| Population | Sample | |
|---|---|---|
| Mean | ||
| Variance | ||
| Standard deviation | ||
| Use when | the data is everything there is | the data stands for something larger (the usual case) |
For large the two hardly differ. For small samples they do: with the sample variance is 25 percent larger. Unless a problem says that the data is the whole population, use . Calculators have two keys (often and , or and ), and programs have a switch (Part 17).
7.5The steps
- Calculate the mean.
- Subtract the mean from every value: the deviations (check: their sum is 0).
- Square the deviations.
- Add the squares.
- Divide by for a sample (by for a population): the variance.
- Take the square root: the standard deviation.
Five values. Move them and follow the six steps to the standard deviation.
| value | 4 | 8 | 6 | 5 | 7 | sum 30 |
|---|---|---|---|---|---|---|
| value − mean | −2 | 2 | 0 | −1 | 1 | sum 0 |
| squared | 4 | 4 | 0 | 1 | 1 | sum 10 |
Mean: 30 / 5 = 6. Sum of the squares: 10
Variance: 10 / 4 = 2.5. Standard deviation: √2.5 = 1.581
The distances from the mean always add up to 0. Move one value far away from the others: its square takes over the sum. The filled strip is the mean plus and minus one standard deviation.
7.6A shortcut
When the mean is not a round number, the deviations are awkward. The same sum of squares comes without them:
For the eight values:
The same sum of squares.
For a population this gives , "the mean of the squares minus the square of the mean", which is the formula of the last lesson.
7.7From a frequency table
For the 20 families of Part 3 (mean 1.45), multiply by the frequencies:
Try it yourself
Show a hint
Show the solution
Answer: Mean 6, sum of squares 26, and .
| 3 | 5 | 6 | 10 | Sum | |
|---|---|---|---|---|---|
| 0 | 4 | 0 | |||
| 9 | 1 | 0 | 16 | 26 |
Of the 26 in the sum of squares, 16 come from the single value 10: the value farthest from the mean counts most.
8Reading a standard deviation
8.1Small and large
A standard deviation is a typical distance from the mean, in the unit of the data. Whether it is "large" depends on the mean and on the purpose: 5 minutes are a lot for a bus that comes every 10 minutes and nothing for a flight of 9 hours.
- means that all values are equal.
- The standard deviation is never negative.
- Like the mean, it is not robust: an outlier is squared and makes it much larger.
8.2z-scores for data
The z-score of the last lesson works for data too:
It says how many standard deviations a value lies above () or below () the mean. In a class with the mean 64 and the standard deviation 8, a mark of 80 has .
8.3The 68-95-99.7 rule
If the histogram of the data is roughly a bell, the rule of the last lesson holds for it:
- about 68 percent of the values lie within 1 standard deviation of the mean
- about 95 percent within 2
- about 99.7 percent within 3
So for bell-shaped data a value with a z-score beyond 3 or is very unusual, and this is the second common rule for outliers.
8.4Chebyshev's rule: for every shape
When the data is not bell-shaped, a weaker rule still holds, for any data whatever. For every , at least this share of the values lies within standard deviations of the mean:
| 2 | 3 | 4 | |
|---|---|---|---|
| At least | 75 percent | about 89 percent | about 94 percent |
| For a bell (for comparison) | about 95 percent | about 99.7 percent | almost all |
The words "at least" matter: usually far more values lie inside. But the rule can never fail.
8.5The coefficient of variation
Is a standard deviation of 5 kg large? For the weights of adults it is small; for the weights of cats it is huge. The coefficient of variation compares the spread with the mean:
Adults with the mean 75 kg and kg have percent. Cats with the mean 4 kg and kg have percent: relative to their size, cats differ far more. The CV has no unit, so it can compare columns in different units. It makes sense only for quantities that cannot be negative and have a real zero (weights, prices, times), not for temperatures in degrees Celsius.
Try it yourself
Show a hint
Show the solution
Answer: (a) , (b) at least 75 percent, (c) about 8.3 percent.
If the times were bell-shaped, about 95 percent would lie between 50 and 70 seconds. Without that knowledge only the 75 percent are certain.
9Changing the data: shifting, rescaling and standardising
9.1What happens to the summaries
Every value is changed in the same way: .
| Change | Mean, median, quartiles | Range, IQR, standard deviation | Variance |
|---|---|---|---|
| Add to every value | is added | unchanged | unchanged |
| Multiply every value by | multiplied by | multiplied by | multiplied by |
| Both: | multiplied by | multiplied by |
A shift moves the whole picture without changing its width. A factor stretches everything, the centre and the width.
The data
mean 5
Every value plus 10
mean 15: the same picture, moved
Every value times 2
mean 10: the picture is stretched
Example. A teacher adds 5 marks to every result. The mean rises by 5; the standard deviation stays as it was. Another teacher converts marks out of 50 to marks out of 100 by doubling: the mean and the standard deviation both double, and the variance becomes four times as large.
Every value x becomes y = a × x + b. What happens to the summaries?
Mean: 2 × 5 + 0 = 10. Median: 2 × 4.5 + 0 = 9
Standard deviation: 2 × 2.191 = 4.382. Variance: 4 × 4.8 = 19.2
The shift b appears only in the mean and the median. The spread feels only the size of a: a negative a turns the picture round without changing its width.
9.2Standardising
Replace every value by its z-score:
This is a shift by followed by a division by . By the table above, the new column has the mean 0 and the standard deviation 1, whatever the old one looked like. The shape does not change: skewed data stays skewed.
Example. The values 2, 4, 6 have the mean 4 and, as a population, the standard deviation :
9.3Min-max scaling
Another rescaling brings every column into the interval from 0 to 1:
The smallest value becomes 0, the largest becomes 1. It is simple, but one outlier squeezes all other values into a corner.
| Standardising | Min-max scaling | |
|---|---|---|
| Result | mean 0, standard deviation 1 | from 0 to 1 |
| Uses | mean and standard deviation | minimum and maximum |
| Outliers | disturb it | disturb it strongly |
Both are used in machine learning so that a column in metres and a column in thousands count equally (Part 12).
The values 10, 20, 25, 30 and one largest value x, rescaled in two ways.
| value | 10 | 20 | 25 | 30 | 50 |
|---|---|---|---|---|---|
| standardised | −1.15 | −0.47 | −0.13 | 0.2 | 1.55 |
| min-max | 0 | 0.25 | 0.38 | 0.5 | 1 |
Standardising: mean 27, standard deviation 14.83. The z-scores have the mean 0 and the standard deviation 1.
Min-max: (value − 10) / (50 − 10). The four smaller values lie between 0 and 0.5.
The picture shows the min-max values. Make x very large: the other four values are pressed together near 0. Standardising suffers too, because x pulls the mean and the standard deviation up.
Try it yourself
Show a hint
Show the solution
Answer: Mean 46, median 40.5, standard deviation 8.8.
The delivery fee moves every price by the same 2, so it leaves the spread alone.
10Shape, outliers and choosing the summary
10.1Skewness
Part 3 showed the three shapes. Without a picture, the summaries reveal the shape:
| Sign | Symmetric | Skewed to the right | Skewed to the left |
|---|---|---|---|
| Mean and median | about equal | mean above the median | mean below the median |
| Box plot | median in the middle, equal whiskers | long right whisker | long left whisker |
| Quartiles |
Skewed to the right is the common case for quantities with a lower limit and no upper limit: incomes, prices, waiting times, the sizes of cities.
10.2Two families of summaries
| Uses every value, not robust | Uses positions, robust | |
|---|---|---|
| Centre | mean | median |
| Spread | standard deviation | IQR |
| Report it for | symmetric data without outliers | skewed data or data with outliers |
The pairs belong together: "mean and standard deviation" or "median and IQR". When you are unsure, give both, or better: look at the histogram first.
10.3Summaries can hide the truth
The data sets 1, 1, 1, 9, 9, 9 and 1, 3, 5, 5, 7, 9 have the same mean (5) and the same median (5), and the first has no value anywhere near 5. A summary is a loss of information by design. Always look at a picture of the data (a histogram or a box plot) before you trust its numbers.
1, 1, 1, 9, 9, 9
1, 3, 5, 5, 7, 9
Try it yourself
Show a hint
Show the solution
Answer: (a) Skewed to the right. (b) The median and the IQR.
The mean lies far above the median, so a tail of large incomes pulls it up: the data is skewed to the right. For skewed data the robust pair, median and IQR, describes a typical household better. "A typical household has 2,200" is closer to the truth than "3,000".
11Data in classes
Sometimes only the table of classes is available, not the single values. The mean can still be estimated: let every value of a class be represented by the midpoint of the class.
The ages of the 30 customers of Part 3:
| Class | 20 to 30 | 30 to 40 | 40 to 50 | 50 to 60 | 60 to 70 |
|---|---|---|---|---|---|
| Midpoint | 25 | 35 | 45 | 55 | 65 |
| Frequency | 4 | 9 | 10 | 5 | 2 |
| 100 | 315 | 450 | 275 | 130 |
The modal class is "40 to 50", the class with the highest frequency. The median is the mean of the 15th and the 16th value; the cumulative frequencies are 4, 13, 23, so both lie in the class "40 to 50", the median class.
The result is an estimate: the true values inside a class are unknown. The narrower the classes, the better it is.
Try it yourself
Show a hint
Show the solution
Answer: The mean is about 7.4 minutes. The modal class is "4 to 8".
The class "4 to 8" has the highest frequency, 9 customers, so it is the modal class.
12Descriptive statistics in machine learning
The first look at any data. In pandas, df.describe() prints for every numerical column the count, the mean, the standard deviation (with ), the minimum, , the median, and the maximum: the summaries of this lesson. A mean far from the median tells you about skew before any chart is drawn; a maximum of 250 in a column of ages tells you about a mistake.
Filling missing values. A gap in a column is often filled with the mean or the median of the column. For a skewed column or one with outliers the median is the safer choice, for the reason of Part 5. For a categorical column the mode is used.
Feature scaling. Many models (k-nearest neighbours, support vector machines, neural networks, anything trained with gradient descent) work badly when one column has values in the thousands and another between 0 and 1. Standardising or min-max scaling (Part 9) makes the columns comparable.
Learn the numbers from the training data only. The mean and the standard deviation used for scaling, and the median used for filling gaps, are calculated from the training set and then applied unchanged to the test set. Calculating them from all the data lets information about the test set leak into the training, and the measured quality becomes too good to be true. This mistake is called data leakage.
Outliers. The 1.5 IQR rule and the z-score rule are the standard first checks. A few wrong extreme values can pull a regression line or a mean prediction far away.
Errors of a model. The mean squared error, , is built like a variance; its square root, the RMSE, like a standard deviation. The mean absolute error is the more robust relative. The model "always predict the mean" has an MSE equal to the variance of the target; a useful model must beat that.
The mean and the median as predictions. If you must predict one number for everybody, the mean makes the squared errors smallest and the median makes the absolute errors smallest. A regression tree does exactly this in each of its leaves.
Try it yourself
Show a hint
Show the solution
Answer: (a) Those of the training set. (b) .
The mean 54 and the standard deviation 12 of the test set are not used at all. With them the value would become , and the model would see numbers on another scale than the one it was trained on.
13Common mistakes
Forgetting to sort before taking the median. The median is the middle of the sorted data.
Taking the middle position as the median. For 9 values the median is the 5th value, not the number 5.
Averaging averages. The mean of two groups is the weighted mean of their means, not the simple one, unless the groups have the same size.
Reporting the mean for skewed data. A few very large values pull it up. Give the median, or both.
Dividing by for a sample. Sample variance: .
Forgetting the square root. The variance has the unit squared. The number to interpret is the standard deviation.
Squaring the sum instead of summing the squares. means: square every value, then add. is something else.
Adding standard deviations, or expecting a shift to change them. Adding 5 to every value changes the mean and not the spread.
Calling every outlier a mistake. An outlier may be the most interesting row of the table. Check before removing it.
Using the 68-95-99.7 rule for data that is not bell-shaped. For other shapes only Chebyshev's rule is safe.
Calculating the mean of a categorical variable. Codes such as 1 = Baku, 2 = Ganja are names. Their mean means nothing.
Scaling with the numbers of the whole data set. The mean and the standard deviation for scaling come from the training set only.
| Wrong | Right |
|---|---|
| median of 7, 2, 9 is 2 | sort first: 2, 7, 9, median 7 |
| groups of 20 and 30 with means 70 and 80: mean 75 | |
| sample variance | |
| "the spread is 16" (the variance) | "the standard deviation is 4" |
| adding 10 to all values adds 10 to | does not change |
| doubling all values doubles the variance | the variance is multiplied by 4 |
| mean 2000, so "a typical salary is 2000" | look at the median when the data is skewed |
14Solved problems (from the easiest to the hardest)
Each problem has the question, the idea behind it with the part of the lesson it uses, and the calculation step by step. Cover the solution and try first.
Level 1: The centre
Problem 1: A mean
A seller made 12, 18, 9, 21 and 15 sales on five days. Find the mean number of sales per day.
Idea. Add the values and divide by their number (Part 4).
Why: the five days together brought 75 sales; the mean spreads them evenly over the days.
Problem 2: A median, odd number of values
Find the median of 14, 6, 21, 9, 17, 11, 30.
Idea. Sort first. With 7 values the median is value number (Part 5).
Why: three values lie below 14 and three above it.
Problem 3: A median, even number of values
Find the median of 8, 3, 15, 6, 12, 10, 4, 9.
Idea. With 8 values the median is the mean of the 4th and the 5th sorted value (Part 5).
Why: the median need not be one of the values. Four values lie below 8.5 and four above it.
Problem 4: The mode
A café sold these numbers of cups of tea on ten days: 24, 30, 27, 30, 22, 27, 30, 25, 27, 30. Find the mode.
Idea. Count how often every value appears (Part 5).
| Value | 22 | 24 | 25 | 27 | 30 |
|---|---|---|---|---|---|
| Frequency | 1 | 1 | 1 | 3 | 4 |
The mode is 30.
Why: the mode is the most frequent value, not the largest frequency. The answer is 30, not 4.
Problem 5: Mean or median?
Six flats in a street were sold for 90, 95, 100, 105, 110 and 400 thousand. Find the mean and the median. Which describes a typical flat better?
Idea. One extreme value pulls the mean; the median stays (Part 5).
The median. Five of the six flats cost less than the mean.
Why: the mean is above the median, which is the sign of a tail to the right. Prices are almost always skewed like this, which is why reports on housing use the median.
Problem 6: A weighted mean
A final grade is made of a project (weight 25 percent), a midterm (25 percent) and a final exam (50 percent). A student has 88, 72 and 80. Find the final grade.
Idea. Multiply every value by its weight; the weights add up to 1 (Part 4).
Why: the simple mean of the three marks is also 80 here only by chance of the numbers; with a final exam of 60 the weighted grade would be 70 and the simple mean 73.3.
Problem 7: Mean, median and mode from a frequency table
The number of goals in 25 matches is given in the table below. Find the mean, the median and the mode.
| Goals | 0 | 1 | 2 | 3 | 4 |
|---|---|---|---|---|---|
| Matches | 3 | 8 | 7 | 5 | 2 |
Idea. Multiply by the frequencies for the mean. For the median use the cumulative frequencies (Parts 3 to 5).
The median is value number . The cumulative frequencies are 3, 11, 18, so the 13th value is a 2: the median is 2. The mode is 1, with 8 matches.
Why: the first 3 sorted values are 0, values 4 to 11 are 1, values 12 to 18 are 2. So number 13 is a 2.
Problem 8: A missing value
Four values have the mean 13. Three of them are 12, 15 and 9. Find the fourth.
Idea. The sum of the data is (Part 4).
Why: a mean fixes the total. This idea solves most "missing value" problems.
Problem 9: Combining two groups
A company has 40 workers with a mean salary of 900 and 10 managers with a mean salary of 1900. Find the mean salary of all 50 employees.
Idea. A weighted mean, with the group sizes as weights (Part 4).
Why: the simple mean of 900 and 1900 would be 1400, but there are four times as many workers as managers, so the result lies much nearer to 900.
Level 2: The spread
Problem 10: Quartiles on whole positions
Find the five-number summary and the IQR of 4, 7, 8, 10, 12, 13, 15, 18, 21.
Idea. with (Part 6).
The five-number summary is 4, 8, 12, 15, 21.
Why: with 9, 13, 17, ... values all three positions are whole numbers, and the quartiles are values of the data.
Problem 11: Quartiles between two values
Find , the median, and the IQR of 2, 5, 6, 9, 11, 14, 15, 20.
Idea. With the positions fall between values: go the fraction of the way (Part 6).
stands at the position : from the 2nd value (5), 0.75 of the way to the 3rd (6).
stands at the position : halfway between the 4th value (9) and the 5th (11).
stands at the position : from the 6th value (14), 0.25 of the way to the 7th (15).
Why: the whole part of the position names the value to start from, the decimal part says how far to go towards the next one.
Problem 12: Finding an outlier
The delivery times of nine orders in minutes: 21, 23, 24, 25, 26, 27, 28, 30, 55. Is there an outlier by the 1.5 IQR rule?
Idea. Quartiles, IQR, then the two fences (Part 6).
55 lies above the upper fence: it is an outlier. All other values are inside.
Why: the fences are built from the middle half of the data, which the value 55 cannot influence. That is what makes the rule reliable.
Problem 13: The variance of a population
A family has five children aged 3, 5, 7, 9 and 11. Find the variance and the standard deviation of the ages. (The five children are the whole population.)
Idea. All values are there, so divide by (Part 7).
| 3 | 5 | 7 | 9 | 11 | Sum | |
|---|---|---|---|---|---|---|
| 0 | 2 | 4 | 0 | |||
| 16 | 4 | 0 | 4 | 16 | 40 |
Why: nothing is estimated here. The five ages are all there is, so is not needed.
Problem 14: The variance of a sample
A sample of five parcels weighs 4, 8, 6, 5 and 7 kg. Find the sample variance and the sample standard deviation.
Idea. The six steps of Part 7, with .
| 4 | 8 | 6 | 5 | 7 | Sum | |
|---|---|---|---|---|---|---|
| 2 | 0 | 1 | 0 | |||
| 4 | 4 | 0 | 1 | 1 | 10 |
Why: the parcels stand for all parcels of the company, so this is a sample. Dividing by 5 would give 2, an estimate that is too small.
Problem 15: The shortcut
For the sample 1, 3, 4, 6, 6 find the variance with the shortcut formula.
Idea. (Part 7).
Why: check with the deviations , , 0, 2, 2: their squares 9, 1, 0, 4, 4 also add up to 18.
Problem 16: The standard deviation from a frequency table
In a sample of 10 households, 2 have one phone, 5 have two phones and 3 have three phones. Find the mean and the sample standard deviation of the number of phones.
Idea. Multiply by the frequencies, then the shortcut (Part 7).
Why: a frequency table is only a shorter way of writing the ten values 1, 1, 2, 2, 2, 2, 2, 3, 3, 3.
Problem 17: z-scores
In an exam the mean is 64 and the standard deviation is 8. Find the z-scores of the marks 80 and 52. Which of the two is more unusual?
Idea. (Part 8). The size of tells how unusual a value is.
The mark 80: it is 2 standard deviations away from the mean, and 52 only 1.5.
Why: "unusual" is about the distance from the mean, in either direction. The sign only says on which side the value lies.
Problem 18: Comparing spreads
Product A is sold for a mean price of 50 with a standard deviation of 5 in different shops. Product B has a mean price of 200 and a standard deviation of 12. Which price varies more, relative to its size?
Idea. The coefficient of variation (Part 8).
Product A.
Why: in absolute numbers B varies more (12 against 5). Relative to the price, A does. Which answer matters depends on the question.
Level 3: Rescaling, shape and putting ideas together
Problem 19: A linear change of the data
A column has the mean 20, the median 18 and the standard deviation 4. Every value is replaced by . Find the new mean, median, standard deviation and variance.
Idea. The table of Part 9: centres follow the whole formula, spreads only the factor.
Why: the shift moves all values together, so no distance between them changes.
Problem 20: Standardising
Standardise the values 10, 20, 30, 40 as a population. Check the mean of the result.
Idea. Subtract the mean, divide by the standard deviation (Part 9).
| 10 | 20 | 30 | 40 | |
|---|---|---|---|---|
| 5 | 15 | |||
| 0.447 | 1.342 |
The mean of the z-scores is .
Why: the z-scores always have the mean 0 and the standard deviation 1. The even spacing of the data is kept: standardising does not change the shape.
Problem 21: Min-max scaling
Scale the values 10, 20, 25, 50 to the interval from 0 to 1.
Idea. (Part 9).
The minimum is 10, and the maximum minus the minimum is 40.
Why: the value 50 lies far from the others, so the other three are pressed into the lower 40 percent of the interval. An outlier of 500 would press them below 0.04.
Problem 22: Chebyshev's rule
The waiting times at a counter have the mean 50 seconds and the standard deviation 5 seconds. Nothing is known about their shape. At least which share of the waiting times lies between 40 and 60 seconds? And between 35 and 65?
Idea. At least within standard deviations (Part 8).
At least 75 percent, and at least 88.9 percent.
Why: without the shape, the 68-95-99.7 rule may not be used. If the data were bell-shaped, the shares would be about 95 and 99.7 percent, well above the guaranteed minimum.
Problem 23: The mean of data in classes
The daily sales of a shop on 30 days are given in classes in the table below. Estimate the mean, and name the modal class and the median class.
| Sales | 0 to 10 | 10 to 20 | 20 to 30 | 30 to 40 |
|---|---|---|---|---|
| Midpoint | 5 | 15 | 25 | 35 |
| Days | 5 | 12 | 8 | 5 |
Idea. Midpoints stand for the classes (Part 11).
The modal class is "10 to 20" (12 days). The median is the mean of the 15th and the 16th value; the cumulative frequencies are 5 and 17, so both lie in "10 to 20", the median class.
Why: the single values are lost, so 19.33 is an estimate. The true mean could be a little different.
Problem 24: What one outlier does
For the sample 5, 6, 7, 8, 9, 25 find the mean, the median and the standard deviation. Then remove the value 25 and calculate them again.
Idea. Compare the robust and the non-robust summaries (Parts 5, 7 and 10).
With the value 25 (the squared deviations are 25, 16, 9, 4, 1 and 225):
Without it (the squared deviations are 4, 1, 0, 1 and 4):
Why: one value moved the mean from 7 to 10 and made the standard deviation almost five times as large. The median moved from 7 to 7.5. Of the 280 in the sum of squares, 225 come from the single value 25.
Problem 25: Adding a value
Nine values have the mean 12. A tenth value, 22, is added. Find the new mean. Which tenth value would have left the mean unchanged?
Idea. Work with the total, (Part 4).
A tenth value of 12 would leave the mean at 12.
Why: a new value pulls the mean towards itself. A value equal to the mean pulls nowhere.
Problem 26: A complete description
The numbers of days that 11 patients stayed in a hospital: 2, 3, 3, 4, 5, 5, 5, 6, 7, 8, 18. Describe the data: mean, median, mode, quartiles, IQR, outliers and shape. Which summary would you report?
Idea. Everything together (Parts 4 to 6 and 10).
stands at the position , halfway between 3 and 4. stands at the position , halfway between 6 and 7.
The value 18 is an outlier. The mean (6) is above the median (5): the data is skewed to the right. Report the median 5 and the IQR 3, and mention the one long stay of 18 days.
Why: without the patient who stayed 18 days the mean would be , close to the median. A single patient changes the "average stay" by more than a day, so the mean alone would mislead a hospital that plans its beds.
15Practice problems
Forty problems in three levels. Solve each one on paper, type your result into the answer boxes and press Check. Under every problem there is a hint and a full solution: look at the hint only when you are stuck, and at the solution only after you have an answer of your own. Unless a problem says "population", treat the data as a sample. Find quartiles with the rule . Give results that are not whole numbers with three decimals.
Level 1: The centre
- 1.Find the mean of 7, 10, 4, 9, 5.
Show a hint
Add and divide by 5.Show one possible solution
Answer: .
- 2.Find the median of 15, 8, 22, 11, 19.
Show a hint
Sort first. The median is the 3rd of the 5 values.Show one possible solution
Answer: .
The middle value is 15.
- 3.Find the median of 6, 2, 9, 4.
Show a hint
With an even number of values, take the mean of the two middle ones.Show one possible solution
Answer: .
- 4.Find the mode of 3, 5, 5, 7, 8, 5, 3.
Show a hint
Which value appears most often?Show one possible solution
Answer: .
3 appears twice, 5 three times, 7 and 8 once. The mode is 5.
- 5.Find the mean and the median of 10, 12, 14, 16, 98. Which of the two describes a typical value better?
Show a hint
One value is far away from the others.Show one possible solution
Answer: Mean 30, median 14. The median.
The data is already sorted, and the middle value is 14. Four of the five values are far below 30, so the median describes the data better.
- 6.A grade is made of a quiz with the weight 0.4 and an exam with the weight 0.6. A student has 60 in the quiz and 85 in the exam. Find the grade.
Show a hint
Every mark times its weight.Show one possible solution
Answer: .
- 7.The number of cars in 20 households: 2 households have no car, 5 have one, 8 have two and 5 have three. Find the mean, the median and the mode.
Show a hint
Multiply the values by their frequencies. For the median, find the 10th and the 11th value with the cumulative frequencies.Show one possible solution
Answer: Mean 1.8, median 2, mode 2.
The cumulative frequencies are 2, 7, 15. The 10th and the 11th value are both 2, so the median is 2. The most frequent value is 2 (8 households).
- 8.Five values have the mean 20. Four of them are 18, 25, 16 and 21. Find the fifth.
Show a hint
The total is .Show one possible solution
Answer: .
The total is 100. The four known values add up to 80, so the fifth is .
- 9.A group of 10 people has the mean age 50, and a group of 30 people has the mean age 70. Find the mean age of all 40.
Show a hint
Weight the two means with the group sizes.Show one possible solution
Answer: .
- 10.25 values have the mean 8.4. What is their sum?
Show a hint
The sum is times the mean.Show one possible solution
Answer: .
- 11.The column "blood group" of a table has the values A, B, AB and 0. What kind of variable is it?
Show a hint
Are the values numbers? Do they have an order?Show one possible solution
Answer: Categorical, nominal.
The values are names without an order. Counts, shares and the mode make sense for it; a mean or a median does not.
- 12.In a survey 12 of 80 people chose the answer "never". Find the relative frequency of this answer.
Show a hint
.Show one possible solution
Answer: .
That is 15 percent.
- 13.Find the range of 14, 9, 27, 18, 11.
Show a hint
Largest minus smallest.Show one possible solution
Answer: .
- 14.Four values have the mean 10. A fifth value, 20, is added. Find the new mean.
Show a hint
First the old total.Show one possible solution
Answer: .
The old total is .
Level 2: The spread
- 15.Find , the median, and the IQR of 2, 4, 5, 7, 9, 10, 12, 15, 20.
Show a hint
With 9 values the positions are 3, 5 and 7.Show one possible solution
Answer: , median 9, , IQR 7.
The positions are , and . The 3rd, 5th and 7th values are 5, 9 and 12.
- 16.Find , the median and of 3, 6, 7, 10, 14, 18.
Show a hint
The positions are 2.25, 3.5 and 4.75.Show one possible solution
Answer: , median , .
: position , from 6 a quarter of the way to 7. Median: position 3.5, halfway between 7 and 10. : position 4.75, from 10 three quarters of the way to 14.
- 17.A data set has and . Find the two fences of the 1.5 IQR rule. Is the value 48 an outlier?
Show a hint
.Show one possible solution
Answer: The fences are 5 and 45. Yes.
The value 48 lies above 45, so it is an outlier.
- 18.The ages of nine members of a club: 10, 12, 13, 14, 15, 16, 17, 19, 40. Find , and the upper fence. Is 40 an outlier?
Show a hint
The quartiles are the 3rd and the 7th value.Show one possible solution
Answer: , , upper fence 23. Yes.
and , so .
The value 40 lies above it and is an outlier.
- 19.The values 2, 4, 6 and 8 are a whole population. Find the variance and the standard deviation.
Show a hint
Divide the sum of squares by .Show one possible solution
Answer: and .
The mean is . The deviations are , , 1, 3, and their squares 9, 1, 1, 9 add up to 20.
- 20.Find the variance and the standard deviation of the sample 5, 7, 9, 11, 13.
Show a hint
The mean is 9. Divide by .Show one possible solution
Answer: and .
The deviations are , , 0, 2, 4, and their squares 16, 4, 0, 4, 16 add up to 40.
- 21.Find the variance and the standard deviation of the sample 2, 3, 5, 6.
Show a hint
The mean is 4.Show one possible solution
Answer: and .
The deviations are , , 1, 2, and their squares 4, 1, 1, 4 add up to 10.
- 22.For a sample of 5 values, and . Find the mean, the variance and the standard deviation.
Show a hint
The shortcut: .Show one possible solution
Answer: Mean 6, , .
- 23.In a sample of 10 days a machine stopped 0 times on 3 days, once on 4 days and twice on 3 days. Find the mean and the standard deviation of the number of stops per day.
Show a hint
and .Show one possible solution
Answer: Mean 1, .
- 24.A data set has the mean 120 and the standard deviation 15. Find the z-scores of the values 150 and 105.
Show a hint
The value minus the mean, divided by .Show one possible solution
Answer: and .
- 25.A data set has the mean 40 and the standard deviation 6. Which value has the z-score ?
Show a hint
.Show one possible solution
Answer: .
- 26.The daily sales of a shop have the mean 80 and the standard deviation 12. Find the coefficient of variation.
Show a hint
.Show one possible solution
Answer: , or 15 percent.
- 27.The weights of bags are roughly bell-shaped with the mean 500 g and the standard deviation 20 g. About which share of the bags weighs between 460 and 540 g?
Show a hint
460 and 540 are two standard deviations from the mean.Show one possible solution
Answer: About 95 percent.
gives 460 and 540. By the 68-95-99.7 rule about 95 percent of bell-shaped data lie within two standard deviations.
- 28.Nothing is known about the shape of a data set. At least which share of its values lies within 2.5 standard deviations of the mean?
Show a hint
Chebyshev's rule with .Show one possible solution
Answer: At least 84 percent.
Level 3: Rescaling, shape and putting ideas together
- 29.A column has the mean 12 and the standard deviation 3. Every value is replaced by . Find the new mean, standard deviation and variance.
Show a hint
The shift changes only the mean.Show one possible solution
Answer: Mean 20, standard deviation 6, variance 36.
The variance also comes as .
- 30.The temperatures of a month have the mean 25 and the standard deviation 4 in degrees Celsius. Find the mean and the standard deviation in degrees Fahrenheit ().
Show a hint
Only the factor 1.8 touches the standard deviation.Show one possible solution
Answer: Mean 77, standard deviation 7.2.
- 31.The values 4, 6, 8, 10 and 12 are a population. Find the z-score of the value 12.
Show a hint
First and .Show one possible solution
Answer: About .
The mean is . The squared deviations 16, 4, 0, 4, 16 add up to 40.
- 32.The values 20, 30, 50 and 100 are scaled to the interval from 0 to 1 with min-max scaling. What do 30 and 50 become?
Show a hint
Subtract 20 and divide by 80.Show one possible solution
Answer: and .
- 33.The times that 20 students needed for a task, in classes: 0 to 20 minutes: 6 students, 20 to 40 minutes: 10 students, 40 to 60 minutes: 4 students. Estimate the mean time.
Show a hint
The midpoints are 10, 30 and 50.Show one possible solution
Answer: About minutes.
- 34.For the data 3, 4, 5, 6, 32 find the mean and the median. Then remove the value 32 and find them again.
Show a hint
Four values are left; their median is the mean of the two middle ones.Show one possible solution
Answer: With 32: mean 10, median 5. Without: mean 4.5, median 4.5.
The mean fell by 5.5, the median by 0.5.
- 35.A class of 30 students has the mean mark 72. The 20 girls have the mean 75. Find the mean of the 10 boys.
Show a hint
Work with totals.Show one possible solution
Answer: .
- 36.Ten values have the mean 15. Then a mistake is found: one value was written as 25 but should be 52. Find the correct mean.
Show a hint
How much does the total change?Show one possible solution
Answer: .
The old total is 150. The correction adds .
- 37.The marks of an exam have the mean 48 and the median 60. Is the distribution skewed to the left, skewed to the right, or symmetric?
Show a hint
The tail pulls the mean towards itself.Show one possible solution
Answer: Skewed to the left.
The mean lies below the median, so a tail of low values pulls it down: the data is skewed to the left. Most students did well and a few did very badly.
- 38.The sample 1, 2, 3, 4, 5 has the standard deviation . Without a new calculation, give the standard deviation of 101, 102, 103, 104, 105 and of 10, 20, 30, 40, 50.
Show a hint
The first set is shifted by 100, the second is multiplied by 10.Show one possible solution
Answer: About and .
A shift does not change the spread: . A factor 10 multiplies it by 10: .
- 39.The numbers of mistakes in seven reports: 1, 2, 2, 3, 4, 5, 11. Find the mean, the median, , , the IQR and the upper fence. Is 11 an outlier?
Show a hint
The positions of the quartiles are 2.5 and 5.5.Show one possible solution
Answer: Mean 4, median 3, , , IQR 2.5, upper fence 8.25. Yes.
The mean is , and the median is the 4th value, 3. stands at the position 2.5, between 2 and 2. stands at the position 5.5, halfway between 4 and 5.
The value 11 lies above it and is an outlier.
- 40.The true values of a target are 2, 4, 6 and 8. A model predicts the mean, 5, for every row. Find its mean squared error. Then find the mean squared error of a model that predicts 4 for every row.
Show a hint
.Show one possible solution
Answer: and .
Predicting 5, the errors are , , 1, 3. Predicting 4, they are , 0, 2, 4.
The first value, 5, is the population variance of the target. No constant prediction has a smaller mean squared error than the mean.
16Check yourself
Twelve quick questions. Your answers are saved in this browser, so you can leave the page and continue later. Each finished round is kept as an attempt below the questions; you can delete the attempts at any time.
0 of 12 answered.
17Checking your work with Python
This is the only code of the lesson. NumPy and Python's statistics module calculate every summary of this lesson.
import statistics
import numpy as np
data = np.array([2, 4, 4, 4, 5, 5, 7, 9])
print(data.mean(), np.median(data), statistics.mode(data.tolist()))
print(data.var(), data.std())
print(round(data.var(ddof=1), 3), round(data.std(ddof=1), 3))
values = np.array([3, 5, 7, 8, 9, 11, 13, 15, 16, 18, 21])
q1, q2, q3 = np.percentile(values, [25, 50, 75])
print(q1, q2, q3, q3 - q1)
z = (data - data.mean()) / data.std()
print(round(z.mean(), 3), round(z.std(), 3))Output
5.0 4.5 4 4.0 2.0 4.571 2.138 7.5 11.0 15.5 8.0 0.0 1.0
Change it and run: put the data of a problem you solved by hand into data and compare. Then change ddof=1 to ddof=0 in the third print and see which line of the output it now repeats.
The first line prints the mean, the median and the mode of the eight values of Part 7. The second line is the population variance and standard deviation: NumPy divides by unless told otherwise. The third line uses ddof=1, which means "divide by ": the sample variance and standard deviation. The fourth line gives the quartiles and the IQR of the eleven values of Part 6, with the same rule as by hand. The last line standardises the data and shows that the result has the mean 0 and the standard deviation 1.
| By hand | In Python |
|---|---|
| mean | np.mean(x) or x.mean() |
| median | np.median(x) |
| mode | statistics.mode(list) |
| population variance and standard deviation | np.var(x), np.std(x) |
| sample variance and standard deviation | np.var(x, ddof=1), np.std(x, ddof=1) |
| quartiles and percentiles | np.percentile(x, [25, 50, 75]) |
| all summaries of a table | df.describe() in pandas (its std uses ) |
18Quick summary
| Idea | Definition or rule |
|---|---|
| Kinds of data | categorical (nominal, ordinal) and numerical (discrete, continuous) |
| Frequency, relative, cumulative | the count; the share ; the count up to a value |
| Mean | ; the balance point; the deviations add up to 0 |
| Weighted mean | ; from a table: |
| Median | the middle of the sorted data; robust |
| Mode | the most frequent value |
| Mean against median | mean above the median: tail to the right; mean below the median: tail to the left |
| Range | largest minus smallest |
| Quartiles | in the sorted data, for , , |
| IQR | , the width of the middle half; robust |
| Five-number summary | minimum, , median, , maximum: the box plot |
| Outlier fences | and |
| Population variance | |
| Sample variance | |
| Standard deviation | the square root of the variance; the unit of the data |
| Shortcut | |
| z-score | |
| 68-95-99.7 rule | only for bell-shaped data |
| Chebyshev's rule | at least within standard deviations, for any data |
| Coefficient of variation | |
| centres: ; spreads: times ; variance: times | |
| Standardising | : mean 0, standard deviation 1 |
| Min-max scaling | : from 0 to 1 |
| Data in classes | use the midpoints: |