+ 1

# Basketball Players

Python for data science first challenge. Code appears to be giving the correct output but isn't passing the test case, which is hidden. Anyone have an idea why? Am i misinterpreting the task? https://code.sololearn.com/c15RTp4cpm28/?ref=app

14 ответов

+ 2

The task ask us to use the formula introduced in the course with n as a denominator.
stdev() calculates the standard deviation with n-1
https://code.sololearn.com/WU5Fss4TVVz0/?ref=app

+ 4

When calculating variance with a function, make sure you calculate the type of variance that you really want
n: "population variance"
n - 1: "sample variance"
Usually you will want to do the later of the 2

+ 4

p is for population, I suppose.
pstdev() is different from stdev()

+ 2

Paul K Sadler You can look at the docs and see what statistics.pstdev() yields as result ;)

+ 1

looking at the code, it seems correct to me. I used a while loop and counted, but it's just a different way of doing the same thing.
count = 0
i = 0
while i < numPlayers:
if(players[i] >= theMean - standardD and
players[i] <= theMean + standardD):
count +=1
i+=1
print(count)
The only other difference I see is that I calculated mean and standard deviation because I hadn't got to the library functions yet. The library functions surely calculate them correctly, so that's not it.
I even ran my code again, because I did it months ago, just in case something had changed. it still passes.
Next I ran my code outside of the challenge and did the same with yours. yours gets 7 and min, which passes, gets 6.
Next I checked the list data to be sure they matched and they do.
So I added prints to see our mean and standard deviation were the same. Themean matched at 189.2 but the standard deviation was different! Whaaaat? Yours is 11.1335332... and mine is 10.55219...idk.

+ 1

Thanks, I've just tried print(6) which passes, so clearly that's what it's looking for. I'll have to look more closely to find out why mine is arriving at 7. Thank you!

+ 1

Brilliant, thank you both! I had no idea of pstdev or indeed how it differed from stdev. That solves the problem but I shall go back and try to solve by coding the raw mathematics as it intended rather than importing statistics.

0

So now I am wondering if how I calculate standard deviation is slightly off, because surely the function is correct and that's why mine passed.
It also makes me wonder if our method is slightly off because our tests to include players is the same..
https://code.sololearn.com/cFE63ReNqfIy/?ref=app

0

Yeah, me too, I must be calculating the variance or standard deviation wrong

0

Lisa sorry, I was being lazy, I am doing this and working on something else at the same time 🙃😂
is pstdev a typo, or is that a different function?

0

Batl

0

players = [180, 172, 178, 185, 190, 195, 192, 200, 210, 190]
#new list array to get number of players in mean-std and mean+std
list=[]
#mean
mean=sum(players)/len(players)
#variance
x=[(i**2) for i in players]
var=(1/len(players))*(sum(x))-mean**2
#Standard Deviation
std=var**0.5
x,y=(int(mean-std),int(mean+std))
for i in players :
if i>x and i<y :
list.append(i)
#len
print(len(list))