Skip to learning content

DATA SCIENCE PYTHON PLAYGROUND

Data Foundations · A little practice goes a long way.

← Visualise lessonsTINY TABLES · REAL PYTHON · YOUR PACE
Optional · distribution detail · V06 · 8 MIN

ECDF

Read the fraction at or below a value.

Exercises within this concept

  1. FollowFollow the techniqueCurrent exercise
  2. ChangeAdapt a requirement
  3. TransferCombine earlier skills

Understand the idea

An ECDF shows the fraction of observations at or below each x value. It uses the observed values without bins or smoothing.

Concept sketch: ECDF: fraction at or below x01ECDF: fraction at or below x
Illustration · not the exercise output

A small example

For [2, 4, 4, 8], the ECDF at x=4 is 3/4 = 0.75. The fraction strictly above 4 is 1/4 = 0.25.

Follow the code

Apply the idea to the supplied table. Read from top to bottom; the final line displays the result.

import matplotlib.pyplot as plt
import seaborn as sns

fig, ax = plt.subplots(figsize=(6, 4))
sns.ecdfplot(data=df, x="hours", ax=ax)
ax.set(title="Study club", xlabel="hours", ylabel="Proportion")
fig.tight_layout()
plt.show()

What each part does

sns.ecdfplot
cumulative fraction from observations
x=
numeric column along the horizontal axis

Your inputs

The editable setup on the right creates df. Run executes the setup and your work from top to bottom.

Study club · 8 synthetic rows
studentclubhoursscoregroup
AriArt152A
BoCode371A
CyArt265B
DeeCode482B
EliArt589A
FloCode376A
GusArt693B
HanCode261B

Your task · Follow

  1. Using df, call sns.ecdfplot for hours.
  2. Chart: title "Study club"; x "hours"; y "Proportion".
Hint

The ECDF height is the fraction at or below the horizontal value.

Reveal solution

One way to do it. Keep any supplied setup in the editor and use this in the Your work section.

import matplotlib.pyplot as plt
import seaborn as sns

fig, ax = plt.subplots(figsize=(6, 4))
sns.ecdfplot(data=df, x="hours", ax=ax)
ax.set(title="Study club", xlabel="hours", ylabel="Proportion")
fig.tight_layout()
plt.show()
Your task · Follow
  1. Using df, call sns.ecdfplot for hours.
  2. Chart: title "Study club"; x "hours"; y "Proportion".

Tab: indent · Shift+Tab: outdent · Esc, then Tab: leave editor

Edit Python. Control or Command plus Enter runs it. Tab indents by four spaces. Shift plus Tab outdents. Press Escape, then Tab or Shift plus Tab to leave the editor.

Each run executes all editor code in a fresh Python session. Display charts with plt.show().

Python starts when you open a lesson.

Output

Run your code to see what Python returns.