Start learning free

LESSON 121 OF 165 · MACHINE LEARNING: STATISTICS AND A/B TESTS

Center and Spread

Practice

A column of numbers has a center and a spread. For the center, .mean() gives back the average: every value added up, divided by how many there are. .median() gives back the middle value once the values are sorted. A few very big values pull the mean up but barely move the median.

visits = pd.Series([2, 3, 3, 4, 18])
print(visits.mean(), visits.median())   # 6.0 3.0
print((visits > visits.mean()).sum())   # 1

One customer with 18 visits lifts the mean to 6.0, above the other four. visits > visits.mean() gives back True or False for each value, and .sum() counts the True ones.

For the spread, .std() gives back the standard deviation: how far the values usually are from the mean (6.75 for these visits). .quantile([0.25, 0.5, 0.75]) gives back the quartiles as a Series: the value that a quarter, half and three quarters of the rows are at or below (3.0, 3.0 and 4.0 here). .describe() gives back all of these in one Series, with the count, the min and the max.

The starting code reads customers.csv and keeps the spent column in spent. When you are done, your script prints:

Customers: 300
Mean: 189.93 Median: 158.0
Above the mean: 119
Std: 151.52
0.25     72.75
0.50    158.00
0.75    270.25
Middle half spans: 197.5
count    300.00
mean     189.93
std      151.52
min        0.00
25%       72.75
50%      158.00
75%      270.25
max      720.00

Your task: print the mean and the median of spent, how many customers spent more than the mean, the standard deviation, the quartiles, the width of the middle half (the 0.75 quartile minus the 0.25 one) and spent.describe().


Assignment
Follow the Coding Tutorial to measure the center and the spread of what the shop's customers spent.


Hint
Round the mean and the std with round(x, 2); round describe() with .round(2) before .to_string(). quartiles[0.75] picks one quartile by its label.


STEP 1 OF 7
    Ctrl + Enter to check