Python for Introductory StatisticsXYZ Homework Edition

⇩ Download ▾

Weighted Mean and std

Watch demo video

numpy syntax for weighted mean and std:

import numpy as np
x = [x1,x2,x3,...]
w = [w1,w2,w3,...]  

np.average(x,weights=w)

numpy sample standard deviation:

import numpy as np
(np.cov(x,aweights=w))**(1/2)

Example: At the end of a term a student received a B in Biology (3 credits), A in History (3 credits), B in English Literature (4 credits), and C in Mathematics (5 credits). Find the students grade point average (GPA). Note: A = 4, B = 3, C = 2, D = 1 F = 0 grade points). GPAs are weighted mean. Find the weighted standard-deviation.

import numpy as np
x = [3,4,3,2]
w = [3,3,4,5]

np.average(x,weights=w)
Show expected output
2.8666666666666667
(np.cov(x,aweights=w))**.5
Show expected output
0.8359396992145376

Interpretation: The grade point average (GPA) is 2.87 with standard deviation 0.84

Mean and Standard deviation of Grouped Frequency Tables

Sometimes instead of the raw data, we are given a summary frequency table.

Example: Below is a frequency table of quiz scores from 26 students. Find the mean and standard deviation. In this case, we take the midpoint of each class to represent the class score.

classmidpointfreq
0-211
3-546
6-8710
9-11107
12-14130
15-17162

numpy syntax for mean and std with frequency as weight:

import numpy as np
np.average(x,weights=freq)

numpy sample standard deviation:

import numpy as np
(np.cov(x,fweights=freq),ddof=1)**(1/2)  

Example: Below is a frequency table of quiz scores from 26 students. Find the mean and standard deviation. In this case, we take the midpoint of each class to represent the class score.

classmidpointfreq
0-211
3-546
6-8710
9-11107
12-14130
15-17162
  1. Click + Code
  2. Type import numpy as np
  3. Type mid_points = [1,4,7,10,13,16]
  4. Type freq = [1,6,10,7,0,2]
  5. Type np.average(mid_points,weights=freq)
  6. Click play button
import numpy as np
mid_points = [1,4,7,10,13,16]
freq = [1,6,10,7,0,2]
np.average(mid_points,weights=freq)
Show expected output
7.576923076923077

Interpretation: the mean is 7.576923076923077

import numpy as np
mid_points = [1,4,7,10,13,16]
freq = [1,6,10,7,0,2]
(np.cov(mid_points,fweights=freq,ddof=1))**.5
Show expected output
3.5005494074282333

Interpretation: the sample standard deviation is 3.5005494074282333

import numpy as np

mid_points = [1,4,7,10,13,16]
freq = [1,6,10,7,0,2]
np.sqrt(np.cov(mid_points,fweights=freq,ddof=0))
Show expected output
3.432571103225427

Interpretation: The population standard deviation is 3.432571103225427

Adapted from Python for Introductory Statistics, by Simon Aman (Truman College, City Colleges of Chicago), licensed under CC BY 4.0. Changes were made: reformatted as an accessible XYZ web edition with live in-browser code cells. License: CC-BY-4.0.

These eBooks are a prerelease and are not yet certified conformant with WCAG 2.1 AA or ADA Title II. Every page is built against an automated accessibility gate, and the published editions will meet ADA Title II requirements when they release in late September 2026. If something is unusable, please tell us.