Can someone please help me with the following. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Can someone please help me with the following.

I want to add all the deviations and devide the answer by the number of deviation to calculate the mean deviation. But I don't know how to do it. https://code.sololearn.com/cGewJL9712dj/?ref=app

14th Jul 2020, 4:19 AM
Pankaj Kumar Panda
Pankaj Kumar Panda - avatar
5 Answers
+ 3
Use your a variable in your sdi function within the loop add each value to the variable a. a += deviations Then after the loop has completed, either print or return the value of a divided by the length of the list numbers to get the mean of the deviations. mean = a/len(x) return mean or print(mean) def sdi(x): a = 0 for xi in x: deviations = abs(xi - median) a += deviations print (deviations) mean = a/len(x) # print or return mean
14th Jul 2020, 4:48 AM
ChaoticDawg
ChaoticDawg - avatar
+ 3
print(f"{mean:.2f}")
14th Jul 2020, 6:16 AM
ChaoticDawg
ChaoticDawg - avatar
+ 2
Please tag a relevant language (Python) 👍
14th Jul 2020, 4:32 AM
Ipang
0
Thank you very much ChaoticDawg just one more question "how can I limit the mean deviation to only two decimal places.
14th Jul 2020, 5:29 AM
Pankaj Kumar Panda
Pankaj Kumar Panda - avatar
0
#ipang : ok I will do it next time.
14th Jul 2020, 5:31 AM
Pankaj Kumar Panda
Pankaj Kumar Panda - avatar