Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Problem 8 : Weighted Means In this problem, we will create a function named mean ( ) that can be used to calculate either standard

Problem 8: Weighted Means
In this problem, we will create a function named mean() that can be used to calculate either standard means or weighted
means. The function will have two parameters, x and w. The parameter x is expected to be a numerical list, and w is
expected to be either a numerical list or to have the value None. If w=None, then the function should simply return the
mean (or average) of the elements in x. If w is a list, then the function should return the weighted mean of the elements in
x, using the elements of w as weights.
If the elements of x are denoted by 0,1,..., and the elements of w are denoted by 0,1,...,, then the weighted
mean is calculated as follows:
00+11++
0+1++
As an example (that you are not being asked to include in your notebook), if:
list1=[200,400,100]
list2=[3,2,5]
Then weighted_mean(list1, list2) should return: 3(200)+2(400)+5(100)
3+2+5=1900
10=190
6
Define a function named mean() with two parameters named x and w.. The parameter x is expected to be a numerical
list, and w is expected to be either a numerical list or to have the value None. The default value of w should be None. The
function should behave as follows:
If w=None, then the function should return the standard mean (average) of the values in x. You may use the
functions sum() and len() to calculate this.
If w is a list, then the function should return the weighted mean of the values in x, using the values from w as
weights.
In any case, this function should return only a single value.
The function should not print anything. It should not create any new lists, and should alter either of the lists provided
to it as arguments.
We will now test the mean() function. Create a new code cell to perform the steps below.
Create the list shown below:
values =[4,7,3,5,2,6,8,2,4,8]
weights =[2,1,3,1,2,3,1,4,2,1]
Use two calls to the function mean() to calculate the standard mean of values and then the weighted mean of
values, using the list weights to supply the weights. Print the results with messages as shown below.
Standard Mean: xxxx
Weighted Mean: xxxx
The call to mean() that is used to calculate the standard mean is expected to involve only one argument

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Pro PowerShell For Database Developers

Authors: Bryan P Cafferky

1st Edition

1484205413, 9781484205419

More Books

Students also viewed these Databases questions