- 2
one standard deviation from the mean
How to find the one standard deviation from the mean of a list that contains heights of basketball players? The given list is: players = [180, 172, 178, 185, 190, 195, 200, 210, 190] "Basketball players The given code includes a list of heights for various basketball players. You need to calculate and output how many players are in the range of one standard deviation from the mean. output the result using the print statement."
9 Respuestas
+ 3
import numpy as np
players = [180, 172, 178, 185, 190, 195, 192, 200, 210, 190]
std = np.std(players)
mean = np.mean(players)
lower = mean - std
higher = mean + std
x = ([val for val in players if ((lower) < val and (higher) > val)])
print(len(x))
+ 2
You can use the fact that the number of elements in a list is its length.
for example
mean = sum(players) / len(players)
What code have you tried so far?
+ 1
#I have tried this one but it's not going through. Although it outputs 10 when I run it on the code playground
players = [180, 172, 178, 185, 190, 195, 192, 200, 210, 190]
mean=sum(players)/len(players)
std=sum(((i-mean)**2) for i in players)/len(players)**0.5
under=(mean-std)
above=(mean+std)
number=len([i for i in players if under<i<above])
print(number)
0
import numpy as np
arr = np.array(players)
std_mean = np.std(np.mean(players))
print (std_mean)
but its output is 0.0.
same i tried to output the mean of heights, then the variance, y then the squared root of variance. But it does not satisfy the project either
0
nout bote you can read this article,
https://www.investopedia.com/terms/e/empirical-rule.asp
According to what it says,1 standard deviation values will fall between (mean +- standard deviation).
So first find deviation using numpy.stdev or square root of variance.
And then check for values that lies between the above range.
0
I believe that i understand now
Thanks!
The next project is similar, but it have that pass a new condition. I'm going to try solve alone
0
import pandas as pd
import math
players = [180, 172, 178, 185, 190, 195, 192, 200, 210, 190]
# print(df)
players.sort()
men = sum(players)/len(players)
def var(ar,men):
sm = 0
for i in ar:
sm += (i-men)**2
return sm/len(ar)
varience = var(players,men)
st = int(math.sqrt(varience))
x = ([val for val in players if ((men-st) < val and (men+st) > val)])
print(len(x))
0
I already understood what one standard deviation from the mean means and also calculated the standard deviation of the array players.
I just don't know the code to implement to check values that lie between the above range
0
Here is how you can do it without built in library
Tips: Read the Qn. carefully the ans is to print the numbers of
datas within range not the datas.
players = [180, 172, 178, 185, 190, 195, 192, 200, 210, 190]
sum=0
sqs=0
for k in players:
sum+=k
N=len(players)
mean=sum/N
for val in players:
sqs+=(mean-val)**2
sd=(sqs/N)**(1/2)
lower=mean-sd
upper=mean+sd
arr=[]
arr1=[]
for i in players:
arr.append(float(i))
for i in arr:
if (i<=upper)and(i>=lower):
ind=arr.index(i)
arr1.append(players[ind])
print(len(arr1))